src/main.c

Sat, 17 Nov 2012 19:13:08 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Sat, 17 Nov 2012 19:13:08 +0000
changeset 111
4c85846b09cd
parent 107
566cfc70ef33
child 112
a392eb8f9806
permissions
-rw-r--r--

Improve floppy disc support

Patch-Author: Andrew Warkentin <andreww591!gmail>
Patch-Message-ID: <50A772FC.8020009@gmail.com>

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