From aca930d803177cb6ea8ebadd77b6ef09ab2b5b49 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 28 Nov 2020 11:20:03 +0100 Subject: addressed initializiation of components and valgrindified --- emu/6502.c | 11 ++++++++++- emu/6502.h | 2 +- emu/7seg.c | 7 ++++++- emu/7seg.h | 2 +- emu/emu.c | 14 ++++++++------ emu/memory.c | 9 ++++++++- emu/memory.h | 3 ++- emu/options.ggo | 4 ++++ emu/options.ggo.in | 4 ++++ 9 files changed, 44 insertions(+), 12 deletions(-) (limited to 'emu') diff --git a/emu/6502.c b/emu/6502.c index dbdf5a1..e39dc38 100644 --- a/emu/6502.c +++ b/emu/6502.c @@ -8,10 +8,19 @@ static const uint16_t reset_vector = 0xFFFC; static const uint16_t ZP_base = 0x0; static const uint16_t SP_base = 0x100; -void cpu_6502_init( cpu_6502_t *cpu, bus_t *bus ) +void cpu_6502_init( cpu_6502_t *cpu, bus_t *bus, bool initialize ) { cpu->bus = bus; cpu->debug_flags = 0; + cpu->steps = 0; + + if( initialize ) { + cpu->PC = 0x0000; + cpu->SP = 0x00; + cpu->A = 0x00; + cpu->X = 0x00; + cpu->Y = 0x00; + } } uint16_t cpu_6502_read_word( cpu_6502_t *cpu, uint16_t addr ) diff --git a/emu/6502.h b/emu/6502.h index 22da89f..baefd0f 100644 --- a/emu/6502.h +++ b/emu/6502.h @@ -59,7 +59,7 @@ enum { TXS_IMPL = 0x9A }; -void cpu_6502_init( cpu_6502_t *cpu, bus_t *bus ); +void cpu_6502_init( cpu_6502_t *cpu, bus_t *bus, bool initialize ); void cpu_6502_reset( cpu_6502_t *cpu ); uint8_t cpu_6502_read_byte( cpu_6502_t *cpu, uint16_t addr ); uint16_t cpu_6502_read_word( cpu_6502_t *cpu, uint16_t addr ); diff --git a/emu/7seg.c b/emu/7seg.c index 04a37f5..8a9ea1a 100644 --- a/emu/7seg.c +++ b/emu/7seg.c @@ -8,13 +8,18 @@ static device_vtable_t const seg7_vtable = { seg7_deinit }; -void seg7_init( seg7_t *seg, uint16_t addr ) +void seg7_init( seg7_t *seg, uint16_t addr, bool initialize ) { device_init( &seg->base, "seg7" ); seg->base.vtable = (device_vtable_t *)&seg7_vtable; seg->addr = addr; seg->debug = false; + + if( initialize ) { + seg->latch = 0x0000; + seg->shift = 0x0000; + } } uint8_t seg7_read( void *obj, uint16_t addr ) diff --git a/emu/7seg.h b/emu/7seg.h index 3953c70..98c6908 100644 --- a/emu/7seg.h +++ b/emu/7seg.h @@ -41,7 +41,7 @@ typedef struct seg7_t bool debug; } seg7_t; -void seg7_init( seg7_t *seg, uint16_t addr ); +void seg7_init( seg7_t *seg, uint16_t addr, bool initialize ); uint8_t seg7_read( void *obj, uint16_t addr ); void seg7_write( void *obj, uint16_t addr, uint8_t data ); diff --git a/emu/emu.c b/emu/emu.c index 95bc5cb..53431c2 100644 --- a/emu/emu.c +++ b/emu/emu.c @@ -34,23 +34,23 @@ int main( int argc, char *argv[] ) } if( args_info.long_version_given ) { - printf( "emu version: %s, Copyright (c) 2020, LGPLv3, Andreas Baumann \n", EMU_VERSION ); + printf( "emu version: %s, Copyright (c) 2020, GPLv3, Andreas Baumann \n", EMU_VERSION ); exit( EXIT_SUCCESS ); } bus_init( &bus ); - memory_init( &rom, MEMORY_ROM, ROM_START, ROM_SIZE ); + memory_init( &rom, MEMORY_ROM, ROM_START, ROM_SIZE, args_info.initialize_given ); memory_load( &rom, args_info.rom_arg ); bus_register( &bus, &rom.base, ROM_START, ROM_END ); - memory_init( &ram, MEMORY_RAM, RAM_START, RAM_SIZE ); + memory_init( &ram, MEMORY_RAM, RAM_START, RAM_SIZE, args_info.initialize_given ); bus_register( &bus, &ram.base, RAM_START, RAM_END ); - seg7_init( &seg7, VIA_START ); + seg7_init( &seg7, VIA_START, args_info.initialize_given ); bus_register( &bus, &seg7.base, VIA_START, VIA_END ); - cpu_6502_init( &cpu, &bus ); + cpu_6502_init( &cpu, &bus, args_info.initialize_given ); if( args_info.debug_given ) { if( args_info.print_cpu_given ) { cpu.debug_flags |= DEBUG_STATUS; @@ -65,7 +65,6 @@ int main( int argc, char *argv[] ) seg7.debug = true; } } - cpu_6502_reset( &cpu ); emul_init( &emul, &cpu, &bus, args_info.width_arg, args_info.height_arg ); if( args_info.gui_given ) { @@ -76,10 +75,13 @@ int main( int argc, char *argv[] ) emul.gui = true; } emul_start( &emul ); + + cpu_6502_reset( &cpu ); emul_run( &emul, args_info.steps_arg ); bus_deinit( &bus ); emul_free( &emul ); + cmdline_parser_free( &args_info ); exit( EXIT_SUCCESS ); } diff --git a/emu/memory.c b/emu/memory.c index 52332e3..c3f49a4 100644 --- a/emu/memory.c +++ b/emu/memory.c @@ -2,6 +2,7 @@ #include #include +#include static device_vtable_t const memory_vtable = { memory_read, @@ -9,7 +10,7 @@ static device_vtable_t const memory_vtable = { memory_deinit }; -void memory_init( memory_t *memory, memory_type_t type, uint16_t addr, uint16_t size ) +void memory_init( memory_t *memory, memory_type_t type, uint16_t addr, uint16_t size, bool initialize ) { device_init( &memory->base, ( type == MEMORY_ROM ) ? "ROM" : "RAM" ); @@ -17,6 +18,10 @@ void memory_init( memory_t *memory, memory_type_t type, uint16_t addr, uint16_t memory->addr = addr; memory->size = size; memory->cell = malloc( memory->size ); + if( initialize ) { + memset( memory->cell, 0, memory->size ); + } + memory->base.vtable = (device_vtable_t *)&memory_vtable; } @@ -58,6 +63,8 @@ void memory_write( void *obj, uint16_t addr, uint8_t data ) void memory_deinit( void *obj ) { memory_t *memory = (memory_t *)obj; + + free( memory->cell ); device_deinit( &memory->base ); } diff --git a/emu/memory.h b/emu/memory.h index a0d45b9..00e0de6 100644 --- a/emu/memory.h +++ b/emu/memory.h @@ -4,6 +4,7 @@ #include "device.h" #include +#include typedef enum memory_type_t { MEMORY_ROM = 1, @@ -20,7 +21,7 @@ typedef struct memory_t uint8_t *cell; } memory_t; -void memory_init( memory_t *memory, memory_type_t type, uint16_t addr, uint16_t size ); +void memory_init( memory_t *memory, memory_type_t type, uint16_t addr, uint16_t size, bool initialize ); void memory_load( memory_t *memory, const char *filename ); uint8_t memory_read( void *obj, uint16_t addr ); diff --git a/emu/options.ggo b/emu/options.ggo index 2f97ea1..5a93897 100644 --- a/emu/options.ggo +++ b/emu/options.ggo @@ -29,6 +29,10 @@ section "Run Options" int typestr="steps" default="-1" optional + + option "initialize" - + "Initialize all components with known values (not realistic, but useful for memchecks)" + optional section "Debug Options" diff --git a/emu/options.ggo.in b/emu/options.ggo.in index 9cfbff9..9e54009 100644 --- a/emu/options.ggo.in +++ b/emu/options.ggo.in @@ -29,6 +29,10 @@ section "Run Options" int typestr="steps" default="-1" optional + + option "initialize" - + "Initialize all components with known values (not realistic, but useful for memchecks)" + optional section "Debug Options" -- cgit v1.2.3-54-g00ecf