7 #include "sim68xx/inc/base/cpu.h"
8 #include "sim68xx/inc/arch/m68xx/instr.h"
9 #include "sim68xx/inc/arch/m68xx/reg.h"
17 #define MEM_SIZE 0x10000
18 uint8_t mem[MEM_SIZE];
20 void load_s19(char *name) {
21 FILE *fp = fopen(name, "r");
28 while (fgets(line, 0x100, fp)) {
29 for (char *p = line; *p; ++p)
35 // allow comments and concatenation
36 if (line[0] != 'S' || line[1] != '1')
41 for (len = 0; len < 0x7f; ++len) {
42 char *p = line + 2 + len * 2;
53 buf[len] = (uint8_t)strtol(p, &q, 16);
56 fprintf(stderr, "not hex byte: %s\n", p);
62 fprintf(stderr, "too short: %s\n", line);
67 for (int i = 0; i < len; ++i)
69 if (checksum != 0xff) {
70 checksum -= 0xff + buf[len - 1];
73 "checksum %02x, should be %02x\n",
80 if (len != buf[0] + 1) {
81 fprintf(stderr, "incorrect length: %s\n", line);
86 int addr = (buf[1] << 8) | buf[2], end_addr;
87 end_addr = addr + len;
88 if (end_addr < addr || end_addr > MEM_SIZE) {
89 fprintf(stderr, "invalid load range: [0x%x, 0x%x)\n", addr, end_addr);
92 memcpy(mem + addr, buf + 3, len);
98 int read_byte(void *context, int addr) {
120 fprintf(stderr, "addr=%04x rd=%02x\n", addr, data);
125 void write_byte(void *context, int addr, int data) {
127 fprintf(stderr, "addr=%04x wr=%02x\n", addr, data);
153 u_char mem_getb(u_int addr) {
154 return read_byte(NULL, addr);
157 u_int mem_getw(u_int addr) {
158 /* Make sure hi byte is accessed first */
159 u_char hi = mem_getb(addr);
160 u_char lo = mem_getb(addr + 1);
161 return (hi << 8) | lo;
164 void mem_putb(u_int addr, u_char value) {
165 write_byte(NULL, addr, value);
168 void mem_putw(u_int addr, u_int value) {
169 mem_putb(addr, value >> 8); /* hi byte */
170 mem_putb(addr + 1, value & 0xFF); /* lo byte */
174 int main(int argc, char **argv) {
176 printf("usage: %s image.s19\n", argv[0]);
181 mem[0xf000] = 3; // bit 0 = rdrf, bit 1 = tdre
190 "pc=%04x a=%02x b=%02x s=%04x x=%04x p=%02x hf=%d if=%d nf=%d zf=%d vf=%d cf=%d\n",
210 cpu_6800_init(&cpu, read_byte, NULL, write_byte, NULL);
211 cpu_6800_reset(&cpu);
217 "pc=%04x a=%02x b=%02x s=%04x x=%04x p=%02x hf=%d if=%d nf=%d zf=%d vf=%d cf=%d\n",
233 cpu_6800_execute(&cpu);