Commit MetaInfo

Revision69d68211fb798d6ce38f71fb1f1039a6d24928f0 (tree)
Time2019-07-22 15:23:23
Authoringlorion <homemicro@ingl...>
Commiteringlorion

Log Message

added cartridge write support to emulator

Change Summary

Incremental Difference

diff -r 0372971e292b -r 69d68211fb79 emulator/hm1000.c
--- a/emulator/hm1000.c Fri May 24 23:56:45 2019 -0700
+++ b/emulator/hm1000.c Sun Jul 21 23:23:23 2019 -0700
@@ -1,13 +1,16 @@
11 #include "hm1000.h"
22 #include "xcb.h"
33
4+#include <fcntl.h>
45 #include <stdbool.h>
56 #include <stdint.h>
67 #include <stdio.h>
78 #include <stdlib.h>
89 #include <string.h>
910 #include <time.h>
10-
11+#include <sys/mman.h>
12+#include <sys/stat.h>
13+#include <unistd.h>
1114 #include <errno.h>
1215
1316 /// To avoid spending excessive CPU time on sleep system calls, we
@@ -46,6 +49,10 @@
4649 #define TWI_ADDR_SET 0x10
4750 #define TWI_BITS_LEFT(X) (X & 7)
4851
52+#define CART_WAIT_A1 1
53+#define CART_WAIT_A2 2
54+#define CART_STATE_NORMAL 3
55+
4956 #define FATALF(FMT, ...) { fprintf(stderr, FMT "\n", __VA_ARGS__); exit(1); }
5057 #define FATAL(MSG) FATALF("%s", MSG)
5158
@@ -94,6 +101,7 @@
94101 uint16_t pc;
95102 uint8_t *cartridge;
96103 uint8_t cartridge_bits;
104+ uint8_t cartridge_state;
97105 size_t cartridge_addr;
98106 size_t cartridge_size;
99107 uint8_t *ram;
@@ -164,6 +172,9 @@
164172 if ((val & SERCR_SCL) == 0) return;
165173
166174 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;
167178 if (scl_rising) {
168179 /* Rising SCL means we're clocking in a bit. */
169180 /* Usage of twi_status:
@@ -188,7 +199,7 @@
188199 * of the top bit in sercr, but allow clients to pull it low. */
189200 s->serir = (s->serir << 1) | (s->sercr >> 7);
190201 if (waiting_for_ack) {
191- if (s->cartridge && (s->twi_addr & 0xf0) == 0xa0) {
202+ if (s->cartridge && addressing_cartridge) {
192203 if (addr_set) {
193204 /* Acknowledge address sent to cartridge. */
194205 if (cart_write) set_sda_low();
@@ -197,6 +208,7 @@
197208 set_sda_low();
198209 s->twi_status |= TWI_ADDR_SET;
199210 if (cart_write) {
211+ s->cartridge_state = CART_WAIT_A1;
200212 s->cartridge_addr = (s->twi_addr >> 1) & 7;
201213 s->cartridge_bits = 0;
202214 }
@@ -204,12 +216,13 @@
204216 }
205217 s->twi_status |= 8;
206218 } else {
207- if (addr_set && (s->twi_addr & 0xf1) == 0xa1) {
219+ if (addr_set && addressing_cartridge && !twi_addr_even) {
208220 static uint8_t data = 0;
209221 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;
213226 s->cartridge_bits = 8;
214227 }
215228 s->serir &= 0xfe | (data >> 7);
@@ -219,7 +232,18 @@
219232 if ((s->twi_status & 7) == 0) {
220233 if (addr_set) {
221234 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+ }
223247 } else {
224248 s->twi_addr = s->serir;
225249 }
@@ -237,6 +261,9 @@
237261 s->twi_status = 15;
238262 } else {
239263 /* Stop condition. */
264+ if (s->cartridge, addr_set && addressing_cartridge && twi_addr_even) {
265+ msync(s->cartridge, s->cartridge_size, MS_ASYNC);
266+ }
240267 s->twi_status = 0;
241268 }
242269 }
Show on old repository browser