Commit MetaInfo

Revisione4b0de5aaa6aca8089eaeac299fc5f9da1e14fcd (tree)
Time2019-02-10 21:20:29
Authoringlorion <homemicro@ingl...>
Commiteringlorion

Log Message

made emulator work with cartridges larger than 64KB

Change Summary

Incremental Difference

diff -r 8abec6ba0677 -r e4b0de5aaa6a emulator/hm1000.c
--- a/emulator/hm1000.c Fri Feb 08 22:26:06 2019 -0800
+++ b/emulator/hm1000.c Sun Feb 10 04:20:29 2019 -0800
@@ -228,29 +228,45 @@
228228 const bool scl_rising = (s->sercr & SERCR_SCL) == 0;
229229 if (scl_rising) {
230230 /* Rising SCL means we're clocking in a bit. */
231+ /* Usage of twi_status:
232+ * - We starte the lower 4 bits at 0xf and count down for every
233+ * bit we send or receive.
234+ * - Once the value reaches 7, this means we have sent or received
235+ * 8 bits and need to send or receive an acknowledgment.
236+ * - TWI_ADDR_SET indicates that we have received a TWI address.
237+ * twi_addr is used to store the address of the TWI device currently
238+ * being addressed. It is valid when TWI_ADDR_SET is set in twi_status.
239+ * cartridge_addr is used to store the current cartridge address.
240+ * It is set by the computer transmitting the bit sequence.
241+ * 1010xxx0 yyyyyyyy zzzzzzzz. This is also the sequence that initiates
242+ * a write to the cartridge. A read is performed by writing zero
243+ * bytes (just to set the address), then sending the sequence
244+ * 1010xxx1.
245+ */
231246 const bool addr_set = s->twi_status & TWI_ADDR_SET;
247+ const bool cart_write = (s->twi_addr & 0xf1) == 0xa0;
232248 const bool waiting_for_ack = (s->twi_status & 15) == 7;
233249 /* Shift incoming bit into serir. We start by setting it to the value
234250 * of the top bit in sercr, but allow clients to pull it low. */
235251 s->serir = (s->serir << 1) | (s->sercr >> 7);
236252 if (waiting_for_ack) {
237- if (s->cartridge && s->twi_addr >> 1 == 0x50) {
253+ if (s->cartridge && (s->twi_addr & 0xf0) == 0xa0) {
238254 if (addr_set) {
239255 /* Acknowledge address sent to cartridge. */
240- if (s->twi_addr == 0xa0) set_sda_low();
256+ if (cart_write) set_sda_low();
241257 } else {
242258 /* Acknowledge cartridge addresses. */
243259 set_sda_low();
244260 s->twi_status |= TWI_ADDR_SET;
245- if (s->twi_addr == 0xa0) {
246- s->cartridge_addr = 0;
261+ if (cart_write) {
262+ s->cartridge_addr = (s->twi_addr >> 1) & 7;
247263 s->cartridge_bits = 0;
248264 }
249265 }
250266 }
251267 s->twi_status |= 8;
252268 } else {
253- if (addr_set && s->twi_addr == 0xa1) {
269+ if (addr_set && (s->twi_addr & 0xf1) == 0xa1) {
254270 static uint8_t data = 0;
255271 if (s->cartridge_bits == 0) {
256272 data = s->cartridge[s->cartridge_addr];
@@ -264,7 +280,7 @@
264280 }
265281 if ((s->twi_status & 7) == 0) {
266282 if (addr_set) {
267- if (s->twi_addr == 0xa0)
283+ if (cart_write)
268284 s->cartridge_addr = (s->cartridge_addr << 8) | s->serir;
269285 } else {
270286 s->twi_addr = s->serir;
Show on old repository browser