src/main.c

changeset 9
3e99497dca33
parent 8
e2dcbabc7e1c
child 10
dbf4ba9563c9
     1.1 --- a/src/main.c	Sun Nov 28 20:02:45 2010 +0000
     1.2 +++ b/src/main.c	Sun Nov 28 20:52:53 2010 +0000
     1.3 @@ -7,7 +7,7 @@
     1.4  #include "musashi/m68k.h"
     1.5  #include "version.h"
     1.6  
     1.7 -#define ROM_SIZE (32768/4)
     1.8 +#define ROM_SIZE (32768/2)
     1.9  
    1.10  void state_done(void);
    1.11  
    1.12 @@ -21,10 +21,10 @@
    1.13  
    1.14  struct {
    1.15  	// Boot PROM can be up to 32Kbytes total size
    1.16 -	uint32_t	rom[ROM_SIZE];
    1.17 +	uint16_t	rom[ROM_SIZE];
    1.18  
    1.19  	// Main system RAM
    1.20 -	uint32_t	*ram;
    1.21 +	uint16_t	*ram;
    1.22  	size_t		ram_size;			// number of RAM bytes allocated
    1.23  	uint32_t	ram_addr_mask;		// address mask
    1.24  
    1.25 @@ -74,12 +74,8 @@
    1.26  	fread(romdat2, 1, romlen2, r14c);
    1.27  
    1.28  	// convert the ROM data
    1.29 -	for (size_t i=0; i<romlen; i+=2) {
    1.30 -		state.rom[i/2] = (
    1.31 -				(romdat1[i+0] << 24) |
    1.32 -				(romdat2[i+0] << 16) |
    1.33 -				(romdat1[i+1] << 8)  |
    1.34 -				(romdat2[i+1]));
    1.35 +	for (size_t i=0; i<romlen; i++) {
    1.36 +		state.rom[i] = ((romdat1[i] << 8) | (romdat2[i]));
    1.37  	}
    1.38  
    1.39  	// free the data arrays and close the files
    1.40 @@ -102,48 +98,56 @@
    1.41  // TODO: find a way to make musashi use function pointers instead of hard coded callbacks, maybe use a context struct too
    1.42  uint32_t m68k_read_memory_32(uint32_t address)
    1.43  {
    1.44 +	uint32_t data = 0xFFFFFFFF;
    1.45 +
    1.46 +	printf("RD32 %08X %d", address, state.romlmap);
    1.47 +
    1.48  	// If ROMLMAP is set, force system to access ROM
    1.49  	if (!state.romlmap)
    1.50  		address |= 0x800000;
    1.51  
    1.52 -	if (address >= 0xC00000) {
    1.53 -		// I/O Registers B
    1.54 -		// TODO
    1.55 -	} else if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
    1.56 +	if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
    1.57  		// ROM access
    1.58 -		return state.rom[(address & (ROM_SIZE-1)) / 4];
    1.59 -	} else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
    1.60 -		// I/O Registers A
    1.61 -		// TODO
    1.62 +		data = ((state.rom[(address & (ROM_SIZE-1)) / 2] << 16) | (state.rom[((address & (ROM_SIZE-1)) / 2)+1]));
    1.63  	} else if (address <= 0x3FFFFF) {
    1.64  		// RAM
    1.65 -		return state.ram[(address & state.ram_addr_mask) / 4];
    1.66 +		data = state.ram[(address & state.ram_addr_mask) / 2];
    1.67  	}
    1.68 -	return 0xffffffff;
    1.69 +
    1.70 +	printf(" ==> %04X\n", data);
    1.71 +	return data;
    1.72  }
    1.73  
    1.74  uint32_t m68k_read_memory_16(uint32_t address)
    1.75  {
    1.76 -	if (address & 2) {
    1.77 -		return m68k_read_memory_32(address) & 0xFFFF;
    1.78 -	} else {
    1.79 -		return (m68k_read_memory_32(address) >> 16) & 0xFFFF;
    1.80 -	}
    1.81 +	uint16_t data = 0xFFFF;
    1.82 +
    1.83 +	printf("RD16 %08X %d", address, state.romlmap);
    1.84 +
    1.85 +	// If ROMLMAP is set, force system to access ROM
    1.86 +	if (!state.romlmap)
    1.87 +		address |= 0x800000;
    1.88 +
    1.89 +	data = (m68k_read_memory_32(address) >> 16) & 0xFFFF;
    1.90 +
    1.91 +	printf(" ==> %04X\n", data);
    1.92 +	return data;
    1.93  }
    1.94  
    1.95  uint32_t m68k_read_memory_8(uint32_t address)
    1.96  {
    1.97 +	uint8_t data = 0xFF;
    1.98 +
    1.99 +	printf("RD 8 %08X %d ", address, state.romlmap);
   1.100 +
   1.101  	// If ROMLMAP is set, force system to access ROM
   1.102  	if (!state.romlmap)
   1.103  		address |= 0x800000;
   1.104  
   1.105 -	switch (address & 3) {
   1.106 -		case 3:		return m68k_read_memory_32(address)			& 0xFF;
   1.107 -		case 2:		return (m68k_read_memory_32(address) >> 8)	& 0xFF;
   1.108 -		case 1:		return (m68k_read_memory_32(address) >> 16)	& 0xFF;
   1.109 -		case 0:		return (m68k_read_memory_32(address) >> 24)	& 0xFF;
   1.110 -	}
   1.111 -	return 0xffffffff;
   1.112 +	data = m68k_read_memory_32(address) & 0xFF;
   1.113 +
   1.114 +	printf("==> %02X\n", data);
   1.115 +	return data;
   1.116  }
   1.117  
   1.118  // write m68k memory
   1.119 @@ -153,18 +157,19 @@
   1.120  	if (!state.romlmap)
   1.121  		address |= 0x800000;
   1.122  
   1.123 -	if (address >= 0xC00000) {
   1.124 -		// I/O Registers B
   1.125 -		// TODO
   1.126 -	} else if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
   1.127 +	printf("WR32 %08X %d %02X\n", address, state.romlmap, value);
   1.128 +
   1.129 +	if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
   1.130  		// ROM access
   1.131  		// TODO: bus error here? can't write to rom!
   1.132 -	} else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
   1.133 -		// I/O Registers A
   1.134 -		// TODO
   1.135  	} else if (address <= 0x3FFFFF) {
   1.136  		// RAM
   1.137 -		state.ram[(address & state.ram_addr_mask) / 4] = value;
   1.138 +		state.ram[(address & state.ram_addr_mask) / 2] = (value >> 16);
   1.139 +		state.ram[((address & state.ram_addr_mask) / 2)+1] = value & 0xffff;
   1.140 +	} else {
   1.141 +		switch (address) {
   1.142 +			case 0xE43000:	state.romlmap = ((value & 0x8000) == 0x8000);
   1.143 +		}
   1.144  	}
   1.145  }
   1.146  
   1.147 @@ -174,21 +179,18 @@
   1.148  	if (!state.romlmap)
   1.149  		address |= 0x800000;
   1.150  
   1.151 -	if (address >= 0xC00000) {
   1.152 -		// I/O Registers B
   1.153 -		// TODO
   1.154 -	} else if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
   1.155 +	printf("WR16 %08X %d %02X\n", address, state.romlmap, value);
   1.156 +
   1.157 +	if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
   1.158  		// ROM access
   1.159  		// TODO: bus error here? can't write to rom!
   1.160 -	} else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
   1.161 -		// I/O Registers A
   1.162 -		// TODO
   1.163  	} else if (address <= 0x3FFFFF) {
   1.164  		// RAM
   1.165 -		if (address & 2)
   1.166 -			state.ram[(address & state.ram_addr_mask) / 4] = (state.ram[(address & state.ram_addr_mask) / 4] & 0xFFFF0000) | (value & 0xFFFF);
   1.167 -		else
   1.168 -			state.ram[(address & state.ram_addr_mask) / 4] = (state.ram[(address & state.ram_addr_mask) / 4] & 0x0000FFFF) | ((value & 0xFFFF) << 16);
   1.169 +		state.ram[(address & state.ram_addr_mask) / 2] = value & 0xFFFF;
   1.170 +	} else {
   1.171 +		switch (address) {
   1.172 +			case 0xE43000:	state.romlmap = ((value & 0x8000) == 0x8000);
   1.173 +		}
   1.174  	}
   1.175  }
   1.176  
   1.177 @@ -198,26 +200,31 @@
   1.178  	if (!state.romlmap)
   1.179  		address |= 0x800000;
   1.180  
   1.181 -	if (address >= 0xC00000) {
   1.182 -		// I/O Registers B
   1.183 -		// TODO
   1.184 -	} else if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
   1.185 +	printf("WR 8 %08X %d %02X\n", address, state.romlmap, value);
   1.186 +
   1.187 +	if ((address >= 0x800000) && (address <= 0xBFFFFF)) {
   1.188  		// ROM access
   1.189  		// TODO: bus error here? can't write to rom!
   1.190 -	} else if ((address >= 0x400000) && (address <= 0x7FFFFF)) {
   1.191 -		// I/O Registers A
   1.192 -		// TODO
   1.193  	} else if (address <= 0x3FFFFF) {
   1.194  		// RAM
   1.195  		switch (address & 3) {
   1.196 +			// FIXME
   1.197  			case 3:		state.ram[(address & state.ram_addr_mask) / 4] = (state.ram[(address & state.ram_addr_mask) / 4] & 0xFFFFFF00) | (value & 0xFF);
   1.198  			case 2:		state.ram[(address & state.ram_addr_mask) / 4] = (state.ram[(address & state.ram_addr_mask) / 4] & 0xFFFF00FF) | ((value & 0xFF) << 8);
   1.199  			case 1:		state.ram[(address & state.ram_addr_mask) / 4] = (state.ram[(address & state.ram_addr_mask) / 4] & 0xFF00FFFF) | ((value & 0xFF) << 16);
   1.200  			case 0:		state.ram[(address & state.ram_addr_mask) / 4] = (state.ram[(address & state.ram_addr_mask) / 4] & 0x00FFFFFF) | ((value & 0xFF) << 24);
   1.201  		}
   1.202 +	} else {
   1.203 +		switch (address) {
   1.204 +			case 0xE43000:	state.romlmap = ((value & 0x80) == 0x80);
   1.205 +		}
   1.206  	}
   1.207  }
   1.208  
   1.209 +uint32_t m68k_read_disassembler_32(uint32_t addr) { return m68k_read_memory_32(addr); }
   1.210 +uint32_t m68k_read_disassembler_16(uint32_t addr) { return m68k_read_memory_16(addr); }
   1.211 +uint32_t m68k_read_disassembler_8 (uint32_t addr) { return m68k_read_memory_8 (addr); }
   1.212 +
   1.213  int main(void)
   1.214  {
   1.215  	// copyright banner
   1.216 @@ -234,6 +241,10 @@
   1.217  	m68k_set_cpu_type(M68K_CPU_TYPE_68010);
   1.218  	m68k_pulse_reset();
   1.219  
   1.220 +	char dasm[512];
   1.221 +	m68k_disassemble(dasm, 0x80001a, M68K_CPU_TYPE_68010);
   1.222 +	printf("%s\n", dasm);
   1.223 +
   1.224  	// set up SDL
   1.225  
   1.226  	// emulation loop!