#include "common.h" // 32X COMM static volatile uint16_t* const mars_comm0 = (uint16_t*) MARS_COMM0; static volatile uint16_t* const mars_comm2 = (uint16_t*) MARS_COMM2; static volatile uint16_t* const mars_comm8 = (uint16_t*) MARS_COMM8; static volatile uint32_t* const mars_comm12 = (uint32_t*) MARS_COMM12; // VDP static volatile uint16_t* const vdp_data_port = (uint16_t*) VDP_DATA_PORT; static volatile uint16_t* const vdp_ctrl_port = (uint16_t*) VDP_CTRL_PORT; static volatile uint32_t* const vdp_ctrl_wide = (uint32_t*) VDP_CTRL_PORT; // External functions extern uint16_t read_joypad(uint8_t player); uint32_t timer = 0; uint16_t vramOffset = 0; // It is recommended to put functions that run 1+ times every frame into RAM // by specifying this attribute before the signature. This keeps the M68K off // the ROM so the SH-2s can access it without slowdown. // It should be safe to add or remove it from any function and experiment with // the speed vs space differences __attribute__((section(".data"))) void vdp_color(uint16_t index, uint16_t color) { index <<= 1; *vdp_ctrl_wide = ((0xC000 + (((uint32_t)index) & 0x3FFF)) << 16) + (((uint32_t)index) >> 14); *vdp_data_port = color; } __attribute__((section(".data"))) void do_commands(void) { uint16_t cmd = *mars_comm0; switch(cmd >> 8) { default: break; // Unknown command case 0: return; // No command case 3: *mars_comm8 = read_joypad(cmd); break; case 4: break; case 5: // Set VRAM or Plane offset vramOffset = *mars_comm2; break; case 6: // Write tile to Plane B *vdp_ctrl_wide = (((uint32_t)0x6000 + ((vramOffset) & 0x3FFF)) << 16) + (((vramOffset) >> 14) | 0x03); *vdp_data_port = *mars_comm2; vramOffset += 2; break; case 7: // Write word to VRAM address *vdp_ctrl_wide = (((uint32_t)0x4000 + ((vramOffset) & 0x3FFF)) << 16) + (((vramOffset) >> 14) | 0x00); *vdp_data_port = *mars_comm2; vramOffset += 2; break; } *mars_comm0 = 0; } const uint16_t color_cycle[10] = { 0xEEE, 0xCCC, 0xAAA, 0x888, 0x666, 0x444, 0x666, 0x888, 0xAAA, 0xCCC }; __attribute__((section(".data"))) void main(void) { uint16_t ticks = 0, col = 0; while(1) { // Cycle background/border color if(++ticks >= 8) { ticks = 0; if(++col >= 10) col = 0; } vdp_color(0, color_cycle[col]); // TODO: Remove this after fixing _vblank while(*vdp_ctrl_port & 8) do_commands(); while(!(*vdp_ctrl_port & 8)) do_commands(); *mars_comm12 = ++timer; } }