Initial import

This commit is contained in:
Stefan Arentz 2015-01-13 19:42:14 -05:00
commit c16b34f14a
10 changed files with 2029 additions and 0 deletions

18
Makefile Normal file
View File

@ -0,0 +1,18 @@
CC=clang
CFLAGS=-O3 -std=c11 -Werror -Wall -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes
SOURCES=cpu.c ins.c pia.c mem.c ewm.c
OBJECTS=$(SOURCES:.c=.o)
LIBS=-lcurses
EXECUTABLE=ewm
all: $(SOURCES) $(EXECUTABLE)
clean:
rm -f $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $@
.c.o:
$(CC) $(CFLAGS) $< -c -o $@

340
cpu.c Normal file
View File

@ -0,0 +1,340 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include "cpu.h"
#include "ins.h"
/* Private API */
typedef void (*cpu_instruction_handler_t)(struct cpu_t *cpu);
typedef void (*cpu_instruction_handler_byte_t)(struct cpu_t *cpu, uint8_t oper);
typedef void (*cpu_instruction_handler_word_t)(struct cpu_t *cpu, uint16_t oper);
static uint8_t _mem_get_byte(struct cpu_t *cpu, uint16_t addr) {
return cpu->memory[addr];
}
static uint16_t _mem_get_word(struct cpu_t *cpu, uint16_t addr) {
return *((uint16_t*) &cpu->memory[addr]);
}
#if 1
static void cpu_format_instruction(struct cpu_t *cpu, char *buffer) {
*buffer = 0x00;
cpu_instruction_t *i = &instructions[cpu->memory[cpu->state.pc]];
uint8_t opcode = cpu->memory[cpu->state.pc];
/* Single byte instructions */
if (i->bytes == 1) {
sprintf(buffer, "%s", i->name);
}
/* JSR is the only exception */
else if (opcode == 0x20) {
sprintf(buffer, "%s $%.4X", i->name, _mem_get_word(cpu, cpu->state.pc+1));
}
/* Branches */
else if ((opcode & 0b00011111) == 0b00010000) {
int8_t offset = (int8_t) _mem_get_byte(cpu, cpu->state.pc+1);
uint16_t addr = cpu->state.pc + 2 + offset;
sprintf(buffer, "%s $%.4X", i->name, addr);
}
else if ((opcode & 0b00000011) == 0b00000001) {
switch ((opcode & 0b00011100) >> 2) {
case 0b000:
sprintf(buffer, "%s ($%.2X,X)", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b001:
sprintf(buffer, "%s $%.2X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b010:
sprintf(buffer, "%s #$%.2X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b011:
sprintf(buffer, "%s $%.2X%.2X", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
case 0b100:
sprintf(buffer, "%s ($%.2X),Y", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b101:
sprintf(buffer, "%s $%.2X,X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b110:
sprintf(buffer, "%s $%.2X%.2X,Y", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
case 0b111:
sprintf(buffer, "%s $%.2X%.2X,X", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
}
}
else if ((opcode & 0b00000011) == 0b00000010) {
switch ((opcode & 0b00011100) >> 2) {
case 0b000:
sprintf(buffer, "%s #$%.2X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b001:
sprintf(buffer, "%s $%.2X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b010:
sprintf(buffer, "%s", i->name);
break;
case 0b011:
sprintf(buffer, "%s $%.2X%.2X", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
case 0b101:
sprintf(buffer, "%s $%.2X,X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b111:
sprintf(buffer, "%s $%.2X%.2X,X", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
}
}
else if ((opcode & 0b00000011) == 0b00000000) {
switch ((opcode & 0b00011100) >> 2) {
case 0b000:
sprintf(buffer, "%s #$%.2X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b001:
sprintf(buffer, "%s $%.2X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b011:
sprintf(buffer, "%s $%.2X%.2X", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
case 0b101:
sprintf(buffer, "%s $%.2X,X", i->name, cpu->memory[cpu->state.pc+1]);
break;
case 0b111:
sprintf(buffer, "%s $%.2X%.2X,X", i->name, cpu->memory[cpu->state.pc+2], cpu->memory[cpu->state.pc+1]);
break;
}
}
}
static void cpu_format_state(struct cpu_t *cpu, char *buffer) {
sprintf(buffer, "A=%.2X X=%.2X Y=%.2X S=%.2X SP=%.4X %c%c%c%c%c%c%c%c",
cpu->state.a, cpu->state.x, cpu->state.y, cpu->state.s, cpu->state.sp,
cpu->state.n ? 'N' : '-',
cpu->state.v ? 'V' : '-',
'-',
cpu->state.b ? 'B' : '-',
cpu->state.d ? 'D' : '-',
cpu->state.i ? 'I' : '-',
cpu->state.z ? 'Z' : '-',
cpu->state.c ? 'C' : '-');
}
static void cpu_format_stack(struct cpu_t *cpu, char *buffer) {
*buffer = 0x00;
for (uint16_t sp = cpu->state.sp; sp != 0x01ff; sp++) {
char tmp[8];
sprintf(tmp, " %.2X", _mem_get_byte(cpu, sp));
buffer = strcat(buffer, tmp);
}
}
#endif
static mach_timebase_info_data_t timebase_info;
static uint64_t abs_to_nanos(uint64_t abs) {
return abs * timebase_info.numer / timebase_info.denom;
}
static uint64_t nanos_to_abs(uint64_t nanos) {
return nanos * timebase_info.denom / timebase_info.numer;
}
static int cpu_execute_instruction(struct cpu_t *cpu) {
/* Trace code - Refactor into its own function or module */
char trace_instruction[256];
char trace_state[256];
char trace_stack[256];
if (cpu->trace) {
cpu_format_instruction(cpu, trace_instruction);
}
uint64_t start_time = mach_absolute_time();
/* Fetch instruction */
cpu_instruction_t *i = &instructions[cpu->memory[cpu->state.pc]];
/* Remember the PC since some instructions modify it */
uint16_t pc = cpu->state.pc;
/* Advance PC */
if (pc == cpu->state.pc) {
cpu->state.pc += i->bytes;
}
/* Execute instruction */
switch (i->bytes) {
case 1:
((cpu_instruction_handler_t) i->handler)(cpu);
break;
case 2:
((cpu_instruction_handler_byte_t) i->handler)(cpu, _mem_get_byte(cpu, pc+1));
break;
case 3:
((cpu_instruction_handler_word_t) i->handler)(cpu, _mem_get_word(cpu, pc+1));
break;
}
if (cpu->trace) {
cpu_format_state(cpu, trace_state);
cpu_format_stack(cpu, trace_stack); // TODO: This crashes on the hello world test
switch (i->bytes) {
case 1:
fprintf(stderr, "CPU: %.4X %-20s | %.2X %-20s STACK: %s\n",
pc, trace_instruction, cpu->memory[pc], trace_state, trace_stack);
break;
case 2:
fprintf(stderr, "CPU: %.4X %-20s | %.2X %.2X %-20s STACK: %s\n",
pc, trace_instruction, cpu->memory[pc], cpu->memory[pc+1], trace_state, trace_stack);
break;
case 3:
fprintf(stderr, "CPU: %.4X %-20s | %.2X %.2X %.2X %-20s STACK: %s\n",
pc, trace_instruction, cpu->memory[pc], cpu->memory[pc+1], cpu->memory[pc+2], trace_state, trace_stack);
break;
}
}
/* Delay */
if (timebase_info.denom == 0) {
(void) mach_timebase_info(&timebase_info);
}
uint64_t now = mach_absolute_time();
uint64_t elapsed_nano = abs_to_nanos(now - start_time);
uint64_t expected_duration = (i->cycles * (1000000000 / 960000));
uint64_t delay_nano = expected_duration - elapsed_nano;
//fprintf(stderr, "Expected: %" PRId64 " Elapsed: %" PRId64 " Delay: %" PRId64 "\n", expected_duration, elapsed_nano, delay_nano);
mach_wait_until(now + nanos_to_abs(delay_nano));
return i->opcode;
}
static void iom_init(struct iom_t *iom, uint16_t start, uint16_t length, void *obj, iom_read_handler_t read_handler, iom_write_handler_t write_handler) {
memset(iom, 0x00, sizeof(struct iom_t));
iom->start = start;
iom->length = length;
iom->obj = obj;
iom->read_handler = read_handler;
iom->write_handler = write_handler;
}
/* Public API */
void cpu_init(struct cpu_t *cpu) {
memset(cpu, 0x00, sizeof(struct cpu_t));
cpu->memory = malloc(64 * 1024);
}
void cpu_add_ram(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data) {
if (data != NULL) {
memcpy(cpu->memory + start, data, length);
}
/* TODO: Mark pages as RAM */
}
void cpu_add_rom(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data) {
if (data != NULL) {
memcpy(cpu->memory + start, data, length);
}
/* TODO: Mark pages as ROM */
}
void cpu_add_iom(struct cpu_t *cpu, uint16_t start, uint16_t length, void *obj, iom_read_handler_t read_handler, iom_write_handler_t write_handler) {
struct iom_t *iom = (struct iom_t*) malloc(sizeof(struct iom_t));
iom_init(iom, start, length, obj, read_handler, write_handler);
iom->next = (struct iom_t*) cpu->iom;
cpu->iom = iom;
/* TODO: Mark pages as IO */
}
void cpu_trace(struct cpu_t *cpu, uint8_t trace) {
cpu->trace = trace;
}
void cpu_reset(struct cpu_t *cpu) {
cpu->state.pc = _mem_get_word(cpu, 0xfffc);
cpu->state.sp = 0x01ff;
}
void cpu_brk(struct cpu_t *cpu) {
}
void cpu_irq(struct cpu_t *cpu) {
/* TODO: Implement support for IRQ triggering */
}
void cpu_nmi(struct cpu_t *cpu) {
/* TODO: Implement support for interrupt triggering */
}
void cpu_run(struct cpu_t *cpu) {
uint64_t instruction_count = 0;
while (cpu_execute_instruction(cpu) != 0x00) {
/* TODO: Tick? */
instruction_count++;
}
fprintf(stderr, "Executed %" PRId64 " instructions\n", instruction_count);
}
void cpu_boot(struct cpu_t *cpu) {
cpu_reset(cpu);
cpu_run(cpu);
}
void cpu_step(struct cpu_t *cpu) {
cpu_execute_instruction(cpu);
}

76
cpu.h Normal file
View File

@ -0,0 +1,76 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef CPU_H
#define CPU_H
#include <stdint.h>
struct iom_t {
uint16_t start;
uint16_t length;
void *obj;
void *read_handler;
void *write_handler;
struct iom_t *next;
};
struct cpu_state_t {
uint8_t a, x, y, s;
uint16_t sp;
uint16_t pc;
uint8_t n, v, b, d, i, z, c;
};
struct cpu_t {
struct cpu_state_t state;
uint8_t *memory;
uint8_t trace;
struct iom_t *iom;
};
typedef uint8_t (*iom_read_handler_t)(struct cpu_t *cpu, void *obj, uint16_t addr);
typedef void (*iom_write_handler_t)(struct cpu_t *cpu, void *obj, uint16_t addr, uint8_t b);
void cpu_init(struct cpu_t *cpu);
void cpu_add_ram(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data);
void cpu_add_rom(struct cpu_t *cpu, uint16_t start, uint16_t length, uint8_t *data);
void cpu_add_iom(struct cpu_t *cpu, uint16_t start, uint16_t length, void *obj, iom_read_handler_t read_handler, iom_write_handler_t write_handler);
void cpu_trace(struct cpu_t *cpu, uint8_t trace);
void cpu_reset(struct cpu_t *cpu);
void cpu_brk(struct cpu_t *cpu);
void cpu_irq(struct cpu_t *cpu);
void cpu_nmi(struct cpu_t *cpu);
void cpu_run(struct cpu_t *cpu);
void cpu_boot(struct cpu_t *cpu);
void cpu_step(struct cpu_t *cpu);
uint16_t cpu_memory_get_word(struct cpu_t *cpu, uint16_t addr);
uint8_t cpu_memory_get_byte(struct cpu_t *cpu, uint16_t addr);
void cpu_memory_set_word(struct cpu_t *cpu, uint16_t addr, uint16_t v);
void cpu_memory_set_byte(struct cpu_t *cpu, uint16_t addr, uint8_t v);
#endif

80
ewm.c Normal file
View File

@ -0,0 +1,80 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "cpu.h"
#include "mem.h"
#include "pia.h"
static uint8_t monitor[256] = {
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
};
int main(int argc, char **argv) {
struct pia_t pia;
pia_init(&pia);
pia_trace(&pia, 0);
struct cpu_t cpu;
cpu_init(&cpu);
cpu_add_ram(&cpu, 0x0000, 0x4000, NULL);
cpu_add_iom(&cpu, 0xd000, 0x1000, &pia, pia_read, pia_write);
cpu_add_rom(&cpu, 0xff00, 0x0100, monitor);
cpu_trace(&cpu, 0);
cpu_boot(&cpu);
return 0;
}

1092
ins.c Normal file

File diff suppressed because it is too large Load Diff

38
ins.h Normal file
View File

@ -0,0 +1,38 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef INS_H
#define INS_H
#include <stdint.h>
typedef struct {
char *name;
uint8_t opcode;
uint8_t bytes;
uint8_t cycles;
void *handler;
} cpu_instruction_t;
extern cpu_instruction_t instructions[256];
#endif

162
mem.c Normal file
View File

@ -0,0 +1,162 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "mem.h"
/* Memory Access */
uint8_t mem_get_byte(struct cpu_t *cpu, uint16_t addr) {
if (cpu->iom) {
/* TODO: Assuming there is only one area of memory mapped io */
if (addr >= cpu->iom->start && addr < (cpu->iom->start + cpu->iom->length)) {
uint8_t v = ((iom_read_handler_t) cpu->iom->read_handler)((struct cpu_t*) cpu, cpu->iom->obj, addr);
//fprintf(stderr, "MEM: GETTING BYTE AT %.4X -> %.2X\n", addr, v);
return v;
}
}
//fprintf(stderr, "MEM: GETTING BYTE AT %.4X -> %.2X\n", addr, cpu->memory[addr]);
return cpu->memory[addr];
}
uint8_t mem_get_byte_abs(struct cpu_t *cpu, uint16_t addr) {
return mem_get_byte(cpu, addr);
}
uint8_t mem_get_byte_absx(struct cpu_t *cpu, uint16_t addr) {
return mem_get_byte(cpu, addr + cpu->state.x); /* TODO: Carry? */
}
uint8_t mem_get_byte_absy(struct cpu_t *cpu, uint16_t addr) {
return mem_get_byte(cpu, addr + cpu->state.y); /* TODO: Carry? */
}
uint8_t mem_get_byte_zpg(struct cpu_t *cpu, uint8_t addr) {
return mem_get_byte(cpu, addr);
}
uint8_t mem_get_byte_zpgx(struct cpu_t *cpu, uint8_t addr) {
return mem_get_byte(cpu, ((uint16_t) addr + cpu->state.x) & 0x00ff);
}
uint8_t mem_get_byte_zpgy(struct cpu_t *cpu, uint8_t addr) {
return mem_get_byte(cpu, ((uint16_t) addr + cpu->state.y) & 0x00ff);
}
uint8_t mem_get_byte_indx(struct cpu_t *cpu, uint8_t addr) {
return mem_get_byte(cpu, mem_get_word(cpu, addr + cpu->state.x)); // TODO: Does this wrap?
}
uint8_t mem_get_byte_indy(struct cpu_t *cpu, uint8_t addr) {
return mem_get_byte(cpu, mem_get_word(cpu, addr) + cpu->state.y);
}
void mem_set_byte(struct cpu_t *cpu, uint16_t addr, uint8_t v) {
//fprintf(stderr, "MEM: SETTING BYTE AT %.4X -> %.2X (%c)\n", addr, v, v & 0x7f);
if (cpu->iom) {
/* TODO: Assuming there is only one area of memory mapped io */
if (addr >= cpu->iom->start && addr < (cpu->iom->start + cpu->iom->length)) {
((iom_write_handler_t) cpu->iom->write_handler)((struct cpu_t*) cpu, cpu->iom->obj, addr, v);
return;
}
}
cpu->memory[addr] = v;
}
void mem_set_byte_zpg(struct cpu_t *cpu, uint8_t addr, uint8_t v) {
mem_set_byte(cpu, addr, v);
}
void mem_set_byte_zpgx(struct cpu_t *cpu, uint8_t addr, uint8_t v) {
mem_set_byte(cpu, ((uint16_t) addr + cpu->state.x) & 0x00ff, v);
}
void mem_set_byte_zpgy(struct cpu_t *cpu, uint8_t addr, uint8_t v) {
mem_set_byte(cpu, ((uint16_t) addr + cpu->state.y) & 0x00ff, v);
}
void mem_set_byte_abs(struct cpu_t *cpu, uint16_t addr, uint8_t v) {
mem_set_byte(cpu, addr, v);
}
void mem_set_byte_absx(struct cpu_t *cpu, uint16_t addr, uint8_t v) {
mem_set_byte(cpu, addr+cpu->state.x, v);
}
void mem_set_byte_absy(struct cpu_t *cpu, uint16_t addr, uint8_t v) {
mem_set_byte(cpu, addr+cpu->state.y, v);
}
void mem_set_byte_indx(struct cpu_t *cpu, uint8_t addr, uint8_t v) {
mem_set_byte(cpu, mem_get_word(cpu, addr+cpu->state.x), v); // TODO: Does this wrap?
}
void mem_set_byte_indy(struct cpu_t *cpu, uint8_t addr, uint8_t v) {
mem_set_byte(cpu, mem_get_word(cpu, addr)+cpu->state.y, v);
}
/* MOD */
void mem_mod_byte_zpg(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) {
mem_set_byte_zpg(cpu, addr, op(cpu, mem_get_byte_zpg(cpu, addr)));
}
void mem_mod_byte_zpgx(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) {
mem_set_byte_zpgx(cpu, addr, op(cpu, mem_get_byte_zpgx(cpu, addr)));
}
void mem_mod_byte_zpgy(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) {
mem_set_byte_zpgy(cpu, addr, op(cpu, mem_get_byte_zpgy(cpu, addr)));
}
void mem_mod_byte_abs(struct cpu_t *cpu, uint16_t addr, mem_mod_t op) {
mem_set_byte_abs(cpu, addr, op(cpu, mem_get_byte_abs(cpu, addr)));
}
void mem_mod_byte_absx(struct cpu_t *cpu, uint16_t addr, mem_mod_t op) {
mem_set_byte_absx(cpu, addr, op(cpu, mem_get_byte_absx(cpu, addr)));
}
void mem_mod_byte_absy(struct cpu_t *cpu, uint16_t addr, mem_mod_t op) {
mem_set_byte_absy(cpu, addr, op(cpu, mem_get_byte_absy(cpu, addr)));
}
void mem_mod_byte_indx(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) {
mem_set_byte_indx(cpu, addr, op(cpu, mem_get_byte_indx(cpu, addr)));
}
void mem_mod_byte_indy(struct cpu_t *cpu, uint8_t addr, mem_mod_t op) {
mem_set_byte_indy(cpu, addr, op(cpu, mem_get_byte_indy(cpu, addr)));
}
/* Words */
uint16_t mem_get_word(struct cpu_t *cpu, uint16_t addr) {
return *((uint16_t*) &cpu->memory[addr]);
}
void mem_set_word(struct cpu_t *cpu, uint16_t addr, uint16_t v) {
*((uint16_t*) &cpu->memory[addr]) = v;
}

62
mem.h Normal file
View File

@ -0,0 +1,62 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef MEM_H
#define MEM_H
#include "cpu.h"
typedef uint8_t (*mem_mod_t)(struct cpu_t *cpu, uint8_t b);
uint8_t mem_get_byte(struct cpu_t *cpu, uint16_t addr);
uint8_t mem_get_byte_abs(struct cpu_t *cpu, uint16_t addr);
uint8_t mem_get_byte_absx(struct cpu_t *cpu, uint16_t addr);
uint8_t mem_get_byte_absy(struct cpu_t *cpu, uint16_t addr);
uint8_t mem_get_byte_zpg(struct cpu_t *cpu, uint8_t addr);
uint8_t mem_get_byte_zpgx(struct cpu_t *cpu, uint8_t addr);
uint8_t mem_get_byte_zpgy(struct cpu_t *cpu, uint8_t addr);
uint8_t mem_get_byte_indx(struct cpu_t *cpu, uint8_t addr);
uint8_t mem_get_byte_indy(struct cpu_t *cpu, uint8_t addr);
void mem_set_byte(struct cpu_t *cpu, uint16_t addr, uint8_t v);
void mem_set_byte_zpg(struct cpu_t *cpu, uint8_t addr, uint8_t v);
void mem_set_byte_zpgx(struct cpu_t *cpu, uint8_t addr, uint8_t v);
void mem_set_byte_zpgy(struct cpu_t *cpu, uint8_t addr, uint8_t v);
void mem_set_byte_abs(struct cpu_t *cpu, uint16_t addr, uint8_t v);
void mem_set_byte_absx(struct cpu_t *cpu, uint16_t addr, uint8_t v);
void mem_set_byte_absy(struct cpu_t *cpu, uint16_t addr, uint8_t v);
void mem_set_byte_indx(struct cpu_t *cpu, uint8_t addr, uint8_t v);
void mem_set_byte_indy(struct cpu_t *cpu, uint8_t addr, uint8_t v);
void mem_mod_byte_zpg(struct cpu_t *cpu, uint8_t addr, mem_mod_t op);
void mem_mod_byte_zpgx(struct cpu_t *cpu, uint8_t addr, mem_mod_t op);
void mem_mod_byte_zpgy(struct cpu_t *cpu, uint8_t addr, mem_mod_t op);
void mem_mod_byte_abs(struct cpu_t *cpu, uint16_t addr, mem_mod_t op);
void mem_mod_byte_absx(struct cpu_t *cpu, uint16_t addr, mem_mod_t op);
void mem_mod_byte_absy(struct cpu_t *cpu, uint16_t addr, mem_mod_t op);
void mem_mod_byte_indx(struct cpu_t *cpu, uint8_t addr, mem_mod_t op);
void mem_mod_byte_indy(struct cpu_t *cpu, uint8_t addr, mem_mod_t op);
uint16_t mem_get_word(struct cpu_t *cpu, uint16_t addr);
void mem_set_word(struct cpu_t *cpu, uint16_t addr, uint16_t v);
#endif

119
pia.c Normal file
View File

@ -0,0 +1,119 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curses.h>
#include "cpu.h"
#include "pia.h"
static void pia_dsp_write(uint8_t b) {
//fprintf(stderr, "PIA: Sending to display: %.2x / %.2x\n", b, b & 0x7f);
b &= 0b01111111;
if (b == '\r') {
b = '\n';
}
addch(b);
refresh();
}
/* static int pia_kbd_read() { */
/* int c = getch(); */
/* } */
/* static uint8_t pia_kbd_read() { */
/* return getchar(); */
/* } */
void pia_init(struct pia_t *pia) {
initscr();
raw();
noecho();
pia->a = 0;
pia->cra = 0;
pia->b = 0;
pia->crb = 0;
}
void pia_trace(struct pia_t *pia, uint8_t trace) {
pia->trace = trace;
}
uint8_t pia_read(struct cpu_t *cpu, void *obj, uint16_t addr) {
struct pia_t *pia = (struct pia_t*) obj;
uint8_t result = 0;
switch (addr) {
case 0xd010: /* KBD */
result = pia->a;
break;
case 0xd011: /* KBDCR */ {
int c = getch();
if (c != ERR) {
/* TODO: Remove this, this is not how we want to stop the emulator */
if (c == 3) {
exit(1);
}
if (c == '\n') {
c = '\r';
}
pia->a = c | 0x80;
}
result = (c == ERR) ? 0x00 : 0x80;
break;
}
case 0xd012: /* DSP */
result = 0;
break;
case 0xd013: /* DSPCR */
result = 0;
break;
}
if (pia->trace) {
fprintf(stderr, "PIA: READ BYTE %.2X FROM %.4X\n", result, addr);
}
return result;
}
void pia_write(struct cpu_t *cpu, void *obj, uint16_t addr, uint8_t b) {
struct pia_t *pia = (struct pia_t*) obj;
if (pia->trace) {
fprintf(stderr, "PIA: WRITING BYTE %.2X TO %.4X\n", b, addr);
}
switch (addr) {
case 0xd010: /* KBD */
break;
case 0xd011: /* KBDCR */
pia->cra = b;
break;
case 0xd012: /* DSP */
if (pia->crb != 0x00) { /* TODO: Check the actual flag */
pia_dsp_write(b);
}
break;
case 0xd013: /* DSPCR */
pia->crb = b;
break;
}
}

42
pia.h Normal file
View File

@ -0,0 +1,42 @@
// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef PIA_H
#define PIA_H
#include <stdint.h>
#include "cpu.h"
struct pia_t {
uint8_t a;
uint8_t cra;
uint8_t b;
uint8_t crb;
uint8_t trace;
};
void pia_init(struct pia_t *pia);
void pia_trace(struct pia_t *pia, uint8_t trace);
uint8_t pia_read(struct cpu_t *cpu, void *obj, uint16_t addr);
void pia_write(struct cpu_t *cpu, void *obj, uint16_t addr, uint8_t b);
#endif