diff nor.c @ 1519:1f745318f10a

Made the NOR flash emulation a bit more flexible, but not yet flexible enough to properly support the flash chip in the MegaWiFi cart
author Michael Pavone <pavone@retrodev.com>
date Wed, 31 Jan 2018 22:05:10 -0800
parents d94855080529
children
line wrap: on
line diff
--- a/nor.c	Wed Jan 31 21:59:08 2018 -0800
+++ b/nor.c	Wed Jan 31 22:05:10 2018 -0800
@@ -1,6 +1,7 @@
 #include "genesis.h"
 #include <stdlib.h>
 #include <string.h>
+#include "util.h"
 
 enum {
 	NOR_NORMAL,
@@ -31,9 +32,11 @@
 	state->cmd_state = NOR_CMD_IDLE;
 	state->alt_cmd = 0;
 	state->bus_flags = bus_flags;
+	state->cmd_address1 = 0x5555;
+	state->cmd_address2 = 0x2AAA;
 }
 
-void nor_run(nor_state *state, uint32_t cycle)
+void nor_run(nor_state *state, m68k_context *m68k, uint32_t cycle)
 {
 	if (state->last_write_cycle == 0xFFFFFFFF) {
 		return;
@@ -44,6 +47,10 @@
 			state->buffer[state->current_page + i] = state->page_buffer[i];
 		}
 		memset(state->page_buffer, 0xFF, state->page_size);
+		if (state->bus_flags == RAM_FLAG_BOTH) {
+			//TODO: add base address of NOR device to start and end addresses
+			m68k_invalidate_code_range(m68k, state->current_page, state->current_page + state->page_size);
+		}
 	}
 }
 
@@ -62,10 +69,13 @@
 		address = address >> 1;
 	}
 	
-	nor_run(state, m68k->current_cycle);
+	nor_run(state, m68k, m68k->current_cycle);
 	switch (state->mode)
 	{
 	case NOR_NORMAL:
+		if (state->bus_flags == RAM_FLAG_BOTH) {
+			address ^= 1;
+		}
 		return state->buffer[address & (state->size-1)];
 		break;
 	case NOR_PRODUCTID:
@@ -80,7 +90,7 @@
 			return 0xFE;
 		default:
 			return 0xFE;
-		}
+		}			//HERE
 		break;
 	case NOR_BOOTBLOCK:
 		break;
@@ -103,6 +113,9 @@
 		if (state->last_write_cycle != 0xFFFFFFFF) {
 			state->current_page = address & (state->size - 1) & ~(state->page_size - 1);
 		}
+		if (state->bus_flags == RAM_FLAG_BOTH) {
+			address ^= 1;
+		}
 		state->page_buffer[address & (state->page_size - 1)] = value;
 		break;
 	case NOR_PRODUCTID:
@@ -129,11 +142,11 @@
 		address = address >> 1;
 	}
 	
-	nor_run(state, m68k->current_cycle);
+	nor_run(state, m68k, m68k->current_cycle);
 	switch (state->cmd_state)
 	{
 	case NOR_CMD_IDLE:
-		if (value == 0xAA && (address & (state->size - 1)) == 0x5555) {
+		if (value == 0xAA && (address & (state->size - 1)) == state->cmd_address1) {
 			state->cmd_state = NOR_CMD_AA;
 		} else {
 			nor_write_byte(state, address, value, m68k->current_cycle);
@@ -141,16 +154,16 @@
 		}
 		break;
 	case NOR_CMD_AA:
-		if (value == 0x55 && (address & (state->size - 1)) == 0x2AAA) {
+		if (value == 0x55 && (address & (state->size - 1)) == state->cmd_address2) {
 			state->cmd_state = NOR_CMD_55;
 		} else {
-			nor_write_byte(state, 0x5555, 0xAA, m68k->current_cycle);
+			nor_write_byte(state, state->cmd_address1, 0xAA, m68k->current_cycle);
 			nor_write_byte(state, address, value, m68k->current_cycle);
 			state->cmd_state = NOR_CMD_IDLE;
 		}
 		break;
 	case NOR_CMD_55:
-		if ((address & (state->size - 1)) == 0x5555) {
+		if ((address & (state->size - 1)) == state->cmd_address1) {
 			if (state->alt_cmd) {
 				switch(value)
 				{
@@ -187,8 +200,8 @@
 				}
 			}
 		} else {
-			nor_write_byte(state, 0x5555, 0xAA, m68k->current_cycle);
-			nor_write_byte(state, 0x2AAA, 0x55, m68k->current_cycle);
+			nor_write_byte(state, state->cmd_address1, 0xAA, m68k->current_cycle);
+			nor_write_byte(state, state->cmd_address2, 0x55, m68k->current_cycle);
 			nor_write_byte(state, address, value, m68k->current_cycle);
 		}
 		state->cmd_state = NOR_CMD_IDLE;