2015-09-13 00:09:01 +00:00
|
|
|
//
|
|
|
|
// main.c
|
|
|
|
// apple1
|
|
|
|
//
|
|
|
|
// Created by Daniel Loffgren on 9/12/15.
|
|
|
|
// Copyright (c) 2015 Daniel Loffgren. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2015-09-14 00:09:42 +00:00
|
|
|
#include <stdio.h>
|
2015-09-14 00:28:23 +00:00
|
|
|
#include <unistd.h>
|
2015-09-18 07:16:14 +00:00
|
|
|
#include <histedit.h>
|
2015-09-18 08:49:26 +00:00
|
|
|
#include <signal.h>
|
2015-09-18 07:16:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-09-14 00:42:17 +00:00
|
|
|
#include <dis6502/reverse.h>
|
2015-09-17 04:20:15 +00:00
|
|
|
#include <v6502/log.h>
|
2015-09-18 07:16:14 +00:00
|
|
|
#include <v6502/debugger.h>
|
|
|
|
#include <v6502/cpu.h>
|
|
|
|
#include <v6502/mem.h>
|
|
|
|
#include <as6502/error.h>
|
|
|
|
|
|
|
|
#include "pia.h"
|
2015-09-14 00:09:42 +00:00
|
|
|
|
2015-09-14 01:13:54 +00:00
|
|
|
#define ROM_START 0xF000
|
|
|
|
#define ROM_SIZE 0x00FF
|
|
|
|
#define RESET_VECTOR 0xFF00
|
2015-09-13 00:09:01 +00:00
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
static v6502_cpu *cpu;
|
|
|
|
static a1pia *pia;
|
|
|
|
|
|
|
|
static void fault(void *ctx, const char *e) {
|
2015-09-13 00:17:41 +00:00
|
|
|
(*(int *)ctx)++;
|
|
|
|
}
|
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
//static uint8_t romMirrorCallback(struct _v6502_memory *memory, uint16_t offset, int trap, void *context) {
|
|
|
|
// return memory->bytes[offset % ROM_SIZE];
|
|
|
|
//}
|
|
|
|
|
|
|
|
static void run(v6502_cpu *cpu) {
|
2015-09-18 08:29:31 +00:00
|
|
|
int faulted = 0;
|
|
|
|
cpu->fault_callback = fault;
|
|
|
|
cpu->fault_context = &faulted;
|
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
FILE *asmfile = fopen("runtime.s", "w");
|
|
|
|
pia_start(pia);
|
2015-09-18 08:29:31 +00:00
|
|
|
while (!faulted && !pia->signalled) {
|
2015-09-18 07:16:14 +00:00
|
|
|
dis6502_printAnnotatedInstruction(asmfile, cpu, cpu->pc);
|
|
|
|
v6502_step(cpu);
|
|
|
|
v6502_printCpuState(asmfile, cpu);
|
2015-09-14 00:09:42 +00:00
|
|
|
}
|
2015-09-18 07:16:14 +00:00
|
|
|
pia_stop(pia);
|
|
|
|
fclose(asmfile);
|
2015-09-14 00:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
static const char *prompt() {
|
|
|
|
static char prompt[10];
|
|
|
|
snprintf(prompt, 10, "(0x%04x) ", cpu->pc);
|
|
|
|
return prompt;
|
2015-09-14 00:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 00:17:41 +00:00
|
|
|
int main(int argc, const char * argv[])
|
|
|
|
{
|
2015-09-18 07:16:14 +00:00
|
|
|
cpu = v6502_createCPU();
|
2015-09-14 00:28:23 +00:00
|
|
|
cpu->memory = v6502_createMemory(v6502_memoryStartCeiling + 1);
|
2015-09-13 00:17:41 +00:00
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
v6502_breakpoint_list *breakpoint_list = v6502_createBreakpointList();
|
|
|
|
|
2015-09-14 00:09:42 +00:00
|
|
|
// Load Woz Monitor
|
2015-09-14 00:42:17 +00:00
|
|
|
for (uint16_t start = ROM_START;
|
|
|
|
start < v6502_memoryStartCeiling && start >= ROM_START;
|
2015-09-14 00:09:42 +00:00
|
|
|
start += ROM_SIZE + 1) {
|
2015-09-18 07:16:14 +00:00
|
|
|
v6502_loadFileAtAddress(cpu->memory, "apple1.rom", start);
|
2015-09-14 00:42:17 +00:00
|
|
|
//v6502_map(cpu->memory, start, ROM_SIZE, romMirrorCallback, NULL, NULL);
|
2015-09-14 00:09:42 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 00:28:23 +00:00
|
|
|
// Attach PIA
|
2015-09-18 07:16:14 +00:00
|
|
|
pia = pia_create(cpu->memory);
|
2015-09-14 00:28:23 +00:00
|
|
|
|
2015-09-13 00:17:41 +00:00
|
|
|
v6502_reset(cpu);
|
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
int verbose = 0;
|
|
|
|
int commandLen;
|
|
|
|
HistEvent ev;
|
|
|
|
History *hist = history_init();
|
|
|
|
history(hist, &ev, H_SETSIZE, 100);
|
|
|
|
|
|
|
|
EditLine *el = el_init("apple1", stdin, stdout, stderr);
|
|
|
|
el_set(el, EL_PROMPT, &prompt);
|
|
|
|
el_set(el, EL_SIGNAL, SIGWINCH);
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
|
|
|
el_set(el, EL_HIST, history, hist);
|
|
|
|
|
|
|
|
char *command = NULL;
|
|
|
|
while (!feof(stdin)) {
|
|
|
|
currentLineNum++;
|
|
|
|
|
|
|
|
const char *in = el_gets(el, &commandLen);
|
|
|
|
if (!in) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
history(hist, &ev, H_ENTER, in);
|
|
|
|
command = realloc(command, commandLen + 1);
|
|
|
|
memcpy(command, in, commandLen);
|
|
|
|
|
|
|
|
// Trim newline, always the last char
|
|
|
|
command[commandLen - 1] = '\0';
|
|
|
|
|
|
|
|
if (command[0] == '\0') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v6502_handleDebuggerCommand(cpu, command, commandLen, breakpoint_list, run, &verbose)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (command[0] != ';') {
|
|
|
|
as6502_executeAsmLineOnCPU(cpu, command, strlen(command));
|
|
|
|
}
|
2015-09-13 00:17:41 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 07:16:14 +00:00
|
|
|
v6502_destroyBreakpointList(breakpoint_list);
|
|
|
|
history_end(hist);
|
|
|
|
el_end(el);
|
|
|
|
free(command);
|
2015-09-14 01:33:42 +00:00
|
|
|
pia_destroy(pia);
|
2015-09-13 00:17:41 +00:00
|
|
|
v6502_destroyMemory(cpu->memory);
|
|
|
|
v6502_destroyCPU(cpu);
|
2015-09-18 07:16:14 +00:00
|
|
|
printf("\n");
|
|
|
|
return EXIT_SUCCESS;
|
2015-09-13 00:09:01 +00:00
|
|
|
}
|