From dfd4419db9141fcebd0e975851a6c64a3257c728 Mon Sep 17 00:00:00 2001 From: Thiago Auler Date: Sun, 12 Nov 2017 23:00:52 -0200 Subject: [PATCH] refactoring code to simplify it --- src/6502.c | 26 ++++++++++++++------------ src/inc/memory.h | 10 ++++------ src/inc/ram.h | 11 ----------- src/inc/rom.h | 37 ++++++++++++++++++++++++++++++++++--- src/inc/types.h | 14 +++----------- src/main.c | 7 ++++--- src/memory.c | 33 +++++++++++++++++---------------- src/opcodes.c | 25 +++++++++---------------- src/ram.c | 14 -------------- src/rom.c | 42 ------------------------------------------ 10 files changed, 85 insertions(+), 134 deletions(-) delete mode 100644 src/inc/ram.h delete mode 100644 src/ram.c delete mode 100644 src/rom.c diff --git a/src/6502.c b/src/6502.c index b72bcd1..52a5feb 100644 --- a/src/6502.c +++ b/src/6502.c @@ -1,5 +1,3 @@ -#include - #include "inc/types.h" #include "inc/memory.h" #include "inc/opcodes.h" @@ -10,20 +8,24 @@ oc2 opcode_decoded_2; void init() { // pc is set using 0xFFFC-0xFFFD - mem.mar = 0xFFFD; - mem_read(); - pc = mem.mdr << 8; - mem.mar = 0xFFFC; - mem_read(); - pc = mem.mdr + pc; + + dw word; + db low_byte; + db high_byte; + + low_byte = read_memory(0xFFFD); + high_byte = read_memory(0xFFFC); + + word = high_byte << 8; // shifts high byte to its place + word = word | low_byte; // appends low byte to form the complete word + + pc = word; } void fetch() { - mem.mar = pc; - mem_read(); - ir = mem.mdr; - pc++; + ir = read_memory(pc); + pc = pc + 1; } void decode() diff --git a/src/inc/memory.h b/src/inc/memory.h index 5722ee0..f881881 100644 --- a/src/inc/memory.h +++ b/src/inc/memory.h @@ -1,11 +1,9 @@ -#ifndef _APPLE_I_MEMORY_LOGIC_H_ -#define _APPLE_I_MEMORY_LOGIC_H_ +#ifndef _APPLE_I_MEMORY_H_ +#define _APPLE_I_MEMORY_H_ #include "types.h" -mr mem; // memory "glue" logic - -void mem_read(); -void mem_write(); + db read_memory(dw address); +void write_memory(dw address, db data); #endif diff --git a/src/inc/ram.h b/src/inc/ram.h deleted file mode 100644 index 79c4f3d..0000000 --- a/src/inc/ram.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _APPLE_I_RAM_H_ -#define _APPLE_I_RAM_H_ - -#include "types.h" - -mr ram; // random access memory - -void ram_read(); -void ram_write(); - -#endif diff --git a/src/inc/rom.h b/src/inc/rom.h index db3e3e1..59d782b 100644 --- a/src/inc/rom.h +++ b/src/inc/rom.h @@ -3,8 +3,39 @@ #include "types.h" -mr rom; // read only memory - -void rom_read(); +db rom_memory[] = { + 0xD8, 0x58, 0xA0, 0x7F, 0x8C, 0x12, 0xD0, 0xA9, + 0xA7, 0x8D, 0x11, 0xD0, 0x8D, 0x13, 0xD0, 0xC9, + 0xDF, 0xF0, 0x13, 0xC9, 0x9B, 0xF0, 0x03, 0xC8, + 0x10, 0x0F, 0xA9, 0xDC, 0x20, 0xEF, 0xFF, 0xA9, + 0x8D, 0x20, 0xEF, 0xFF, 0xA0, 0x01, 0x88, 0x30, + 0xF6, 0xAD, 0x11, 0xD0, 0x10, 0xFB, 0xAD, 0x10, + 0xD0, 0x99, 0x00, 0x02, 0x20, 0xEF, 0xFF, 0xC9, + 0x8D, 0xD0, 0xD4, 0xA0, 0xFF, 0xA9, 0x00, 0xAA, + 0x0A, 0x85, 0x2B, 0xC8, 0xB9, 0x00, 0x02, 0xC9, + 0x8D, 0xF0, 0xD4, 0xC9, 0xAE, 0x90, 0xF4, 0xF0, + 0xF0, 0xC9, 0xBA, 0xF0, 0xEB, 0xC9, 0xD2, 0xF0, + 0x3B, 0x86, 0x28, 0x86, 0x29, 0x84, 0x2A, 0xB9, + 0x00, 0x02, 0x49, 0xB0, 0xC9, 0x0A, 0x90, 0x06, + 0x69, 0x88, 0xC9, 0xFA, 0x90, 0x11, 0x0A, 0x0A, + 0x0A, 0x0A, 0xA2, 0x04, 0x0A, 0x26, 0x28, 0x26, + 0x29, 0xCA, 0xD0, 0xF8, 0xC8, 0xD0, 0xE0, 0xC4, + 0x2A, 0xF0, 0x97, 0x24, 0x2B, 0x50, 0x10, 0xA5, + 0x28, 0x81, 0x26, 0xE6, 0x26, 0xD0, 0xB5, 0xE6, + 0x27, 0x4C, 0x44, 0xFF, 0x6C, 0x24, 0x00, 0x30, + 0x2B, 0xA2, 0x02, 0xB5, 0x27, 0x95, 0x25, 0x95, + 0x23, 0xCA, 0xD0, 0xF7, 0xD0, 0x14, 0xA9, 0x8D, + 0x20, 0xEF, 0xFF, 0xA5, 0x25, 0x20, 0xDC, 0xFF, + 0xA5, 0x24, 0x20, 0xDC, 0xFF, 0xA9, 0xBA, 0x20, + 0xEF, 0xFF, 0xA9, 0xA0, 0x20, 0xEF, 0xFF, 0xA1, + 0x24, 0x20, 0xDC, 0xFF, 0x86, 0x2B, 0xA5, 0x24, + 0xC5, 0x28, 0xA5, 0x25, 0xE5, 0x29, 0xB0, 0xC1, + 0xE6, 0x24, 0xD0, 0x02, 0xE6, 0x25, 0xA5, 0x24, + 0x29, 0x07, 0x10, 0xC8, 0x48, 0x4A, 0x4A, 0x4A, + 0x4A, 0x20, 0xE5, 0xFF, 0x68, 0x29, 0x0F, 0x09, + 0xB0, 0xC9, 0xBA, 0x90, 0x02, 0x69, 0x06, 0x2C, + 0x12, 0xD0, 0x30, 0xFB, 0x8D, 0x12, 0xD0, 0x60, + 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, 0x00, 0x00 +}; #endif diff --git a/src/inc/types.h b/src/inc/types.h index d822094..372e67d 100644 --- a/src/inc/types.h +++ b/src/inc/types.h @@ -1,16 +1,8 @@ #ifndef _APPLE_I_TYPES_H_ #define _APPLE_I_TYPES_H_ -typedef unsigned char db; -typedef unsigned short dw; -typedef unsigned int dd; - -struct memory_registers -{ - dw mar; // memory address register - db mdr; // memory data register -}; - -typedef struct memory_registers mr; +typedef unsigned char db; // data byte (1 byte) +typedef unsigned short dw; // data word (2 bytes) +typedef unsigned int dd; // data double (4 bytes) #endif diff --git a/src/main.c b/src/main.c index 32ce5d6..3986ddb 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,12 @@ #include - #include "inc/6502.h" int main(int argc, char **argv) { printf("Apple-I Computer\n"); - init(); - run(); + + init(); // start the processor + run(); // and runs it + return 0; } diff --git a/src/memory.c b/src/memory.c index 3529595..f7a7368 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,9 +1,10 @@ #include "inc/types.h" -#include "inc/ram.h" #include "inc/rom.h" #include "inc/memory.h" /* + + memory model: + - - - - + | 0000 | @@ -30,36 +31,36 @@ + - - - - + */ + +db ram_memory[4096]; // total memory: 4KB -void mem_read() +db read_memory(dw address) { - if (mem.mar >= 0x0000 && mem.mar <= 0x0FFF) + if (address >= 0x0000 && address <= 0x0FFF) { // 4KB memory RAM - ram.mar = mem.mar; - ram_read(); - mem.mdr = ram.mdr; + return ram_memory[address]; } - else if (mem.mar >= 0xFF00 && mem.mar <= 0xFFFF) + else if (address >= 0xFF00 && address <= 0xFFFF) { // wozmon ROM - rom.mar = mem.mar & 0x00FF; - rom_read(); - mem.mdr = rom.mdr; + address = address & 0x00FF; + return rom_memory[address]; } else { - mem.mdr = 0x00; + // unused memory + return 0x00; } } -void mem_write() +void write_memory(dw address, db data) { - if (mem.mar >= 0x0000 && mem.mar <= 0x0FFF) + if (address >= 0x0000 && address <= 0x0FFF) { // 4KB memory RAM - ram.mar = mem.mar; - ram.mdr = mem.mdr; - ram_write(); + ram_memory[address] = data; } + + // any other addressed memory will be ignored on write } diff --git a/src/opcodes.c b/src/opcodes.c index 061a80d..c7f7990 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -3,23 +3,16 @@ #include "inc/opcodes.h" #include "inc/memory.h" -db fetch_operand(dw address) -{ - mem.mar = address; - mem_read(); - return mem.mdr; -} - dw fetch_address() { dw word; db low_byte; db high_byte; - low_byte = fetch_operand(pc); + low_byte = read_memory(pc); pc = pc + 1; - high_byte = fetch_operand(pc); + high_byte = read_memory(pc); pc = pc + 1; word = high_byte << 8; // shifts high byte to its place @@ -37,27 +30,27 @@ db decode_operand() switch (addressing_mode) { case immediate: - operand = fetch_operand(pc); + operand = read_memory(pc); pc = pc + 1; break; case zero_page: - byte = fetch_operand(pc); + byte = read_memory(pc); word = 0x0000 | byte; - operand = fetch_operand(word); + operand = read_memory(word); pc = pc + 1; break; case zero_page_x: - byte = fetch_operand(pc); + byte = read_memory(pc); word = 0x0000 | byte; word = word + x; - operand = fetch_operand(word); + operand = read_memory(word); pc = pc + 1; break; case zero_page_y: - byte = fetch_operand(pc); + byte = read_memory(pc); word = 0x0000 | byte; word = word + y; - operand = fetch_operand(word); + operand = read_memory(word); pc = pc + 1; break; case accumulator: diff --git a/src/ram.c b/src/ram.c deleted file mode 100644 index d95f1af..0000000 --- a/src/ram.c +++ /dev/null @@ -1,14 +0,0 @@ -#include "inc/types.h" -#include "inc/ram.h" - -db ram_memory[4096]; // total memory: 4KB - -void ram_read() -{ - ram.mdr = ram_memory[ram.mar]; -} - -void ram_write() -{ - ram_memory[ram.mar] = ram.mdr; -} diff --git a/src/rom.c b/src/rom.c deleted file mode 100644 index 5015dc5..0000000 --- a/src/rom.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "inc/types.h" -#include "inc/rom.h" - -db rom_memory[] = { - 0xD8, 0x58, 0xA0, 0x7F, 0x8C, 0x12, 0xD0, 0xA9, - 0xA7, 0x8D, 0x11, 0xD0, 0x8D, 0x13, 0xD0, 0xC9, - 0xDF, 0xF0, 0x13, 0xC9, 0x9B, 0xF0, 0x03, 0xC8, - 0x10, 0x0F, 0xA9, 0xDC, 0x20, 0xEF, 0xFF, 0xA9, - 0x8D, 0x20, 0xEF, 0xFF, 0xA0, 0x01, 0x88, 0x30, - 0xF6, 0xAD, 0x11, 0xD0, 0x10, 0xFB, 0xAD, 0x10, - 0xD0, 0x99, 0x00, 0x02, 0x20, 0xEF, 0xFF, 0xC9, - 0x8D, 0xD0, 0xD4, 0xA0, 0xFF, 0xA9, 0x00, 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x30, 0xFB, 0x8D, 0x12, 0xD0, 0x60, - 0x00, 0x00, 0x00, 0x0F, 0x00, 0xFF, 0x00, 0x00 -}; - -void rom_read() -{ - rom.mdr = rom_memory[rom.mar]; -}