src/main.c

Thu, 30 Dec 2010 00:41:48 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 30 Dec 2010 00:41:48 +0000
changeset 87
822d8b2c4639
parent 79
674226015c8a
child 90
934ae2efdd01
permissions
-rw-r--r--

fix fdc irq handling (but irqs still disabled for now until the arbiter is sorted out)

philpem@0 1 #include <stdio.h>
philpem@7 2 #include <stdlib.h>
philpem@4 3 #include <stdint.h>
philpem@7 4 #include <stdbool.h>
philpem@7 5 #include <malloc.h>
philpem@7 6 #include <string.h>
philpem@18 7
philpem@20 8 #include "SDL.h"
philpem@20 9
philpem@4 10 #include "musashi/m68k.h"
philpem@7 11 #include "version.h"
philpem@18 12 #include "state.h"
philpem@40 13 #include "memory.h"
philpem@7 14
philpem@7 15 void FAIL(char *err)
philpem@7 16 {
philpem@7 17 state_done();
philpem@7 18 fprintf(stderr, "ERROR: %s\nExiting...\n", err);
philpem@7 19 exit(EXIT_FAILURE);
philpem@7 20 }
philpem@7 21
philpem@41 22 /**
philpem@41 23 * @brief Set the pixel at (x, y) to the given value
philpem@41 24 * @note The surface must be locked before calling this!
philpem@41 25 * @param surface SDL surface upon which to draw
philpem@41 26 * @param x X co-ordinate
philpem@41 27 * @param y Y co-ordinate
philpem@41 28 * @param pixel Pixel value (from SDL_MapRGB)
philpem@41 29 */
philpem@41 30 void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
philpem@41 31 {
philpem@41 32 int bpp = surface->format->BytesPerPixel;
philpem@41 33 /* Here p is the address to the pixel we want to set */
philpem@41 34 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
philpem@41 35
philpem@41 36 switch (bpp) {
philpem@41 37 case 1:
philpem@41 38 *p = pixel;
philpem@41 39 break;
philpem@41 40
philpem@41 41 case 2:
philpem@41 42 *(Uint16 *)p = pixel;
philpem@41 43 break;
philpem@41 44
philpem@41 45 case 3:
philpem@41 46 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
philpem@41 47 p[0] = (pixel >> 16) & 0xff;
philpem@41 48 p[1] = (pixel >> 8) & 0xff;
philpem@41 49 p[2] = pixel & 0xff;
philpem@41 50 }
philpem@41 51 else {
philpem@41 52 p[0] = pixel & 0xff;
philpem@41 53 p[1] = (pixel >> 8) & 0xff;
philpem@41 54 p[2] = (pixel >> 16) & 0xff;
philpem@41 55 }
philpem@41 56 break;
philpem@41 57
philpem@41 58 case 4:
philpem@41 59 *(Uint32 *)p = pixel;
philpem@41 60 break;
philpem@41 61
philpem@41 62 default:
philpem@41 63 break; /* shouldn't happen, but avoids warnings */
philpem@41 64 } // switch
philpem@41 65 }
philpem@41 66
philpem@41 67
philpem@41 68 /**
philpem@41 69 * @brief Refresh the screen.
philpem@41 70 * @param surface SDL surface upon which to draw.
philpem@41 71 */
philpem@41 72 void refreshScreen(SDL_Surface *s)
philpem@41 73 {
philpem@41 74 // Lock the screen surface (if necessary)
philpem@41 75 if (SDL_MUSTLOCK(s)) {
philpem@41 76 if (SDL_LockSurface(s) < 0) {
philpem@41 77 fprintf(stderr, "ERROR: Unable to lock screen!\n");
philpem@41 78 exit(EXIT_FAILURE);
philpem@41 79 }
philpem@41 80 }
philpem@41 81
philpem@41 82 // Map the foreground and background colours
philpem@42 83 Uint32 fg = SDL_MapRGB(s->format, 0x00, 0xFF, 0x00); // green foreground
philpem@42 84 // Uint32 fg = SDL_MapRGB(s->format, 0xFF, 0xC1, 0x06); // amber foreground
philpem@42 85 // Uint32 fg = SDL_MapRGB(s->format, 0xFF, 0xFF, 0xFF); // white foreground
philpem@42 86 Uint32 bg = SDL_MapRGB(s->format, 0x00, 0x00, 0x00); // black background
philpem@41 87
philpem@41 88 // Refresh the 3B1 screen area first. TODO: only do this if VRAM has actually changed!
philpem@41 89 uint32_t vram_address = 0;
philpem@41 90 for (int y=0; y<348; y++) {
philpem@41 91 for (int x=0; x<720; x+=16) { // 720 pixels, monochrome, packed into 16bit words
philpem@41 92 // Get the pixel
philpem@41 93 uint16_t val = RD16(state.vram, vram_address, sizeof(state.vram)-1);
philpem@41 94 vram_address += 2;
philpem@41 95 // Now copy it to the video buffer
philpem@41 96 for (int px=0; px<16; px++) {
philpem@41 97 if (val & 1)
philpem@41 98 putpixel(s, x+px, y, fg);
philpem@41 99 else
philpem@41 100 putpixel(s, x+px, y, bg);
philpem@45 101 val >>= 1;
philpem@41 102 }
philpem@41 103 }
philpem@41 104 }
philpem@41 105
philpem@41 106 // TODO: blit LEDs and status info
philpem@41 107
philpem@41 108 // Unlock the screen surface
philpem@41 109 if (SDL_MUSTLOCK(s)) {
philpem@41 110 SDL_UnlockSurface(s);
philpem@41 111 }
philpem@41 112
philpem@41 113 // Trigger a refresh -- TODO: partial refresh depending on whether we
philpem@41 114 // refreshed the screen area, status area, both, or none. Use SDL_UpdateRect() for this.
philpem@41 115 SDL_Flip(s);
philpem@41 116 }
philpem@41 117
philpem@47 118 /**
philpem@47 119 * @brief Handle events posted by SDL.
philpem@47 120 */
philpem@47 121 bool HandleSDLEvents(SDL_Surface *screen)
philpem@47 122 {
philpem@47 123 SDL_Event event;
philpem@47 124 while (SDL_PollEvent(&event))
philpem@47 125 {
philpem@47 126 switch (event.type) {
philpem@47 127 case SDL_QUIT:
philpem@47 128 // Quit button tagged. Exit.
philpem@47 129 return true;
philpem@47 130 case SDL_KEYDOWN:
philpem@47 131 switch (event.key.keysym.sym) {
philpem@48 132 case SDLK_F12:
philpem@47 133 if (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))
philpem@48 134 // ALT-F12 pressed; exit emulator
philpem@47 135 return true;
philpem@47 136 break;
philpem@47 137 default:
philpem@47 138 break;
philpem@47 139 }
philpem@47 140 break;
philpem@47 141 default:
philpem@47 142 break;
philpem@47 143 }
philpem@47 144 }
philpem@47 145
philpem@47 146 return false;
philpem@47 147 }
philpem@47 148
philpem@47 149
philpem@27 150 /****************************
philpem@27 151 * blessed be thy main()...
philpem@27 152 ****************************/
philpem@27 153
philpem@0 154 int main(void)
philpem@0 155 {
philpem@7 156 // copyright banner
philpem@16 157 printf("FreeBee: A Quick-and-Dirty AT&T 3B1 Emulator. Version %s, %s mode.\n", VER_FULLSTR, VER_BUILD_TYPE);
philpem@17 158 printf("Copyright (C) 2010 P. A. Pemberton. All rights reserved.\nLicensed under the Apache License Version 2.0.\n");
philpem@17 159 printf("Musashi M680x0 emulator engine developed by Karl Stenerud <kstenerud@gmail.com>\n");
philpem@16 160 printf("Built %s by %s@%s.\n", VER_COMPILE_DATETIME, VER_COMPILE_BY, VER_COMPILE_HOST);
philpem@16 161 printf("Compiler: %s\n", VER_COMPILER);
philpem@16 162 printf("CFLAGS: %s\n", VER_CFLAGS);
philpem@17 163 printf("\n");
philpem@7 164
philpem@7 165 // set up system state
philpem@7 166 // 512K of RAM
philpem@55 167 int i;
philpem@63 168 if ((i = state_init(512*1024, 512*1024)) != STATE_E_OK) {
philpem@55 169 fprintf(stderr, "ERROR: Emulator initialisation failed. Error code %d.\n", i);
philpem@55 170 return i;
philpem@55 171 }
philpem@7 172
philpem@20 173 // set up musashi and reset the CPU
philpem@7 174 m68k_set_cpu_type(M68K_CPU_TYPE_68010);
philpem@7 175 m68k_pulse_reset();
philpem@9 176
philpem@28 177 // Set up SDL
philpem@20 178 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
philpem@20 179 printf("Could not initialise SDL: %s.\n", SDL_GetError());
philpem@28 180 exit(EXIT_FAILURE);
philpem@20 181 }
philpem@7 182
philpem@28 183 // Make sure SDL cleans up after itself
philpem@28 184 atexit(SDL_Quit);
philpem@28 185
philpem@28 186 // Set up the video display
philpem@28 187 SDL_Surface *screen = NULL;
philpem@28 188 if ((screen = SDL_SetVideoMode(720, 384, 8, SDL_SWSURFACE | SDL_ANYFORMAT)) == NULL) {
philpem@28 189 printf("Could not find a suitable video mode: %s.\n", SDL_GetError());
philpem@28 190 exit(EXIT_FAILURE);
philpem@28 191 }
philpem@32 192 printf("Set %dx%d at %d bits-per-pixel mode\n\n", screen->w, screen->h, screen->format->BitsPerPixel);
philpem@28 193 SDL_WM_SetCaption("FreeBee 3B1 emulator", "FreeBee");
philpem@28 194
philpem@52 195 // Load a disc image
philpem@52 196 FILE *disc = fopen("discim", "rb");
philpem@55 197 if (!disc) {
philpem@55 198 fprintf(stderr, "ERROR loading disc image 'discim'.\n");
philpem@55 199 return -4;
philpem@55 200 }
philpem@52 201 wd2797_load(&state.fdc_ctx, disc, 512, 10, 2);
philpem@52 202
philpem@20 203 /***
philpem@20 204 * The 3B1 CPU runs at 10MHz, with DMA running at 1MHz and video refreshing at
philpem@20 205 * around 60Hz (???), with a 60Hz periodic interrupt.
philpem@20 206 */
philpem@76 207 const uint32_t TIMESLOT_FREQUENCY = 1000;//240; // Hz
philpem@20 208 const uint32_t MILLISECS_PER_TIMESLOT = 1e3 / TIMESLOT_FREQUENCY;
philpem@20 209 const uint32_t CLOCKS_PER_60HZ = (10e6 / 60);
philpem@20 210 uint32_t next_timeslot = SDL_GetTicks() + MILLISECS_PER_TIMESLOT;
philpem@20 211 uint32_t clock_cycles = 0;
philpem@16 212 bool exitEmu = false;
philpem@87 213 // bool lastirq_fdc = false;
philpem@16 214 for (;;) {
philpem@20 215 // Run the CPU for however many cycles we need to. CPU core clock is
philpem@20 216 // 10MHz, and we're running at 240Hz/timeslot. Thus: 10e6/240 or
philpem@20 217 // 41667 cycles per timeslot.
philpem@20 218 clock_cycles += m68k_execute(10e6/TIMESLOT_FREQUENCY);
philpem@20 219
philpem@53 220 // Run the DMA engine
philpem@76 221 if (state.dmaen) {
philpem@53 222 // DMA ready to go -- so do it.
philpem@53 223 size_t num = 0;
philpem@53 224 while (state.dma_count < 0x4000) {
philpem@53 225 uint16_t d = 0;
philpem@53 226
philpem@53 227 // num tells us how many words we've copied. If this is greater than the per-timeslot DMA maximum, bail out!
philpem@53 228 if (num > (1e6/TIMESLOT_FREQUENCY)) break;
philpem@53 229
philpem@53 230 // Evidently we have more words to copy. Copy them.
philpem@53 231 if (!wd2797_get_drq(&state.fdc_ctx)) {
philpem@53 232 // Bail out, no data available. Try again later.
philpem@53 233 // TODO: handle HDD controller too
philpem@53 234 break;
philpem@53 235 }
philpem@53 236
philpem@67 237 // Check memory access permissions
philpem@67 238 // TODO: enforce these!!!! use ACCESS_CHECK_* for guidance.
philpem@67 239 bool access_ok;
philpem@67 240 switch (checkMemoryAccess(state.dma_address, !state.dma_reading)) {
philpem@67 241 case MEM_PAGEFAULT:
philpem@69 242 // Page fault
philpem@69 243 state.genstat = 0x8BFF
philpem@69 244 | (state.dma_reading ? 0x4000 : 0)
philpem@69 245 | (state.pie ? 0x0400 : 0);
philpem@67 246 access_ok = false;
philpem@67 247 break;
philpem@69 248
philpem@69 249 case MEM_UIE:
philpem@69 250 // User access to memory above 4MB
philpem@69 251 // FIXME? Shouldn't be possible with DMA... assert this?
philpem@69 252 state.genstat = 0x9AFF
philpem@69 253 | (state.dma_reading ? 0x4000 : 0)
philpem@69 254 | (state.pie ? 0x0400 : 0);
philpem@69 255 access_ok = false;
philpem@69 256 break;
philpem@69 257
philpem@69 258 case MEM_KERNEL:
philpem@69 259 case MEM_PAGE_NO_WE:
philpem@69 260 // Kernel access or page not write enabled
philpem@69 261 access_ok = false;
philpem@69 262 break;
philpem@69 263
philpem@67 264 case MEM_ALLOWED:
philpem@67 265 access_ok = true;
philpem@67 266 break;
philpem@67 267 }
philpem@67 268 if (!access_ok) {
philpem@69 269 state.bsr0 = 0x3C00;
philpem@69 270 state.bsr0 |= (state.dma_address >> 16);
philpem@69 271 state.bsr1 = state.dma_address & 0xffff;
philpem@69 272 m68k_pulse_bus_error();
philpem@69 273 printf("BUS ERROR FROM DMA: genstat=%04X, bsr0=%04X, bsr1=%04X\n", state.genstat, state.bsr0, state.bsr1);
philpem@69 274
philpem@67 275 // TODO: FIXME: if we get a pagefault, it NEEDS to be tagged as 'peripheral sourced'... this is a HACK!
philpem@67 276 printf("REALLY BIG FSCKING HUGE ERROR: DMA Memory Access caused a FAULT!\n");
philpem@69 277 exit(-1);
philpem@67 278 }
philpem@67 279
philpem@67 280 // Map logical address to a physical RAM address
philpem@67 281 uint32_t newAddr = mapAddr(state.dma_address, !state.dma_reading);
philpem@67 282
philpem@53 283 if (!state.dma_reading) {
philpem@67 284 // Data available. Get it from the FDC. TODO: handle HDD too
philpem@53 285 d = wd2797_read_reg(&state.fdc_ctx, WD2797_REG_DATA);
philpem@53 286 d <<= 8;
philpem@53 287 d += wd2797_read_reg(&state.fdc_ctx, WD2797_REG_DATA);
philpem@53 288
philpem@67 289 if (newAddr <= 0x1FFFFF) {
philpem@67 290 WR16(state.base_ram, newAddr, state.base_ram_size - 1, d);
philpem@67 291 } else if (newAddr >= 0x200000) {
philpem@67 292 WR16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1, d);
philpem@67 293 }
philpem@57 294 m68k_write_memory_16(state.dma_address, d);
philpem@53 295 } else {
philpem@67 296 // Data write to FDC. TODO: handle HDD too.
philpem@53 297
philpem@67 298 // Get the data from RAM
philpem@67 299 if (newAddr <= 0x1fffff) {
philpem@67 300 d = RD16(state.base_ram, newAddr, state.base_ram_size - 1);
philpem@67 301 } else {
philpem@67 302 if (newAddr <= (state.exp_ram_size + 0x200000 - 1))
philpem@67 303 d = RD16(state.exp_ram, newAddr - 0x200000, state.exp_ram_size - 1);
philpem@67 304 else
philpem@67 305 d = 0xffff;
philpem@67 306 }
philpem@67 307
philpem@67 308 // Send the data to the FDD
philpem@53 309 wd2797_write_reg(&state.fdc_ctx, WD2797_REG_DATA, (d >> 8));
philpem@53 310 wd2797_write_reg(&state.fdc_ctx, WD2797_REG_DATA, (d & 0xff));
philpem@53 311 }
philpem@53 312
philpem@53 313 // Increment DMA address
philpem@57 314 state.dma_address+=2;
philpem@53 315 // Increment number of words transferred
philpem@53 316 num++; state.dma_count++;
philpem@53 317 }
philpem@53 318
philpem@53 319 // Turn off DMA engine if we finished this cycle
philpem@53 320 if (state.dma_count >= 0x4000) {
philpem@67 321 // FIXME? apparently this isn't required... or is it?
philpem@56 322 // state.dma_count = 0;
philpem@53 323 state.dmaen = false;
philpem@53 324 }
philpem@53 325 }
philpem@53 326
philpem@56 327 // Any interrupts? --> TODO: masking
philpem@78 328 /* if (!lastirq_fdc) {
philpem@76 329 if (wd2797_get_irq(&state.fdc_ctx)) {
philpem@76 330 lastirq_fdc = true;
philpem@76 331 m68k_set_irq(2);
philpem@76 332 } else {
philpem@76 333 lastirq_fdc = false;
philpem@76 334 }
philpem@53 335 } else {
philpem@87 336 lastirq_fdc = wd2797_get_irq(&state.fdc_ctx);
philpem@53 337 m68k_set_irq(0);
philpem@53 338 }
philpem@56 339 */
philpem@20 340 // Is it time to run the 60Hz periodic interrupt yet?
philpem@20 341 if (clock_cycles > CLOCKS_PER_60HZ) {
philpem@41 342 // Refresh the screen
philpem@41 343 refreshScreen(screen);
philpem@20 344 // TODO: trigger periodic interrupt (if enabled)
philpem@20 345 // decrement clock cycle counter, we've handled the intr.
philpem@20 346 clock_cycles -= CLOCKS_PER_60HZ;
philpem@16 347 }
philpem@16 348
philpem@47 349 // handle SDL events -- returns true if we need to exit
philpem@47 350 if (HandleSDLEvents(screen))
philpem@47 351 exitEmu = true;
philpem@47 352
philpem@20 353 // make sure frame rate is equal to real time
philpem@20 354 uint32_t now = SDL_GetTicks();
philpem@20 355 if (now < next_timeslot) {
philpem@20 356 // timeslot finished early -- eat up some time
philpem@20 357 SDL_Delay(next_timeslot - now);
philpem@20 358 } else {
philpem@20 359 // timeslot finished late -- skip ahead to gain time
philpem@20 360 // TODO: if this happens a lot, we should let the user know
philpem@20 361 // that their PC might not be fast enough...
philpem@20 362 next_timeslot = now;
philpem@20 363 }
philpem@20 364 // advance to the next timeslot
philpem@20 365 next_timeslot += MILLISECS_PER_TIMESLOT;
philpem@20 366
philpem@20 367 // if we've been asked to exit the emulator, then do so.
philpem@16 368 if (exitEmu) break;
philpem@16 369 }
philpem@7 370
philpem@52 371 // Release the disc image
philpem@52 372 wd2797_unload(&state.fdc_ctx);
philpem@52 373 fclose(disc);
philpem@52 374
philpem@0 375 return 0;
philpem@0 376 }