Revision | 69d68211fb798d6ce38f71fb1f1039a6d24928f0 (tree) |
---|---|
Time | 2019-07-22 15:23:23 |
Author | inglorion <homemicro@ingl...> |
Commiter | inglorion |
added cartridge write support to emulator
@@ -1,13 +1,16 @@ | ||
1 | 1 | #include "hm1000.h" |
2 | 2 | #include "xcb.h" |
3 | 3 | |
4 | +#include <fcntl.h> | |
4 | 5 | #include <stdbool.h> |
5 | 6 | #include <stdint.h> |
6 | 7 | #include <stdio.h> |
7 | 8 | #include <stdlib.h> |
8 | 9 | #include <string.h> |
9 | 10 | #include <time.h> |
10 | - | |
11 | +#include <sys/mman.h> | |
12 | +#include <sys/stat.h> | |
13 | +#include <unistd.h> | |
11 | 14 | #include <errno.h> |
12 | 15 | |
13 | 16 | /// To avoid spending excessive CPU time on sleep system calls, we |
@@ -46,6 +49,10 @@ | ||
46 | 49 | #define TWI_ADDR_SET 0x10 |
47 | 50 | #define TWI_BITS_LEFT(X) (X & 7) |
48 | 51 | |
52 | +#define CART_WAIT_A1 1 | |
53 | +#define CART_WAIT_A2 2 | |
54 | +#define CART_STATE_NORMAL 3 | |
55 | + | |
49 | 56 | #define FATALF(FMT, ...) { fprintf(stderr, FMT "\n", __VA_ARGS__); exit(1); } |
50 | 57 | #define FATAL(MSG) FATALF("%s", MSG) |
51 | 58 |
@@ -94,6 +101,7 @@ | ||
94 | 101 | uint16_t pc; |
95 | 102 | uint8_t *cartridge; |
96 | 103 | uint8_t cartridge_bits; |
104 | + uint8_t cartridge_state; | |
97 | 105 | size_t cartridge_addr; |
98 | 106 | size_t cartridge_size; |
99 | 107 | uint8_t *ram; |
@@ -164,6 +172,9 @@ | ||
164 | 172 | if ((val & SERCR_SCL) == 0) return; |
165 | 173 | |
166 | 174 | const bool scl_rising = (s->sercr & SERCR_SCL) == 0; |
175 | + const bool addr_set = s->twi_status & TWI_ADDR_SET; | |
176 | + const bool addressing_cartridge = (s->twi_addr & 0xf0) == 0xa0; | |
177 | + const bool twi_addr_even = (s->twi_addr & 1) == 0; | |
167 | 178 | if (scl_rising) { |
168 | 179 | /* Rising SCL means we're clocking in a bit. */ |
169 | 180 | /* Usage of twi_status: |
@@ -188,7 +199,7 @@ | ||
188 | 199 | * of the top bit in sercr, but allow clients to pull it low. */ |
189 | 200 | s->serir = (s->serir << 1) | (s->sercr >> 7); |
190 | 201 | if (waiting_for_ack) { |
191 | - if (s->cartridge && (s->twi_addr & 0xf0) == 0xa0) { | |
202 | + if (s->cartridge && addressing_cartridge) { | |
192 | 203 | if (addr_set) { |
193 | 204 | /* Acknowledge address sent to cartridge. */ |
194 | 205 | if (cart_write) set_sda_low(); |
@@ -197,6 +208,7 @@ | ||
197 | 208 | set_sda_low(); |
198 | 209 | s->twi_status |= TWI_ADDR_SET; |
199 | 210 | if (cart_write) { |
211 | + s->cartridge_state = CART_WAIT_A1; | |
200 | 212 | s->cartridge_addr = (s->twi_addr >> 1) & 7; |
201 | 213 | s->cartridge_bits = 0; |
202 | 214 | } |
@@ -204,12 +216,13 @@ | ||
204 | 216 | } |
205 | 217 | s->twi_status |= 8; |
206 | 218 | } else { |
207 | - if (addr_set && (s->twi_addr & 0xf1) == 0xa1) { | |
219 | + if (addr_set && addressing_cartridge && !twi_addr_even) { | |
208 | 220 | static uint8_t data = 0; |
209 | 221 | if (s->cartridge_bits == 0) { |
210 | - data = s->cartridge[s->cartridge_addr]; | |
211 | - // printf("cartridge %08x: %02x\n", s->cartridge_addr, data); | |
212 | - s->cartridge_addr = (s->cartridge_addr + 1) % s->cartridge_size; | |
222 | + data = s->cartridge[s->cartridge_addr % s->cartridge_size]; | |
223 | + /* if (s->pc < 0xc000) */ | |
224 | + /* printf("cartridge %08x: %02x\n", s->cartridge_addr, data); */ | |
225 | + s->cartridge_addr += 1; | |
213 | 226 | s->cartridge_bits = 8; |
214 | 227 | } |
215 | 228 | s->serir &= 0xfe | (data >> 7); |
@@ -219,7 +232,18 @@ | ||
219 | 232 | if ((s->twi_status & 7) == 0) { |
220 | 233 | if (addr_set) { |
221 | 234 | if (cart_write) |
222 | - s->cartridge_addr = (s->cartridge_addr << 8) | s->serir; | |
235 | + if (addressing_cartridge && twi_addr_even) { | |
236 | + /* printf("to cartridge: %02x\n", s->serir); */ | |
237 | + if (s->cartridge_state < CART_STATE_NORMAL) { | |
238 | + s->cartridge_addr = (s->cartridge_addr << 8) | s->serir; | |
239 | + ++s->cartridge_state; | |
240 | + /* printf("cartridge_addr: %04x\n", s->cartridge_addr); */ | |
241 | + } else { | |
242 | + /* printf("cartridge[%08x] <- %02x\n", s->cartridge_addr, s->serir); */ | |
243 | + s->cartridge[s->cartridge_addr % s->cartridge_size] = s->serir; | |
244 | + ++s->cartridge_addr; | |
245 | + } | |
246 | + } | |
223 | 247 | } else { |
224 | 248 | s->twi_addr = s->serir; |
225 | 249 | } |
@@ -237,6 +261,9 @@ | ||
237 | 261 | s->twi_status = 15; |
238 | 262 | } else { |
239 | 263 | /* Stop condition. */ |
264 | + if (s->cartridge, addr_set && addressing_cartridge && twi_addr_even) { | |
265 | + msync(s->cartridge, s->cartridge_size, MS_ASYNC); | |
266 | + } | |
240 | 267 | s->twi_status = 0; |
241 | 268 | } |
242 | 269 | } |