1 /* CPU emulation loop and memory access
3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #define IOMASK 0xfffff000
30 #define IOBASE 0x00dff000
32 static uint8_t *memory = NULL;
33 static uint32_t memory_size = 0;
34 static uint16_t *chipreg = NULL;
35 static uint16_t (*io_read)(uint32_t address) = NULL;
36 static void (*io_write)(uint32_t address, uint16_t value) = NULL;
39 static uint16_t read_io_16(unsigned int address)
42 printf("Chip read from 0x%08x\n", address);
44 return io_read(address);
47 static void write_io_16(unsigned int address, uint16_t value)
50 printf("Chip write to 0x%08x\n", address);
52 if ((address & IOMASK) == IOBASE)
53 chipreg[address & ~IOMASK] = value;
54 io_write(address, value);
57 unsigned int m68k_read_memory_8(unsigned int address)
59 if (address > memory_size - 1) {
61 /* read lower (right) byte */
62 return read_io_16(address & ~1);
64 /* read upper (left) byte */
65 return read_io_16(address) >> 8;
68 return *((uint8_t *)(memory + address));
71 unsigned int m68k_read_memory_16(unsigned int address)
73 if (address > memory_size - 2) {
74 return read_io_16(address);
76 return (memory[address] << 8) |
80 unsigned int m68k_read_memory_32(unsigned int address)
82 if (address > memory_size - 4) {
84 value = read_io_16(address) << 16;
85 value |= read_io_16(address + 2);
88 return (memory[address] << 24)|
89 (memory[address + 1] << 16) |
90 (memory[address + 2] << 8) |
94 void m68k_write_memory_8(unsigned int address, unsigned int value)
96 if (address > memory_size - 1) {
98 /* write lower (right) byte */
99 write_io_16(address & ~1, 0xff00 | value);
101 /* write upper (left) byte */
102 write_io_16(address, 0x00ff | (value << 8));
106 *((uint8_t *)(memory + address)) = value;
109 void m68k_write_memory_16(unsigned int address, unsigned int value)
111 if (address > memory_size - 2) {
112 write_io_16(address, value);
115 memory[address] = value >> 8;
116 memory[address + 1] = value;
119 void m68k_write_memory_32(unsigned int address, unsigned int value)
121 if (address > memory_size - 4) {
122 write_io_16(address, value >> 16);
123 write_io_16(address + 2, value);
126 memory[address] = value >> 24;
127 memory[address + 1] = value >> 16;
128 memory[address + 2] = value >> 8;
129 memory[address + 3] = value;
132 void execute_init(int32_t _memory_size, uint8_t *_memory, uint16_t *_chipreg, uint16_t (*_io_read)(uint32_t address), void (*_io_write)(uint32_t address, uint16_t value))
134 memory_size = _memory_size;
138 io_write = _io_write;
142 m68k_set_cpu_type(M68K_CPU_TYPE_68000);
150 int execute_cpu(int irq, const struct cpu_stop stop_at[], int *event)
153 int instruction_count = 0;
160 /* loop until we returned from interrupt */
162 /* execute one opcode */
163 cycle_count += m68k_execute(0);
165 } while (REG_PC != save_pc);
167 printf("interrupt %d took %d opcodes\n", irq, instruction_count);
171 /* execute one opcode */
172 cycle_count += m68k_execute(0);
175 /* checking if game does not hit any 'stop_at' break point, give error output */
176 if (instruction_count >= 10000000) {
177 if (instruction_count == 10000000)
178 fprintf(stderr, "!!! games seems to got stuck in an endless loop, please fix !!!\n");
179 fprintf(stderr, "program counter at: %06x\n", REG_PC);
180 if (instruction_count == 10000020)
184 for (i = 0; stop_at[i].event; i++) {
185 if (REG_PC == stop_at[i].pc) {
187 printf("execution to address 0x%06x took %d opcodes\n", REG_PC, instruction_count);
189 *event = stop_at[i].event;