refactoring code to simplify it

This commit is contained in:
Thiago Auler 2017-11-12 23:00:52 -02:00
parent 10f9063872
commit dfd4419db9
10 changed files with 85 additions and 134 deletions

View File

@ -1,5 +1,3 @@
#include <stdio.h>
#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()

View File

@ -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

View File

@ -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

View File

@ -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, 0xD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E5, 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

View File

@ -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

View File

@ -1,11 +1,12 @@
#include <stdio.h>
#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;
}

View File

@ -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
}

View File

@ -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:

View File

@ -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;
}

View File

@ -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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