| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#ifndef __APPLE__ |
| 7 |
#ifndef _MSC_VER |
| 8 |
#define _XOPEN_SOURCE 600 |
| 9 |
#define _POSIX_C_SOURCE 1 |
| 10 |
#endif |
| 11 |
#endif |
| 12 |
|
| 13 |
#include "video.h" |
| 14 |
#include "memory.h" |
| 15 |
#include "glue.h" |
| 16 |
#include "debugger.h" |
| 17 |
#ifdef HAS_IMGUI |
| 18 |
#include "smc.h" |
| 19 |
#include "dbg_load.h" |
| 20 |
#include "debug_ui/debug_ui.h" |
| 21 |
#endif |
| 22 |
#include "keyboard.h" |
| 23 |
#include "gif.h" |
| 24 |
#include "joystick.h" |
| 25 |
#include "vera_spi.h" |
| 26 |
#include "vera_psg.h" |
| 27 |
#include "vera_pcm.h" |
| 28 |
#include "sprite_trace.h" |
| 29 |
#include "vera_bandwidth.h" |
| 30 |
#include "icon.h" |
| 31 |
#include "sdcard.h" |
| 32 |
#include "i2c.h" |
| 33 |
#include "audio.h" |
| 34 |
|
| 35 |
#include <stdbool.h> |
| 36 |
#include <limits.h> |
| 37 |
#include <stdint.h> |
| 38 |
#include <time.h> |
| 39 |
#include "compat.h" |
| 40 |
|
| 41 |
#ifdef __EMSCRIPTEN__ |
| 42 |
#include "emscripten.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifndef __EMSCRIPTEN__ |
| 46 |
#define STB_IMAGE_WRITE_IMPLEMENTATION |
| 47 |
#include "stb_image_write.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#define APPROX_TITLEBAR_HEIGHT 30 |
| 51 |
|
| 52 |
#define VERA_VERSION_MAJOR 47 |
| 53 |
#define VERA_VERSION_MINOR 0 |
| 54 |
#define VERA_VERSION_PATCH 2 |
| 55 |
|
| 56 |
#define ADDR_VRAM_START 0x00000 |
| 57 |
#define ADDR_VRAM_END 0x20000 |
| 58 |
#define ADDR_PSG_START 0x1F9C0 |
| 59 |
#define ADDR_PSG_END 0x1FA00 |
| 60 |
#define ADDR_PALETTE_START 0x1FA00 |
| 61 |
#define ADDR_PALETTE_END 0x1FC00 |
| 62 |
#define ADDR_SPRDATA_START 0x1FC00 |
| 63 |
#define ADDR_SPRDATA_END 0x20000 |
| 64 |
|
| 65 |
#define NUM_SPRITES 128 |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#define SPRITE_RENDER_TIME 798 |
| 77 |
|
| 78 |
|
| 79 |
#define SCAN_HEIGHT 525 |
| 80 |
#define PIXEL_FREQ 25.0 |
| 81 |
|
| 82 |
|
| 83 |
#define VGA_SCAN_WIDTH 800 |
| 84 |
#define VGA_Y_OFFSET 0 |
| 85 |
|
| 86 |
|
| 87 |
#define NTSC_HALF_SCAN_WIDTH 794 |
| 88 |
#define NTSC_X_OFFSET 270 |
| 89 |
#define NTSC_Y_OFFSET_LOW 42 |
| 90 |
#define NTSC_Y_OFFSET_HIGH 568 |
| 91 |
#define TITLE_SAFE_X 0.067 |
| 92 |
#define TITLE_SAFE_Y 0.05 |
| 93 |
|
| 94 |
|
| 95 |
#define SCREEN_WIDTH 640 |
| 96 |
#define SCREEN_HEIGHT 480 |
| 97 |
|
| 98 |
#define SCREEN_RAM_OFFSET 0x00000 |
| 99 |
|
| 100 |
#ifdef __APPLE__ |
| 101 |
#define LSHORTCUT_KEY SDL_SCANCODE_LGUI |
| 102 |
#define RSHORTCUT_KEY SDL_SCANCODE_RGUI |
| 103 |
#else |
| 104 |
#define LSHORTCUT_KEY SDL_SCANCODE_LCTRL |
| 105 |
#define RSHORTCUT_KEY SDL_SCANCODE_RCTRL |
| 106 |
#endif |
| 107 |
|
| 108 |
|
| 109 |
#define LAYER_PIXELS_PER_ITERATION 8 |
| 110 |
|
| 111 |
#define MAX(a,b) ((a) > (b) ? a : b) |
| 112 |
|
| 113 |
static SDL_Window *window; |
| 114 |
static SDL_Renderer *renderer; |
| 115 |
static SDL_Texture *sdlTexture; |
| 116 |
static bool is_fullscreen = false; |
| 117 |
#ifdef HAS_IMGUI |
| 118 |
|
| 119 |
static SDL_Window *imgui_window = NULL; |
| 120 |
static SDL_Renderer *imgui_renderer = NULL; |
| 121 |
static Uint32 imgui_window_id = 0; |
| 122 |
#endif |
| 123 |
bool mouse_grabbed = false; |
| 124 |
bool no_keyboard_capture = false; |
| 125 |
bool kernal_mouse_enabled = false; |
| 126 |
|
| 127 |
static uint8_t video_ram[0x20000]; |
| 128 |
static uint8_t palette[256 * 2]; |
| 129 |
static uint8_t sprite_data[128][8]; |
| 130 |
|
| 131 |
|
| 132 |
static uint32_t io_addr[2]; |
| 133 |
static uint8_t io_rddata[2]; |
| 134 |
static uint8_t io_inc[2]; |
| 135 |
static uint8_t io_addrsel; |
| 136 |
static uint8_t io_dcsel; |
| 137 |
|
| 138 |
static uint8_t ien; |
| 139 |
static uint8_t isr; |
| 140 |
|
| 141 |
static uint16_t irq_line; |
| 142 |
|
| 143 |
static uint8_t reg_layer[2][7]; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
static uint8_t line_reg_layer[SCREEN_HEIGHT][2][7]; |
| 151 |
static uint16_t line_eff_y[SCREEN_HEIGHT]; |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static uint16_t line_layer_row[SCREEN_HEIGHT][2]; |
| 159 |
static uint8_t line_layer_enabled[SCREEN_HEIGHT]; |
| 160 |
static bool line_state_valid[SCREEN_HEIGHT]; |
| 161 |
|
| 162 |
|
| 163 |
static uint8_t prev_reg_layer[2][2][7]; |
| 164 |
|
| 165 |
#define COMPOSER_SLOTS 4*64 |
| 166 |
static uint8_t reg_composer[COMPOSER_SLOTS]; |
| 167 |
static uint8_t prev_reg_composer[2][COMPOSER_SLOTS]; |
| 168 |
|
| 169 |
static uint8_t layer_line[2][SCREEN_WIDTH]; |
| 170 |
static uint8_t sprite_line_col[SCREEN_WIDTH]; |
| 171 |
static uint8_t sprite_line_z[SCREEN_WIDTH]; |
| 172 |
static uint8_t sprite_line_mask[SCREEN_WIDTH]; |
| 173 |
static uint8_t sprite_line_collisions; |
| 174 |
static bool layer_line_enable[2]; |
| 175 |
static bool old_layer_line_enable[2]; |
| 176 |
static bool old_sprite_line_enable; |
| 177 |
static bool sprite_line_enable; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static uint8_t fx_addr1_mode; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
static uint32_t fx_x_pixel_increment; |
| 192 |
static uint32_t fx_y_pixel_increment; |
| 193 |
static uint32_t fx_x_pixel_position; |
| 194 |
static uint32_t fx_y_pixel_position; |
| 195 |
|
| 196 |
static uint16_t fx_poly_fill_length; |
| 197 |
|
| 198 |
static uint32_t fx_affine_tile_base; |
| 199 |
static uint32_t fx_affine_map_base; |
| 200 |
|
| 201 |
static uint8_t fx_affine_map_size; |
| 202 |
|
| 203 |
static bool fx_4bit_mode; |
| 204 |
static bool fx_16bit_hop; |
| 205 |
static bool fx_cache_byte_cycling; |
| 206 |
static bool fx_cache_fill; |
| 207 |
static bool fx_cache_write; |
| 208 |
static bool fx_trans_writes; |
| 209 |
|
| 210 |
static bool fx_2bit_poly; |
| 211 |
static bool fx_2bit_poking; |
| 212 |
|
| 213 |
static bool fx_cache_increment_mode; |
| 214 |
static bool fx_cache_nibble_index; |
| 215 |
static uint8_t fx_cache_byte_index; |
| 216 |
static bool fx_multiplier; |
| 217 |
static bool fx_subtract; |
| 218 |
|
| 219 |
static bool fx_affine_clip; |
| 220 |
|
| 221 |
static uint8_t fx_16bit_hop_align; |
| 222 |
|
| 223 |
static bool fx_nibble_bit[2]; |
| 224 |
static bool fx_nibble_incr[2]; |
| 225 |
|
| 226 |
static uint8_t fx_cache[4]; |
| 227 |
|
| 228 |
static int32_t fx_mult_accumulator; |
| 229 |
|
| 230 |
static const uint8_t vera_version_string[] = {'V', |
| 231 |
VERA_VERSION_MAJOR, |
| 232 |
VERA_VERSION_MINOR, |
| 233 |
VERA_VERSION_PATCH |
| 234 |
}; |
| 235 |
|
| 236 |
float vga_scan_pos_x; |
| 237 |
uint16_t vga_scan_pos_y; |
| 238 |
float ntsc_half_cnt; |
| 239 |
uint16_t ntsc_scan_pos_y; |
| 240 |
int frame_count = 0; |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static uint32_t sprite_trace_cpu_cycles = 0; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
static uint16_t |
| 253 |
sprite_trace_current_line(void) |
| 254 |
{ |
| 255 |
int y; |
| 256 |
if (reg_composer[0] & 2) { |
| 257 |
y = (ntsc_scan_pos_y < SCAN_HEIGHT) ? (int)ntsc_scan_pos_y - NTSC_Y_OFFSET_LOW |
| 258 |
: (int)ntsc_scan_pos_y - NTSC_Y_OFFSET_HIGH; |
| 259 |
} else { |
| 260 |
y = (int)vga_scan_pos_y - VGA_Y_OFFSET; |
| 261 |
} |
| 262 |
if (y < 0 || y >= SPRITE_TRACE_LINES) { |
| 263 |
return SPRITE_TRACE_NO_LINE; |
| 264 |
} |
| 265 |
return (uint16_t)y; |
| 266 |
} |
| 267 |
|
| 268 |
static uint8_t framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT * 4]; |
| 269 |
#ifndef __EMSCRIPTEN__ |
| 270 |
static uint8_t png_buffer[SCREEN_WIDTH * SCREEN_HEIGHT * 3]; |
| 271 |
#endif |
| 272 |
|
| 273 |
static GifWriter gif_writer; |
| 274 |
|
| 275 |
static const uint16_t default_palette[] = { |
| 276 |
0x000,0xfff,0x800,0xafe,0xc4c,0x0c5,0x00a,0xee7,0xd85,0x640,0xf77,0x333,0x777,0xaf6,0x08f,0xbbb,0x000,0x111,0x222,0x333,0x444,0x555,0x666,0x777,0x888,0x999,0xaaa,0xbbb,0xccc,0xddd,0xeee,0xfff,0x211,0x433,0x644,0x866,0xa88,0xc99,0xfbb,0x211,0x422,0x633,0x844,0xa55,0xc66,0xf77,0x200,0x411,0x611,0x822,0xa22,0xc33,0xf33,0x200,0x400,0x600,0x800,0xa00,0xc00,0xf00,0x221,0x443,0x664,0x886,0xaa8,0xcc9,0xfeb,0x211,0x432,0x653,0x874,0xa95,0xcb6,0xfd7,0x210,0x431,0x651,0x862,0xa82,0xca3,0xfc3,0x210,0x430,0x640,0x860,0xa80,0xc90,0xfb0,0x121,0x343,0x564,0x786,0x9a8,0xbc9,0xdfb,0x121,0x342,0x463,0x684,0x8a5,0x9c6,0xbf7,0x120,0x241,0x461,0x582,0x6a2,0x8c3,0x9f3,0x120,0x240,0x360,0x480,0x5a0,0x6c0,0x7f0,0x121,0x343,0x465,0x686,0x8a8,0x9ca,0xbfc,0x121,0x242,0x364,0x485,0x5a6,0x6c8,0x7f9,0x020,0x141,0x162,0x283,0x2a4,0x3c5,0x3f6,0x020,0x041,0x061,0x082,0x0a2,0x0c3,0x0f3,0x122,0x344,0x466,0x688,0x8aa,0x9cc,0xbff,0x122,0x244,0x366,0x488,0x5aa,0x6cc,0x7ff,0x022,0x144,0x166,0x288,0x2aa,0x3cc,0x3ff,0x022,0x044,0x066,0x088,0x0aa,0x0cc,0x0ff,0x112,0x334,0x456,0x668,0x88a,0x9ac,0xbcf,0x112,0x224,0x346,0x458,0x56a,0x68c,0x79f,0x002,0x114,0x126,0x238,0x24a,0x35c,0x36f,0x002,0x014,0x016,0x028,0x02a,0x03c,0x03f,0x112,0x334,0x546,0x768,0x98a,0xb9c,0xdbf,0x112,0x324,0x436,0x648,0x85a,0x96c,0xb7f,0x102,0x214,0x416,0x528,0x62a,0x83c,0x93f,0x102,0x204,0x306,0x408,0x50a,0x60c,0x70f,0x212,0x434,0x646,0x868,0xa8a,0xc9c,0xfbe,0x211,0x423,0x635,0x847,0xa59,0xc6b,0xf7d,0x201,0x413,0x615,0x826,0xa28,0xc3a,0xf3c,0x201,0x403,0x604,0x806,0xa08,0xc09,0xf0b |
| 277 |
}; |
| 278 |
|
| 279 |
uint8_t video_space_read(uint32_t address); |
| 280 |
static void video_space_read_range(uint8_t* dest, uint32_t address, uint32_t size); |
| 281 |
|
| 282 |
static void refresh_palette(); |
| 283 |
|
| 284 |
void |
| 285 |
mousegrab_toggle() { |
| 286 |
mouse_grabbed = !mouse_grabbed; |
| 287 |
SDL_SetWindowGrab(window, mouse_grabbed && !no_keyboard_capture); |
| 288 |
SDL_SetRelativeMouseMode(mouse_grabbed); |
| 289 |
SDL_ShowCursor((mouse_grabbed || kernal_mouse_enabled) ? SDL_DISABLE : SDL_ENABLE); |
| 290 |
sprintf(window_title, WINDOW_TITLE "%s", mouse_grabbed ? MOUSE_GRAB_MSG : ""); |
| 291 |
video_update_title(window_title); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
static void video_reset_layer_pipeline(void); |
| 301 |
|
| 302 |
void |
| 303 |
video_reset() |
| 304 |
{ |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
memset(line_state_valid, 0, sizeof(line_state_valid)); |
| 310 |
|
| 311 |
|
| 312 |
memset(io_addr, 0, sizeof(io_addr)); |
| 313 |
memset(io_inc, 0, sizeof(io_inc)); |
| 314 |
io_addrsel = 0; |
| 315 |
io_dcsel = 0; |
| 316 |
io_rddata[0] = 0; |
| 317 |
io_rddata[1] = 0; |
| 318 |
|
| 319 |
ien = 0; |
| 320 |
isr = 0; |
| 321 |
irq_line = 0; |
| 322 |
|
| 323 |
|
| 324 |
memset(reg_layer, 0, sizeof(reg_layer)); |
| 325 |
video_reset_layer_pipeline(); |
| 326 |
|
| 327 |
|
| 328 |
memset(reg_composer, 0, sizeof(reg_composer)); |
| 329 |
reg_composer[1] = 128; |
| 330 |
reg_composer[2] = 128; |
| 331 |
reg_composer[5] = 640 >> 2; |
| 332 |
reg_composer[7] = 480 >> 1; |
| 333 |
|
| 334 |
|
| 335 |
fx_addr1_mode = 0; |
| 336 |
fx_x_pixel_position = 0x8000; |
| 337 |
fx_y_pixel_position = 0x8000; |
| 338 |
fx_x_pixel_increment = 0; |
| 339 |
fx_y_pixel_increment = 0; |
| 340 |
|
| 341 |
fx_cache_write = false; |
| 342 |
fx_cache_fill = false; |
| 343 |
fx_4bit_mode = false; |
| 344 |
fx_16bit_hop = false; |
| 345 |
fx_subtract = false; |
| 346 |
fx_cache_byte_cycling = false; |
| 347 |
fx_trans_writes = false; |
| 348 |
fx_multiplier = false; |
| 349 |
|
| 350 |
fx_mult_accumulator = 0; |
| 351 |
|
| 352 |
fx_2bit_poly = false; |
| 353 |
fx_2bit_poking = false; |
| 354 |
|
| 355 |
fx_cache_nibble_index = 0; |
| 356 |
fx_cache_byte_index = 0; |
| 357 |
fx_cache_increment_mode = 0; |
| 358 |
|
| 359 |
fx_cache[0] = 0; |
| 360 |
fx_cache[1] = 0; |
| 361 |
fx_cache[2] = 0; |
| 362 |
fx_cache[3] = 0; |
| 363 |
|
| 364 |
fx_16bit_hop_align = 0; |
| 365 |
|
| 366 |
fx_nibble_bit[0] = false; |
| 367 |
fx_nibble_bit[1] = false; |
| 368 |
fx_nibble_incr[0] = false; |
| 369 |
fx_nibble_incr[1] = false; |
| 370 |
|
| 371 |
fx_poly_fill_length = 0; |
| 372 |
fx_affine_tile_base = 0; |
| 373 |
fx_affine_map_base = 0; |
| 374 |
fx_affine_map_size = 2; |
| 375 |
fx_affine_clip = false; |
| 376 |
|
| 377 |
|
| 378 |
memset(sprite_data, 0, sizeof(sprite_data)); |
| 379 |
|
| 380 |
|
| 381 |
memcpy(palette, default_palette, sizeof(palette)); |
| 382 |
for (int i = 0; i < 256; i++) { |
| 383 |
palette[i * 2 + 0] = default_palette[i] & 0xff; |
| 384 |
palette[i * 2 + 1] = default_palette[i] >> 8; |
| 385 |
} |
| 386 |
|
| 387 |
refresh_palette(); |
| 388 |
|
| 389 |
|
| 390 |
for (int i = 0; i < 128 * 1024; i++) { |
| 391 |
video_ram[i] = rand(); |
| 392 |
} |
| 393 |
|
| 394 |
sprite_line_collisions = 0; |
| 395 |
|
| 396 |
vga_scan_pos_x = 0; |
| 397 |
vga_scan_pos_y = 0; |
| 398 |
ntsc_half_cnt = 0; |
| 399 |
ntsc_scan_pos_y = 0; |
| 400 |
|
| 401 |
psg_reset(); |
| 402 |
pcm_reset(); |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static int SDLCALL |
| 413 |
video_move_resize_watch(void *userdata, SDL_Event *event) |
| 414 |
{ |
| 415 |
(void)userdata; |
| 416 |
static bool in_watch = false; |
| 417 |
if (in_watch) { |
| 418 |
return 1; |
| 419 |
} |
| 420 |
if (event->type == SDL_WINDOWEVENT && window && renderer && sdlTexture && |
| 421 |
event->window.windowID == SDL_GetWindowID(window)) { |
| 422 |
switch (event->window.event) { |
| 423 |
case SDL_WINDOWEVENT_MOVED: |
| 424 |
case SDL_WINDOWEVENT_RESIZED: |
| 425 |
case SDL_WINDOWEVENT_SIZE_CHANGED: |
| 426 |
case SDL_WINDOWEVENT_EXPOSED: |
| 427 |
in_watch = true; |
| 428 |
SDL_RenderClear(renderer); |
| 429 |
SDL_RenderCopy(renderer, sdlTexture, NULL, NULL); |
| 430 |
SDL_RenderPresent(renderer); |
| 431 |
in_watch = false; |
| 432 |
break; |
| 433 |
} |
| 434 |
} |
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
bool |
| 439 |
video_init(int window_scale, float screen_x_scale, char *quality, bool fullscreen, float opacity) |
| 440 |
{ |
| 441 |
uint32_t window_flags = SDL_WINDOW_ALLOW_HIGHDPI; |
| 442 |
|
| 443 |
#ifdef __EMSCRIPTEN__ |
| 444 |
|
| 445 |
window_flags &= ~SDL_WINDOW_ALLOW_HIGHDPI; |
| 446 |
#endif |
| 447 |
|
| 448 |
video_reset(); |
| 449 |
|
| 450 |
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, quality); |
| 451 |
SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1"); |
| 452 |
SDL_CreateWindowAndRenderer(SCREEN_WIDTH * window_scale * screen_x_scale, SCREEN_HEIGHT * window_scale, window_flags, &window, &renderer); |
| 453 |
#ifndef __MORPHOS__ |
| 454 |
SDL_SetWindowResizable(window, true); |
| 455 |
#endif |
| 456 |
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH * screen_x_scale, SCREEN_HEIGHT); |
| 457 |
|
| 458 |
sdlTexture = SDL_CreateTexture(renderer, |
| 459 |
SDL_PIXELFORMAT_RGB888, |
| 460 |
SDL_TEXTUREACCESS_STREAMING, |
| 461 |
SCREEN_WIDTH, SCREEN_HEIGHT); |
| 462 |
|
| 463 |
|
| 464 |
SDL_AddEventWatch(video_move_resize_watch, NULL); |
| 465 |
|
| 466 |
SDL_SetWindowTitle(window, WINDOW_TITLE); |
| 467 |
SDL_SetWindowIcon(window, CommanderX16Icon()); |
| 468 |
if (fullscreen) { |
| 469 |
is_fullscreen = true; |
| 470 |
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN); |
| 471 |
} else { |
| 472 |
int winX, winY; |
| 473 |
SDL_GetWindowPosition(window, &winX, &winY); |
| 474 |
if (winX < 0 || winY < APPROX_TITLEBAR_HEIGHT) { |
| 475 |
winX = winX < 0 ? 0 : winX; |
| 476 |
winY = winY < APPROX_TITLEBAR_HEIGHT ? APPROX_TITLEBAR_HEIGHT : winY; |
| 477 |
SDL_SetWindowPosition(window, winX, winY); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
SDL_SetWindowOpacity(window, opacity); |
| 482 |
|
| 483 |
#ifdef _WIN32 |
| 484 |
extern void video_win32_set_rounded_corners(SDL_Window *window); |
| 485 |
video_win32_set_rounded_corners(window); |
| 486 |
|
| 487 |
|
| 488 |
extern void video_win32_install_move_hook(SDL_Window *window); |
| 489 |
video_win32_install_move_hook(window); |
| 490 |
#endif |
| 491 |
|
| 492 |
if (record_gif != RECORD_GIF_DISABLED) { |
| 493 |
if (!strcmp(gif_path+strlen(gif_path)-5, ",wait")) { |
| 494 |
|
| 495 |
record_gif = RECORD_GIF_PAUSED; |
| 496 |
|
| 497 |
gif_path[strlen(gif_path)-5] = 0; |
| 498 |
} else { |
| 499 |
|
| 500 |
record_gif = RECORD_GIF_ACTIVE; |
| 501 |
} |
| 502 |
if (!GifBegin(&gif_writer, gif_path, SCREEN_WIDTH, SCREEN_HEIGHT, 1, 8, false)) { |
| 503 |
record_gif = RECORD_GIF_DISABLED; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
if (debugger_enabled) { |
| 508 |
DEBUGInitUI(renderer); |
| 509 |
} |
| 510 |
|
| 511 |
#ifdef HAS_IMGUI |
| 512 |
|
| 513 |
if (imgui_debugger_enabled) { |
| 514 |
int mainX, mainY, mainW; |
| 515 |
SDL_GetWindowPosition(window, &mainX, &mainY); |
| 516 |
SDL_GetWindowSize(window, &mainW, NULL); |
| 517 |
|
| 518 |
imgui_window = SDL_CreateWindow("Commander X16 - ImGui Debugger", |
| 519 |
mainX + mainW + 4, mainY, |
| 520 |
960, 720, |
| 521 |
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); |
| 522 |
if (imgui_window) { |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
imgui_renderer = SDL_CreateRenderer(imgui_window, -1, SDL_RENDERER_ACCELERATED); |
| 531 |
} |
| 532 |
if (imgui_window && imgui_renderer) { |
| 533 |
imgui_window_id = SDL_GetWindowID(imgui_window); |
| 534 |
debug_ui_init(imgui_window, imgui_renderer); |
| 535 |
#ifdef _WIN32 |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
video_win32_install_move_hook(imgui_window); |
| 543 |
#endif |
| 544 |
} else { |
| 545 |
printf("Warning: Failed to create debugger window: %s\n", SDL_GetError()); |
| 546 |
if (imgui_renderer) { SDL_DestroyRenderer(imgui_renderer); imgui_renderer = NULL; } |
| 547 |
if (imgui_window) { SDL_DestroyWindow(imgui_window); imgui_window = NULL; } |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
imgui_debugger_enabled = false; |
| 553 |
printf("The graphical debugger is disabled for this session.\n"); |
| 554 |
|
| 555 |
|
| 556 |
dbg_load_note_debugger(debugger_enabled); |
| 557 |
} |
| 558 |
} |
| 559 |
#endif |
| 560 |
|
| 561 |
if (grab_mouse && !mouse_grabbed) |
| 562 |
mousegrab_toggle(); |
| 563 |
|
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
struct video_layer_properties |
| 568 |
{ |
| 569 |
uint8_t color_depth; |
| 570 |
uint32_t map_base; |
| 571 |
uint32_t tile_base; |
| 572 |
|
| 573 |
bool text_mode; |
| 574 |
bool text_mode_256c; |
| 575 |
bool tile_mode; |
| 576 |
bool bitmap_mode; |
| 577 |
|
| 578 |
uint16_t hscroll; |
| 579 |
uint16_t vscroll; |
| 580 |
|
| 581 |
uint8_t mapw_log2; |
| 582 |
uint8_t maph_log2; |
| 583 |
uint16_t tilew; |
| 584 |
uint16_t tileh; |
| 585 |
uint8_t tilew_log2; |
| 586 |
uint8_t tileh_log2; |
| 587 |
|
| 588 |
uint16_t mapw_max; |
| 589 |
uint16_t maph_max; |
| 590 |
uint16_t tilew_max; |
| 591 |
uint16_t tileh_max; |
| 592 |
uint16_t layerw_max; |
| 593 |
uint16_t layerh_max; |
| 594 |
|
| 595 |
uint8_t tile_size_log2; |
| 596 |
|
| 597 |
int min_eff_x; |
| 598 |
int max_eff_x; |
| 599 |
|
| 600 |
uint8_t bits_per_pixel; |
| 601 |
uint8_t first_color_pos; |
| 602 |
uint8_t color_mask; |
| 603 |
uint8_t color_fields_max; |
| 604 |
}; |
| 605 |
|
| 606 |
#define NUM_LAYERS 2 |
| 607 |
struct video_layer_properties layer_properties[NUM_LAYERS]; |
| 608 |
struct video_layer_properties prev_layer_properties[2][NUM_LAYERS]; |
| 609 |
|
| 610 |
static int |
| 611 |
calc_layer_eff_x(const struct video_layer_properties *props, const int x) |
| 612 |
{ |
| 613 |
return (x + props->hscroll) & (props->layerw_max); |
| 614 |
} |
| 615 |
|
| 616 |
static int |
| 617 |
calc_layer_eff_y(const struct video_layer_properties *props, const int y) |
| 618 |
{ |
| 619 |
return (y + props->vscroll) & (props->layerh_max); |
| 620 |
} |
| 621 |
|
| 622 |
static uint32_t |
| 623 |
calc_layer_map_addr_base2(const struct video_layer_properties *props, const int eff_x, const int eff_y) |
| 624 |
{ |
| 625 |
|
| 626 |
return props->map_base + ((((eff_y >> props->tileh_log2) << props->mapw_log2) + (eff_x >> props->tilew_log2)) << 1); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
static void |
| 636 |
refresh_layer_properties(const uint8_t layer) |
| 637 |
{ |
| 638 |
struct video_layer_properties* props = &layer_properties[layer]; |
| 639 |
|
| 640 |
uint16_t prev_layerw_max = props->layerw_max; |
| 641 |
uint16_t prev_hscroll = props->hscroll; |
| 642 |
|
| 643 |
props->color_depth = reg_layer[layer][0] & 0x3; |
| 644 |
props->map_base = reg_layer[layer][1] << 9; |
| 645 |
props->tile_base = (reg_layer[layer][2] & 0xFC) << 9; |
| 646 |
props->bitmap_mode = (reg_layer[layer][0] & 0x4) != 0; |
| 647 |
props->text_mode = (props->color_depth == 0) && !props->bitmap_mode; |
| 648 |
props->text_mode_256c = (reg_layer[layer][0] & 8) != 0; |
| 649 |
props->tile_mode = !props->bitmap_mode && !props->text_mode; |
| 650 |
|
| 651 |
if (!props->bitmap_mode) { |
| 652 |
props->hscroll = reg_layer[layer][3] | (reg_layer[layer][4] & 0xf) << 8; |
| 653 |
props->vscroll = reg_layer[layer][5] | (reg_layer[layer][6] & 0xf) << 8; |
| 654 |
} else { |
| 655 |
props->hscroll = 0; |
| 656 |
props->vscroll = 0; |
| 657 |
} |
| 658 |
|
| 659 |
uint16_t mapw = 0; |
| 660 |
uint16_t maph = 0; |
| 661 |
props->tilew = 0; |
| 662 |
props->tileh = 0; |
| 663 |
|
| 664 |
if (props->tile_mode || props->text_mode) { |
| 665 |
props->mapw_log2 = 5 + ((reg_layer[layer][0] >> 4) & 3); |
| 666 |
props->maph_log2 = 5 + ((reg_layer[layer][0] >> 6) & 3); |
| 667 |
mapw = 1 << props->mapw_log2; |
| 668 |
maph = 1 << props->maph_log2; |
| 669 |
|
| 670 |
|
| 671 |
props->tilew_log2 = 3 + (reg_layer[layer][2] & 1); |
| 672 |
props->tileh_log2 = 3 + ((reg_layer[layer][2] >> 1) & 1); |
| 673 |
props->tilew = 1 << props->tilew_log2; |
| 674 |
props->tileh = 1 << props->tileh_log2; |
| 675 |
} else if (props->bitmap_mode) { |
| 676 |
|
| 677 |
props->tilew = (reg_layer[layer][2] & 1) ? 640 : 320; |
| 678 |
props->tileh = SCREEN_HEIGHT; |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
props->mapw_max = mapw - 1; |
| 685 |
props->maph_max = maph - 1; |
| 686 |
props->tilew_max = props->tilew - 1; |
| 687 |
props->tileh_max = props->tileh - 1; |
| 688 |
props->layerw_max = (mapw * props->tilew) - 1; |
| 689 |
props->layerh_max = (maph * props->tileh) - 1; |
| 690 |
|
| 691 |
|
| 692 |
if (prev_layerw_max != props->layerw_max || prev_hscroll != props->hscroll) { |
| 693 |
int min_eff_x = INT_MAX; |
| 694 |
int max_eff_x = INT_MIN; |
| 695 |
for (int x = 0; x < SCREEN_WIDTH; ++x) { |
| 696 |
int eff_x = calc_layer_eff_x(props, x); |
| 697 |
if (eff_x < min_eff_x) { |
| 698 |
min_eff_x = eff_x; |
| 699 |
} |
| 700 |
if (eff_x > max_eff_x) { |
| 701 |
max_eff_x = eff_x; |
| 702 |
} |
| 703 |
} |
| 704 |
props->min_eff_x = min_eff_x; |
| 705 |
props->max_eff_x = max_eff_x; |
| 706 |
} |
| 707 |
|
| 708 |
props->bits_per_pixel = 1 << props->color_depth; |
| 709 |
props->tile_size_log2 = props->tilew_log2 + props->tileh_log2 + props->color_depth - 3; |
| 710 |
|
| 711 |
props->first_color_pos = 8 - props->bits_per_pixel; |
| 712 |
props->color_mask = (1 << props->bits_per_pixel) - 1; |
| 713 |
props->color_fields_max = (8 >> props->color_depth) - 1; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
static void |
| 718 |
video_reset_layer_pipeline(void) |
| 719 |
{ |
| 720 |
refresh_layer_properties(0); |
| 721 |
refresh_layer_properties(1); |
| 722 |
memcpy(prev_layer_properties[0], layer_properties, sizeof(*layer_properties) * NUM_LAYERS); |
| 723 |
memcpy(prev_layer_properties[1], layer_properties, sizeof(*layer_properties) * NUM_LAYERS); |
| 724 |
memset(prev_reg_layer, 0, sizeof(prev_reg_layer)); |
| 725 |
} |
| 726 |
|
| 727 |
struct video_sprite_properties |
| 728 |
{ |
| 729 |
int8_t sprite_zdepth; |
| 730 |
uint8_t sprite_collision_mask; |
| 731 |
|
| 732 |
int16_t sprite_x; |
| 733 |
int16_t sprite_y; |
| 734 |
uint8_t sprite_width_log2; |
| 735 |
uint8_t sprite_height_log2; |
| 736 |
uint8_t sprite_width; |
| 737 |
uint8_t sprite_height; |
| 738 |
|
| 739 |
bool hflip; |
| 740 |
bool vflip; |
| 741 |
|
| 742 |
uint8_t color_mode; |
| 743 |
uint32_t sprite_address; |
| 744 |
|
| 745 |
uint16_t palette_offset; |
| 746 |
}; |
| 747 |
|
| 748 |
#ifndef __EMSCRIPTEN__ |
| 749 |
static void |
| 750 |
screenshot(void) |
| 751 |
{ |
| 752 |
char path[PATH_MAX]; |
| 753 |
const time_t now = time(NULL); |
| 754 |
strftime(path, PATH_MAX, "x16emu-%Y-%m-%d-%H-%M-%S.png", localtime(&now)); |
| 755 |
|
| 756 |
memset(png_buffer, 0, SCREEN_WIDTH * SCREEN_HEIGHT * 3); |
| 757 |
|
| 758 |
|
| 759 |
for (int i = 0; i < SCREEN_WIDTH * SCREEN_HEIGHT; i++) { |
| 760 |
png_buffer[(i*3)+0] = framebuffer[(i*4)+2]; |
| 761 |
png_buffer[(i*3)+1] = framebuffer[(i*4)+1]; |
| 762 |
png_buffer[(i*3)+2] = framebuffer[(i*4)+0]; |
| 763 |
} |
| 764 |
|
| 765 |
if (stbi_write_png(path, SCREEN_WIDTH, SCREEN_HEIGHT, 3, png_buffer, SCREEN_WIDTH*3)) { |
| 766 |
printf("Wrote screenshot to %s\n", path); |
| 767 |
} else { |
| 768 |
printf("WARNING: Couldn't write screenshot to %s\n", path); |
| 769 |
} |
| 770 |
} |
| 771 |
#endif |
| 772 |
|
| 773 |
struct video_sprite_properties sprite_properties[128]; |
| 774 |
|
| 775 |
static void |
| 776 |
refresh_sprite_properties(const uint16_t sprite) |
| 777 |
{ |
| 778 |
struct video_sprite_properties* props = &sprite_properties[sprite]; |
| 779 |
|
| 780 |
props->sprite_zdepth = (sprite_data[sprite][6] >> 2) & 3; |
| 781 |
props->sprite_collision_mask = sprite_data[sprite][6] & 0xf0; |
| 782 |
|
| 783 |
props->sprite_x = sprite_data[sprite][2] | (sprite_data[sprite][3] & 3) << 8; |
| 784 |
props->sprite_y = sprite_data[sprite][4] | (sprite_data[sprite][5] & 3) << 8; |
| 785 |
props->sprite_width_log2 = (((sprite_data[sprite][7] >> 4) & 3) + 3); |
| 786 |
props->sprite_height_log2 = ((sprite_data[sprite][7] >> 6) + 3); |
| 787 |
props->sprite_width = 1 << props->sprite_width_log2; |
| 788 |
props->sprite_height = 1 << props->sprite_height_log2; |
| 789 |
|
| 790 |
|
| 791 |
if (props->sprite_x >= 0x400 - props->sprite_width) { |
| 792 |
props->sprite_x -= 0x400; |
| 793 |
} |
| 794 |
if (props->sprite_y >= 0x400 - props->sprite_height) { |
| 795 |
props->sprite_y -= 0x400; |
| 796 |
} |
| 797 |
|
| 798 |
props->hflip = sprite_data[sprite][6] & 1; |
| 799 |
props->vflip = (sprite_data[sprite][6] >> 1) & 1; |
| 800 |
|
| 801 |
props->color_mode = (sprite_data[sprite][1] >> 7) & 1; |
| 802 |
props->sprite_address = sprite_data[sprite][0] << 5 | (sprite_data[sprite][1] & 0xf) << 13; |
| 803 |
|
| 804 |
props->palette_offset = (sprite_data[sprite][7] & 0x0f) << 4; |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
static void |
| 811 |
sprite_attr_write(uint32_t address, uint8_t value) |
| 812 |
{ |
| 813 |
const uint8_t slot = (address >> 3) & 0x7f; |
| 814 |
const uint8_t offset = address & 0x7; |
| 815 |
if (sprite_trace_active) { |
| 816 |
sprite_trace_note_write(slot, offset, sprite_data[slot][offset], value, |
| 817 |
sprite_trace_current_line(), sprite_trace_cpu_cycles); |
| 818 |
} |
| 819 |
sprite_data[slot][offset] = value; |
| 820 |
refresh_sprite_properties(slot); |
| 821 |
} |
| 822 |
|
| 823 |
struct video_palette |
| 824 |
{ |
| 825 |
uint32_t entries[256]; |
| 826 |
bool dirty; |
| 827 |
}; |
| 828 |
|
| 829 |
struct video_palette video_palette; |
| 830 |
|
| 831 |
static void |
| 832 |
refresh_palette() { |
| 833 |
const uint8_t out_mode = reg_composer[0] & 3; |
| 834 |
const bool chroma_disable = ((reg_composer[0] & 0x07) == 6); |
| 835 |
for (int i = 0; i < 256; ++i) { |
| 836 |
uint8_t r; |
| 837 |
uint8_t g; |
| 838 |
uint8_t b; |
| 839 |
if (out_mode == 0) { |
| 840 |
|
| 841 |
|
| 842 |
r = 0; |
| 843 |
g = 0; |
| 844 |
b = 255; |
| 845 |
} else { |
| 846 |
uint16_t entry = palette[i * 2] | palette[i * 2 + 1] << 8; |
| 847 |
r = ((entry >> 8) & 0xf) << 4 | ((entry >> 8) & 0xf); |
| 848 |
g = ((entry >> 4) & 0xf) << 4 | ((entry >> 4) & 0xf); |
| 849 |
b = (entry & 0xf) << 4 | (entry & 0xf); |
| 850 |
if (chroma_disable) { |
| 851 |
r = g = b = (r + b + g) / 3; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
video_palette.entries[i] = (uint32_t)(r << 16) | ((uint32_t)g << 8) | ((uint32_t)b); |
| 856 |
} |
| 857 |
video_palette.dirty = false; |
| 858 |
} |
| 859 |
|
| 860 |
static void |
| 861 |
expand_4bpp_data(uint8_t *dst, const uint8_t *src, int dst_size) |
| 862 |
{ |
| 863 |
while (dst_size >= 2) { |
| 864 |
*dst = (*src) >> 4; |
| 865 |
++dst; |
| 866 |
*dst = (*src) & 0xf; |
| 867 |
++dst; |
| 868 |
|
| 869 |
++src; |
| 870 |
dst_size -= 2; |
| 871 |
} |
| 872 |
} |
| 873 |
|
| 874 |
static void |
| 875 |
render_sprite_line(const uint16_t y) |
| 876 |
{ |
| 877 |
memset(sprite_line_col, 0, SCREEN_WIDTH); |
| 878 |
memset(sprite_line_z, 0, SCREEN_WIDTH); |
| 879 |
memset(sprite_line_mask, 0, SCREEN_WIDTH); |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
const bool trace = sprite_trace_active; |
| 906 |
uint16_t traced_evaluated = 0; |
| 907 |
uint16_t traced_drawn = 0; |
| 908 |
uint16_t traced_cut_slot = SPRITE_TRACE_NO_LINE; |
| 909 |
uint16_t traced_dropped = 0; |
| 910 |
uint32_t traced_tail = 0; |
| 911 |
if (trace) { |
| 912 |
sprite_trace_line_begin(y, sprite_trace_cpu_cycles); |
| 913 |
} |
| 914 |
|
| 915 |
uint32_t t = 0; |
| 916 |
uint32_t render_free_at = 0; |
| 917 |
int stopped_at = NUM_SPRITES; |
| 918 |
|
| 919 |
for (int i = 0; i < NUM_SPRITES; i++) { |
| 920 |
if (t >= SPRITE_RENDER_TIME) { |
| 921 |
|
| 922 |
stopped_at = i; |
| 923 |
break; |
| 924 |
} |
| 925 |
|
| 926 |
const uint32_t t_before = t; |
| 927 |
uint8_t tflags = SPRITE_TRACE_EVALUATED; |
| 928 |
const struct video_sprite_properties *props = &sprite_properties[i]; |
| 929 |
|
| 930 |
const bool on_line = props->sprite_zdepth != 0 && y >= props->sprite_y && |
| 931 |
y < props->sprite_y + props->sprite_height; |
| 932 |
|
| 933 |
if (!on_line) { |
| 934 |
t++; |
| 935 |
if (trace) { |
| 936 |
traced_evaluated++; |
| 937 |
sprite_trace_note_slot(y, (uint8_t)i, sprite_data[i], |
| 938 |
(uint16_t)(t - t_before), tflags); |
| 939 |
} |
| 940 |
continue; |
| 941 |
} |
| 942 |
|
| 943 |
tflags |= SPRITE_TRACE_ONSCREEN; |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
if (t < render_free_at) { |
| 948 |
t = render_free_at; |
| 949 |
} |
| 950 |
if (t >= SPRITE_RENDER_TIME) { |
| 951 |
stopped_at = i; |
| 952 |
break; |
| 953 |
} |
| 954 |
t += 2; |
| 955 |
|
| 956 |
const uint16_t eff_sy = props->vflip ? ((props->sprite_height - 1) - (y - props->sprite_y)) : (y - props->sprite_y); |
| 957 |
|
| 958 |
int16_t eff_sx = (props->hflip ? (props->sprite_width - 1) : 0); |
| 959 |
const int16_t eff_sx_incr = props->hflip ? -1 : 1; |
| 960 |
|
| 961 |
const uint8_t *bitmap_data = video_ram + props->sprite_address + (eff_sy << (props->sprite_width_log2 - (1 - props->color_mode))); |
| 962 |
|
| 963 |
uint8_t unpacked_sprite_line[64]; |
| 964 |
const uint16_t width = (props->sprite_width<64? props->sprite_width : 64); |
| 965 |
const uint8_t vram_fetch_mask = ((2 - props->color_mode) << 2) - 1; |
| 966 |
if (props->color_mode == 0) { |
| 967 |
|
| 968 |
expand_4bpp_data(unpacked_sprite_line, bitmap_data, width); |
| 969 |
} else { |
| 970 |
|
| 971 |
memcpy(unpacked_sprite_line, bitmap_data, width); |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
uint32_t rt = t + 1; |
| 977 |
bool truncated = false; |
| 978 |
uint16_t sx = 0; |
| 979 |
|
| 980 |
for (; sx < props->sprite_width; ++sx) { |
| 981 |
|
| 982 |
if (!(sx & vram_fetch_mask)) { |
| 983 |
if (rt >= SPRITE_RENDER_TIME) { truncated = true; break; } |
| 984 |
rt++; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
if (rt >= SPRITE_RENDER_TIME) { truncated = true; break; } |
| 992 |
rt++; |
| 993 |
|
| 994 |
const uint16_t line_x = props->sprite_x + sx; |
| 995 |
if (line_x >= SCREEN_WIDTH) { |
| 996 |
eff_sx += eff_sx_incr; |
| 997 |
continue; |
| 998 |
} |
| 999 |
|
| 1000 |
uint8_t col_index = unpacked_sprite_line[eff_sx]; |
| 1001 |
eff_sx += eff_sx_incr; |
| 1002 |
|
| 1003 |
|
| 1004 |
if (col_index > 0) { |
| 1005 |
sprite_line_collisions |= sprite_line_mask[line_x] & props->sprite_collision_mask; |
| 1006 |
sprite_line_mask[line_x] |= props->sprite_collision_mask; |
| 1007 |
|
| 1008 |
if (props->sprite_zdepth > sprite_line_z[line_x]) { |
| 1009 |
if (col_index < 16) { |
| 1010 |
col_index += props->palette_offset; |
| 1011 |
} |
| 1012 |
sprite_line_col[line_x] = col_index; |
| 1013 |
sprite_line_z[line_x] = props->sprite_zdepth; |
| 1014 |
tflags |= SPRITE_TRACE_DREW; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
render_free_at = rt; |
| 1020 |
|
| 1021 |
if (trace) { |
| 1022 |
traced_evaluated++; |
| 1023 |
if (tflags & SPRITE_TRACE_DREW) { |
| 1024 |
traced_drawn++; |
| 1025 |
} |
| 1026 |
if (truncated) { |
| 1027 |
tflags |= SPRITE_TRACE_CUT; |
| 1028 |
if (traced_cut_slot == SPRITE_TRACE_NO_LINE) { |
| 1029 |
traced_cut_slot = (uint16_t)i; |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
if (!(tflags & SPRITE_TRACE_DREW)) { |
| 1035 |
traced_dropped++; |
| 1036 |
} |
| 1037 |
const uint32_t left = (uint32_t)(props->sprite_width - sx); |
| 1038 |
traced_tail = left + left / (vram_fetch_mask + 1u); |
| 1039 |
} |
| 1040 |
sprite_trace_note_slot(y, (uint8_t)i, sprite_data[i], |
| 1041 |
(uint16_t)(rt - t_before), tflags); |
| 1042 |
} |
| 1043 |
|
| 1044 |
if (truncated) { |
| 1045 |
|
| 1046 |
stopped_at = i + 1; |
| 1047 |
break; |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
if (!trace) { |
| 1052 |
return; |
| 1053 |
} |
| 1054 |
|
| 1055 |
uint32_t used = (t > render_free_at ? t : render_free_at); |
| 1056 |
if (used > SPRITE_RENDER_TIME) { |
| 1057 |
used = SPRITE_RENDER_TIME; |
| 1058 |
} |
| 1059 |
uint32_t demand = used + traced_tail; |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
for (int i = stopped_at; i < NUM_SPRITES; i++) { |
| 1066 |
const struct video_sprite_properties *p = &sprite_properties[i]; |
| 1067 |
const bool on_line = p->sprite_zdepth != 0 && y >= p->sprite_y && |
| 1068 |
y < p->sprite_y + p->sprite_height; |
| 1069 |
if (!on_line) { |
| 1070 |
demand++; |
| 1071 |
sprite_trace_note_slot(y, (uint8_t)i, sprite_data[i], 0, SPRITE_TRACE_EVALUATED); |
| 1072 |
continue; |
| 1073 |
} |
| 1074 |
const uint16_t per_fetch = (uint16_t)((2 - p->color_mode) << 2); |
| 1075 |
demand += 3u + p->sprite_width + (uint32_t)(p->sprite_width / per_fetch); |
| 1076 |
traced_dropped++; |
| 1077 |
if (traced_cut_slot == SPRITE_TRACE_NO_LINE) { |
| 1078 |
traced_cut_slot = (uint16_t)i; |
| 1079 |
} |
| 1080 |
sprite_trace_note_slot(y, (uint8_t)i, sprite_data[i], 0, |
| 1081 |
(uint8_t)(SPRITE_TRACE_EVALUATED | SPRITE_TRACE_ONSCREEN | |
| 1082 |
SPRITE_TRACE_CUT)); |
| 1083 |
} |
| 1084 |
|
| 1085 |
sprite_line_stat_t st; |
| 1086 |
memset(&st, 0, sizeof(st)); |
| 1087 |
st.budget_used = (uint16_t)used; |
| 1088 |
st.demand = demand; |
| 1089 |
st.evaluated = traced_evaluated; |
| 1090 |
st.drawn = traced_drawn; |
| 1091 |
st.dropped = traced_dropped; |
| 1092 |
st.cut_slot = traced_cut_slot; |
| 1093 |
st.exhausted = (stopped_at < NUM_SPRITES); |
| 1094 |
sprite_trace_line_end(y, &st); |
| 1095 |
} |
| 1096 |
static void |
| 1097 |
render_layer_line_text(uint8_t layer, uint16_t y) |
| 1098 |
{ |
| 1099 |
const struct video_layer_properties *props = &prev_layer_properties[1][layer]; |
| 1100 |
const struct video_layer_properties *props0 = &prev_layer_properties[0][layer]; |
| 1101 |
|
| 1102 |
const uint8_t max_pixels_per_byte = (8 >> props->color_depth) - 1; |
| 1103 |
const int eff_y = calc_layer_eff_y(props0, y); |
| 1104 |
const int yy = eff_y & props->tileh_max; |
| 1105 |
|
| 1106 |
|
| 1107 |
const uint32_t y_add = (yy << props->tilew_log2) >> 3; |
| 1108 |
|
| 1109 |
const uint32_t map_addr_begin = calc_layer_map_addr_base2(props, props->min_eff_x, eff_y); |
| 1110 |
const uint32_t map_addr_end = calc_layer_map_addr_base2(props, props->max_eff_x, eff_y); |
| 1111 |
const int size = (map_addr_end - map_addr_begin) + 2; |
| 1112 |
|
| 1113 |
uint8_t tile_bytes[512]; |
| 1114 |
video_space_read_range(tile_bytes, map_addr_begin, size); |
| 1115 |
|
| 1116 |
uint32_t tile_start; |
| 1117 |
|
| 1118 |
uint8_t fg_color; |
| 1119 |
uint8_t bg_color; |
| 1120 |
uint8_t s; |
| 1121 |
uint8_t color_shift; |
| 1122 |
|
| 1123 |
{ |
| 1124 |
const int eff_x = calc_layer_eff_x(props, 0); |
| 1125 |
const int xx = eff_x & props->tilew_max; |
| 1126 |
|
| 1127 |
|
| 1128 |
const uint32_t map_addr = calc_layer_map_addr_base2(props, eff_x, eff_y) - map_addr_begin; |
| 1129 |
|
| 1130 |
const uint8_t tile_index = tile_bytes[map_addr]; |
| 1131 |
const uint8_t byte1 = tile_bytes[map_addr + 1]; |
| 1132 |
|
| 1133 |
if (!props->text_mode_256c) { |
| 1134 |
fg_color = byte1 & 15; |
| 1135 |
bg_color = byte1 >> 4; |
| 1136 |
} else { |
| 1137 |
fg_color = byte1; |
| 1138 |
bg_color = 0; |
| 1139 |
} |
| 1140 |
|
| 1141 |
|
| 1142 |
tile_start = tile_index << props->tile_size_log2; |
| 1143 |
|
| 1144 |
|
| 1145 |
const uint16_t x_add = xx >> 3; |
| 1146 |
const uint32_t tile_offset = tile_start + y_add + x_add; |
| 1147 |
|
| 1148 |
s = video_space_read(props->tile_base + tile_offset); |
| 1149 |
color_shift = max_pixels_per_byte - (xx & 0x7); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
for (int x = 0; x < SCREEN_WIDTH; x++) { |
| 1154 |
|
| 1155 |
const int eff_x = calc_layer_eff_x(props, x); |
| 1156 |
const int xx = eff_x & props->tilew_max; |
| 1157 |
|
| 1158 |
if ((eff_x & 0x7) == 0) { |
| 1159 |
if ((eff_x & props->tilew_max) == 0) { |
| 1160 |
|
| 1161 |
const uint32_t map_addr = calc_layer_map_addr_base2(props, eff_x, eff_y) - map_addr_begin; |
| 1162 |
|
| 1163 |
const uint8_t tile_index = tile_bytes[map_addr]; |
| 1164 |
const uint8_t byte1 = tile_bytes[map_addr + 1]; |
| 1165 |
|
| 1166 |
if (!props->text_mode_256c) { |
| 1167 |
fg_color = byte1 & 15; |
| 1168 |
bg_color = byte1 >> 4; |
| 1169 |
} else { |
| 1170 |
fg_color = byte1; |
| 1171 |
bg_color = 0; |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
tile_start = tile_index << props->tile_size_log2; |
| 1176 |
} |
| 1177 |
|
| 1178 |
|
| 1179 |
const uint16_t x_add = xx >> 3; |
| 1180 |
const uint32_t tile_offset = tile_start + y_add + x_add; |
| 1181 |
|
| 1182 |
s = video_space_read(props->tile_base + tile_offset); |
| 1183 |
color_shift = max_pixels_per_byte; |
| 1184 |
} |
| 1185 |
|
| 1186 |
|
| 1187 |
const uint8_t col_index = (s >> color_shift) & 1; |
| 1188 |
--color_shift; |
| 1189 |
layer_line[layer][x] = col_index ? fg_color : bg_color; |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
static void |
| 1194 |
render_layer_line_tile(uint8_t layer, uint16_t y) |
| 1195 |
{ |
| 1196 |
const struct video_layer_properties *props = &prev_layer_properties[1][layer]; |
| 1197 |
const struct video_layer_properties *props0 = &prev_layer_properties[0][layer]; |
| 1198 |
|
| 1199 |
const uint8_t max_pixels_per_byte = (8 >> props->color_depth) - 1; |
| 1200 |
const int eff_y = calc_layer_eff_y(props0, y); |
| 1201 |
const uint8_t yy = eff_y & props->tileh_max; |
| 1202 |
const uint8_t yy_flip = yy ^ props->tileh_max; |
| 1203 |
const uint32_t y_add = (yy << ((props->tilew_log2 + props->color_depth - 3) & 31)); |
| 1204 |
const uint32_t y_add_flip = (yy_flip << ((props->tilew_log2 + props->color_depth - 3) & 31)); |
| 1205 |
|
| 1206 |
const uint32_t map_addr_begin = calc_layer_map_addr_base2(props, props->min_eff_x, eff_y); |
| 1207 |
const uint32_t map_addr_end = calc_layer_map_addr_base2(props, props->max_eff_x, eff_y); |
| 1208 |
const int size = (map_addr_end - map_addr_begin) + 2; |
| 1209 |
|
| 1210 |
uint8_t tile_bytes[512]; |
| 1211 |
video_space_read_range(tile_bytes, map_addr_begin, size); |
| 1212 |
|
| 1213 |
uint8_t palette_offset; |
| 1214 |
bool vflip; |
| 1215 |
bool hflip; |
| 1216 |
uint32_t tile_start; |
| 1217 |
uint8_t s; |
| 1218 |
uint8_t color_shift; |
| 1219 |
int8_t color_shift_incr; |
| 1220 |
|
| 1221 |
{ |
| 1222 |
const int eff_x = calc_layer_eff_x(props, 0); |
| 1223 |
|
| 1224 |
|
| 1225 |
const uint32_t map_addr = calc_layer_map_addr_base2(props, eff_x, eff_y) - map_addr_begin; |
| 1226 |
|
| 1227 |
const uint8_t byte0 = tile_bytes[map_addr]; |
| 1228 |
const uint8_t byte1 = tile_bytes[map_addr + 1]; |
| 1229 |
|
| 1230 |
|
| 1231 |
vflip = (byte1 >> 3) & 1; |
| 1232 |
hflip = (byte1 >> 2) & 1; |
| 1233 |
|
| 1234 |
palette_offset = byte1 & 0xf0; |
| 1235 |
|
| 1236 |
|
| 1237 |
const uint16_t tile_index = byte0 | ((byte1 & 3) << 8); |
| 1238 |
tile_start = tile_index << props->tile_size_log2; |
| 1239 |
|
| 1240 |
color_shift_incr = hflip ? props->bits_per_pixel : -props->bits_per_pixel; |
| 1241 |
|
| 1242 |
int xx = eff_x & props->tilew_max; |
| 1243 |
if (hflip) { |
| 1244 |
xx = xx ^ (props->tilew_max); |
| 1245 |
color_shift = 0; |
| 1246 |
} else { |
| 1247 |
color_shift = props->first_color_pos; |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
uint16_t x_add = (xx << props->color_depth) >> 3; |
| 1252 |
uint32_t tile_offset = tile_start + (vflip ? y_add_flip : y_add) + x_add; |
| 1253 |
|
| 1254 |
s = video_space_read(props->tile_base + tile_offset); |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
for (int x = 0; x < SCREEN_WIDTH; x++) { |
| 1260 |
const int eff_x = calc_layer_eff_x(props, x); |
| 1261 |
|
| 1262 |
if ((eff_x & max_pixels_per_byte) == 0) { |
| 1263 |
if ((eff_x & props->tilew_max) == 0) { |
| 1264 |
|
| 1265 |
const uint32_t map_addr = calc_layer_map_addr_base2(props, eff_x, eff_y) - map_addr_begin; |
| 1266 |
|
| 1267 |
const uint8_t byte0 = tile_bytes[map_addr]; |
| 1268 |
const uint8_t byte1 = tile_bytes[map_addr + 1]; |
| 1269 |
|
| 1270 |
|
| 1271 |
vflip = (byte1 >> 3) & 1; |
| 1272 |
hflip = (byte1 >> 2) & 1; |
| 1273 |
|
| 1274 |
palette_offset = byte1 & 0xf0; |
| 1275 |
|
| 1276 |
|
| 1277 |
const uint16_t tile_index = byte0 | ((byte1 & 3) << 8); |
| 1278 |
tile_start = tile_index << props->tile_size_log2; |
| 1279 |
|
| 1280 |
color_shift_incr = hflip ? props->bits_per_pixel : -props->bits_per_pixel; |
| 1281 |
} |
| 1282 |
|
| 1283 |
int xx = eff_x & props->tilew_max; |
| 1284 |
if (hflip) { |
| 1285 |
xx = xx ^ (props->tilew_max); |
| 1286 |
color_shift = 0; |
| 1287 |
} else { |
| 1288 |
color_shift = props->first_color_pos; |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
const uint16_t x_add = (xx << props->color_depth) >> 3; |
| 1293 |
const uint32_t tile_offset = tile_start + (vflip ? y_add_flip : y_add) + x_add; |
| 1294 |
|
| 1295 |
s = video_space_read(props->tile_base + tile_offset); |
| 1296 |
} |
| 1297 |
|
| 1298 |
|
| 1299 |
uint8_t col_index = (s >> color_shift) & props->color_mask; |
| 1300 |
color_shift += color_shift_incr; |
| 1301 |
|
| 1302 |
|
| 1303 |
if (col_index > 0 && col_index < 16) { |
| 1304 |
col_index += palette_offset; |
| 1305 |
if (props->text_mode_256c) { |
| 1306 |
col_index |= 0x80; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
layer_line[layer][x] = col_index; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
static void |
| 1315 |
render_layer_line_bitmap(uint8_t layer, uint16_t y) |
| 1316 |
{ |
| 1317 |
const struct video_layer_properties *props = &prev_layer_properties[1][layer]; |
| 1318 |
|
| 1319 |
|
| 1320 |
int yy = y % props->tileh; |
| 1321 |
|
| 1322 |
uint32_t y_add = (yy * props->tilew * props->bits_per_pixel) >> 3; |
| 1323 |
|
| 1324 |
|
| 1325 |
for (int x = 0; x < SCREEN_WIDTH; x++) { |
| 1326 |
int xx = x % props->tilew; |
| 1327 |
|
| 1328 |
|
| 1329 |
uint8_t palette_offset = reg_layer[layer][4] & 0xf; |
| 1330 |
|
| 1331 |
|
| 1332 |
uint16_t x_add = (xx * props->bits_per_pixel) >> 3; |
| 1333 |
uint32_t tile_offset = y_add + x_add; |
| 1334 |
uint8_t s = video_space_read(props->tile_base + tile_offset); |
| 1335 |
|
| 1336 |
|
| 1337 |
uint8_t col_index = (s >> (props->first_color_pos - ((xx & props->color_fields_max) << props->color_depth))) & props->color_mask; |
| 1338 |
|
| 1339 |
|
| 1340 |
if (col_index > 0 && col_index < 16) { |
| 1341 |
col_index += palette_offset << 4; |
| 1342 |
if (props->text_mode_256c) { |
| 1343 |
col_index |= 0x80; |
| 1344 |
} |
| 1345 |
} |
| 1346 |
layer_line[layer][x] = col_index; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
static uint8_t calculate_line_col_index(uint8_t spr_zindex, uint8_t spr_col_index, uint8_t l1_col_index, uint8_t l2_col_index) |
| 1351 |
{ |
| 1352 |
uint8_t col_index = 0; |
| 1353 |
switch (spr_zindex) { |
| 1354 |
case 3: |
| 1355 |
col_index = spr_col_index ? spr_col_index : (l2_col_index ? l2_col_index : l1_col_index); |
| 1356 |
break; |
| 1357 |
case 2: |
| 1358 |
col_index = l2_col_index ? l2_col_index : (spr_col_index ? spr_col_index : l1_col_index); |
| 1359 |
break; |
| 1360 |
case 1: |
| 1361 |
col_index = l2_col_index ? l2_col_index : (l1_col_index ? l1_col_index : spr_col_index); |
| 1362 |
break; |
| 1363 |
case 0: |
| 1364 |
col_index = l2_col_index ? l2_col_index : l1_col_index; |
| 1365 |
break; |
| 1366 |
} |
| 1367 |
return col_index; |
| 1368 |
} |
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
static vera_bw_layer_t |
| 1384 |
vera_bw_layer_of(uint8_t layer) |
| 1385 |
{ |
| 1386 |
const struct video_layer_properties *p = &prev_layer_properties[1][layer]; |
| 1387 |
vera_bw_layer_t out; |
| 1388 |
|
| 1389 |
out.enabled = layer_line_enable[layer] != 0; |
| 1390 |
out.bitmap_mode = p->bitmap_mode; |
| 1391 |
out.color_depth = p->color_depth; |
| 1392 |
|
| 1393 |
|
| 1394 |
out.tile_width = (uint8_t)(p->bitmap_mode ? (p->tilew > 320) : (p->tilew_log2 > 3)); |
| 1395 |
out.scrolled = !p->bitmap_mode && (p->hscroll & p->tilew_max) != 0; |
| 1396 |
|
| 1397 |
return out; |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
static bool debug_port_write = false; |
| 1410 |
|
| 1411 |
void |
| 1412 |
video_set_debug_write(bool on) |
| 1413 |
{ |
| 1414 |
debug_port_write = on; |
| 1415 |
} |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
static void |
| 1422 |
note_port_traffic(bool write, uint8_t vram_bytes) |
| 1423 |
{ |
| 1424 |
if (!vera_bandwidth_active || debug_port_write) |
| 1425 |
return; |
| 1426 |
vera_bandwidth_note_port(write, vram_bytes, sprite_trace_current_line()); |
| 1427 |
} |
| 1428 |
|
| 1429 |
static void |
| 1430 |
note_port_fx_cache_write(void) |
| 1431 |
{ |
| 1432 |
if (!vera_bandwidth_active || debug_port_write) |
| 1433 |
return; |
| 1434 |
vera_bandwidth_note_fx_cache_write(sprite_trace_current_line()); |
| 1435 |
} |
| 1436 |
|
| 1437 |
static void |
| 1438 |
note_port_fx_affine(void) |
| 1439 |
{ |
| 1440 |
if (!vera_bandwidth_active || debug_port_write) |
| 1441 |
return; |
| 1442 |
vera_bandwidth_note_fx_affine(sprite_trace_current_line()); |
| 1443 |
} |
| 1444 |
|
| 1445 |
static void |
| 1446 |
render_line(uint16_t y, float scan_pos_x) |
| 1447 |
{ |
| 1448 |
static uint16_t y_prev; |
| 1449 |
static uint16_t s_pos_x_p; |
| 1450 |
static uint32_t eff_y_fp; |
| 1451 |
static uint32_t eff_x_fp; |
| 1452 |
|
| 1453 |
static uint8_t col_line[SCREEN_WIDTH]; |
| 1454 |
|
| 1455 |
uint8_t dc_video = reg_composer[0]; |
| 1456 |
uint16_t vstart = reg_composer[6] << 1; |
| 1457 |
uint16_t vstop = reg_composer[7] << 1; |
| 1458 |
|
| 1459 |
if (y != y_prev) { |
| 1460 |
y_prev = y; |
| 1461 |
s_pos_x_p = 0; |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
memcpy(prev_reg_composer[1], prev_reg_composer[0], sizeof(*reg_composer) * COMPOSER_SLOTS); |
| 1472 |
memcpy(prev_reg_composer[0], reg_composer, sizeof(*reg_composer) * COMPOSER_SLOTS); |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
memcpy(prev_layer_properties[1], prev_layer_properties[0], sizeof(*layer_properties) * NUM_LAYERS); |
| 1477 |
memcpy(prev_layer_properties[0], layer_properties, sizeof(*layer_properties) * NUM_LAYERS); |
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
memcpy(prev_reg_layer[1], prev_reg_layer[0], sizeof(prev_reg_layer[0])); |
| 1486 |
memcpy(prev_reg_layer[0], reg_layer, sizeof(reg_layer)); |
| 1487 |
|
| 1488 |
if ((dc_video & 3) > 1) { |
| 1489 |
if ((y >> 1) == 0) { |
| 1490 |
eff_y_fp = y*(prev_reg_composer[1][2] << 9); |
| 1491 |
} else if ( ((y & 0xfffe) >= vstart) && ((y & 0xfffe) < vstop) ) { |
| 1492 |
eff_y_fp += (prev_reg_composer[1][2] << 10); |
| 1493 |
} |
| 1494 |
} else { |
| 1495 |
if (y == 0) { |
| 1496 |
eff_y_fp = 0; |
| 1497 |
} else if ( (y >= vstart) && (y < vstop) ) { |
| 1498 |
eff_y_fp += (prev_reg_composer[1][2] << 9); |
| 1499 |
} |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
if ((dc_video & 8) && (dc_video & 3) > 1) { |
| 1504 |
y &= 0xfffe; |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
if (video_palette.dirty) { |
| 1509 |
refresh_palette(); |
| 1510 |
} |
| 1511 |
|
| 1512 |
if (y >= SCREEN_HEIGHT) { |
| 1513 |
return; |
| 1514 |
} |
| 1515 |
|
| 1516 |
uint16_t s_pos_x = round(scan_pos_x); |
| 1517 |
if (s_pos_x > SCREEN_WIDTH) { |
| 1518 |
s_pos_x = SCREEN_WIDTH; |
| 1519 |
} |
| 1520 |
|
| 1521 |
if (s_pos_x_p == 0) { |
| 1522 |
eff_x_fp = 0; |
| 1523 |
} |
| 1524 |
|
| 1525 |
uint8_t out_mode = reg_composer[0] & 3; |
| 1526 |
|
| 1527 |
uint8_t border_color = reg_composer[3]; |
| 1528 |
uint16_t hstart = reg_composer[4] << 2; |
| 1529 |
uint16_t hstop = reg_composer[5] << 2; |
| 1530 |
|
| 1531 |
uint16_t eff_y = (eff_y_fp >> 16); |
| 1532 |
if (eff_y >= 480) eff_y = 480 - (y & 1); |
| 1533 |
|
| 1534 |
layer_line_enable[0] = dc_video & 0x10; |
| 1535 |
layer_line_enable[1] = dc_video & 0x20; |
| 1536 |
sprite_line_enable = dc_video & 0x40; |
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
for (uint8_t layer = 0; layer < 2; layer++) { |
| 1541 |
if (!layer_line_enable[layer] && old_layer_line_enable[layer]) { |
| 1542 |
for (uint16_t i = s_pos_x_p; i < SCREEN_WIDTH; i++) { |
| 1543 |
layer_line[layer][i] = 0; |
| 1544 |
} |
| 1545 |
} |
| 1546 |
if (s_pos_x_p == 0) |
| 1547 |
old_layer_line_enable[layer] = layer_line_enable[layer]; |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
if (!sprite_line_enable && old_sprite_line_enable) { |
| 1552 |
for (uint16_t i = s_pos_x_p; i < SCREEN_WIDTH; i++) { |
| 1553 |
sprite_line_col[i] = 0; |
| 1554 |
sprite_line_z[i] = 0; |
| 1555 |
sprite_line_mask[i] = 0; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
|
| 1559 |
if (s_pos_x_p == 0) |
| 1560 |
old_sprite_line_enable = sprite_line_enable; |
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
if (sprite_line_enable) { |
| 1565 |
render_sprite_line(eff_y); |
| 1566 |
} |
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
if (vera_bandwidth_active) { |
| 1590 |
const vera_bw_layer_t l0 = vera_bw_layer_of(0); |
| 1591 |
const vera_bw_layer_t l1 = vera_bw_layer_of(1); |
| 1592 |
vera_bandwidth_line(y, &l0, &l1); |
| 1593 |
} |
| 1594 |
|
| 1595 |
if (warp_mode && (frame_count & 63)) { |
| 1596 |
|
| 1597 |
|
| 1598 |
return; |
| 1599 |
} |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
if (y < SCREEN_HEIGHT) { |
| 1607 |
|
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
const bool line_displayed = (out_mode != 0) && (y >= vstart) && (y < vstop); |
| 1617 |
|
| 1618 |
memcpy(line_reg_layer[y], prev_reg_layer[1], sizeof(line_reg_layer[y])); |
| 1619 |
line_eff_y[y] = eff_y; |
| 1620 |
line_layer_enabled[y] = line_displayed |
| 1621 |
? (uint8_t)((layer_line_enable[0] ? 1 : 0) | (layer_line_enable[1] ? 2 : 0)) |
| 1622 |
: 0; |
| 1623 |
for (uint8_t l = 0; l < 2; l++) { |
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
line_layer_row[y][l] = prev_layer_properties[1][l].bitmap_mode |
| 1631 |
? eff_y |
| 1632 |
: (uint16_t)calc_layer_eff_y(&prev_layer_properties[0][l], eff_y); |
| 1633 |
} |
| 1634 |
line_state_valid[y] = true; |
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
if ((dc_video & 8) && (dc_video & 3) > 1 && y + 1 < SCREEN_HEIGHT) { |
| 1640 |
line_state_valid[y + 1] = false; |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
if (layer_line_enable[0]) { |
| 1645 |
if (prev_layer_properties[1][0].text_mode) { |
| 1646 |
render_layer_line_text(0, eff_y); |
| 1647 |
} else if (prev_layer_properties[1][0].bitmap_mode) { |
| 1648 |
render_layer_line_bitmap(0, eff_y); |
| 1649 |
} else { |
| 1650 |
render_layer_line_tile(0, eff_y); |
| 1651 |
} |
| 1652 |
} |
| 1653 |
if (layer_line_enable[1]) { |
| 1654 |
if (prev_layer_properties[1][1].text_mode) { |
| 1655 |
render_layer_line_text(1, eff_y); |
| 1656 |
} else if (prev_layer_properties[1][1].bitmap_mode) { |
| 1657 |
render_layer_line_bitmap(1, eff_y); |
| 1658 |
} else { |
| 1659 |
render_layer_line_tile(1, eff_y); |
| 1660 |
} |
| 1661 |
} |
| 1662 |
|
| 1663 |
|
| 1664 |
if (out_mode != 0) { |
| 1665 |
|
| 1666 |
if (y < vstart || y >= vstop) { |
| 1667 |
uint32_t border_fill = border_color; |
| 1668 |
border_fill = border_fill | (border_fill << 8); |
| 1669 |
border_fill = border_fill | (border_fill << 16); |
| 1670 |
memset(col_line, border_fill, SCREEN_WIDTH); |
| 1671 |
} else { |
| 1672 |
hstart = hstart < 640 ? hstart : 640; |
| 1673 |
hstop = hstop < 640 ? hstop : 640; |
| 1674 |
|
| 1675 |
for (uint16_t x = s_pos_x_p; x < hstart && x < s_pos_x; ++x) { |
| 1676 |
col_line[x] = border_color; |
| 1677 |
} |
| 1678 |
|
| 1679 |
const uint32_t scale = reg_composer[1]; |
| 1680 |
for (uint16_t x = MAX(hstart, s_pos_x_p); x < hstop && x < s_pos_x; ++x) { |
| 1681 |
uint16_t eff_x = eff_x_fp >> 16; |
| 1682 |
col_line[x] = (eff_x < SCREEN_WIDTH) ? calculate_line_col_index(sprite_line_z[eff_x], sprite_line_col[eff_x], layer_line[0][eff_x], layer_line[1][eff_x]) : 0; |
| 1683 |
eff_x_fp += (scale << 9); |
| 1684 |
} |
| 1685 |
for (uint16_t x = hstop; x < s_pos_x; ++x) { |
| 1686 |
col_line[x] = border_color; |
| 1687 |
} |
| 1688 |
} |
| 1689 |
} |
| 1690 |
|
| 1691 |
|
| 1692 |
uint32_t* framebuffer4_begin = ((uint32_t*)framebuffer) + (y * SCREEN_WIDTH) + s_pos_x_p; |
| 1693 |
{ |
| 1694 |
uint32_t* framebuffer4 = framebuffer4_begin; |
| 1695 |
for (uint16_t x = s_pos_x_p; x < s_pos_x; x++) { |
| 1696 |
*framebuffer4++ = video_palette.entries[col_line[x]]; |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
|
| 1701 |
if (out_mode == 2) { |
| 1702 |
uint32_t* framebuffer4 = framebuffer4_begin; |
| 1703 |
for (uint16_t x = s_pos_x_p; x < s_pos_x; x++) |
| 1704 |
{ |
| 1705 |
if (x < SCREEN_WIDTH * TITLE_SAFE_X || |
| 1706 |
x > SCREEN_WIDTH * (1 - TITLE_SAFE_X) || |
| 1707 |
y < SCREEN_HEIGHT * TITLE_SAFE_Y || |
| 1708 |
y > SCREEN_HEIGHT * (1 - TITLE_SAFE_Y)) { |
| 1709 |
|
| 1710 |
|
| 1711 |
*framebuffer4 &= 0x00fcfcfc; |
| 1712 |
*framebuffer4 >>= 2; |
| 1713 |
} |
| 1714 |
framebuffer4++; |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
s_pos_x_p = s_pos_x; |
| 1719 |
} |
| 1720 |
|
| 1721 |
static void |
| 1722 |
update_isr_and_coll(uint16_t y, uint16_t compare) |
| 1723 |
{ |
| 1724 |
if (y == SCREEN_HEIGHT) { |
| 1725 |
if (sprite_line_collisions != 0) { |
| 1726 |
isr |= 4; |
| 1727 |
} |
| 1728 |
isr = (isr & 0xf) | sprite_line_collisions; |
| 1729 |
sprite_line_collisions = 0; |
| 1730 |
isr |= 1; |
| 1731 |
} |
| 1732 |
if (y == compare) { |
| 1733 |
isr |= 2; |
| 1734 |
} |
| 1735 |
} |
| 1736 |
|
| 1737 |
bool |
| 1738 |
video_step(float mhz, float steps, bool midline) |
| 1739 |
{ |
| 1740 |
uint16_t y = 0; |
| 1741 |
bool ntsc_mode = reg_composer[0] & 2; |
| 1742 |
bool new_frame = false; |
| 1743 |
sprite_trace_cpu_cycles += (uint32_t)steps; |
| 1744 |
vga_scan_pos_x += PIXEL_FREQ * steps / mhz; |
| 1745 |
if (vga_scan_pos_x > VGA_SCAN_WIDTH) { |
| 1746 |
vga_scan_pos_x -= VGA_SCAN_WIDTH; |
| 1747 |
if (!ntsc_mode) { |
| 1748 |
render_line(vga_scan_pos_y - VGA_Y_OFFSET, VGA_SCAN_WIDTH); |
| 1749 |
} |
| 1750 |
vga_scan_pos_y++; |
| 1751 |
if (vga_scan_pos_y == SCAN_HEIGHT) { |
| 1752 |
vga_scan_pos_y = 0; |
| 1753 |
if (!ntsc_mode) { |
| 1754 |
new_frame = true; |
| 1755 |
frame_count++; |
| 1756 |
} |
| 1757 |
} |
| 1758 |
if (!ntsc_mode) { |
| 1759 |
update_isr_and_coll(vga_scan_pos_y - VGA_Y_OFFSET, irq_line); |
| 1760 |
} |
| 1761 |
} else if (midline) { |
| 1762 |
if (!ntsc_mode) { |
| 1763 |
render_line(vga_scan_pos_y - VGA_Y_OFFSET, vga_scan_pos_x); |
| 1764 |
} |
| 1765 |
} |
| 1766 |
ntsc_half_cnt += PIXEL_FREQ * steps / mhz; |
| 1767 |
if (ntsc_half_cnt > NTSC_HALF_SCAN_WIDTH) { |
| 1768 |
ntsc_half_cnt -= NTSC_HALF_SCAN_WIDTH; |
| 1769 |
if (ntsc_mode) { |
| 1770 |
if (ntsc_scan_pos_y < SCAN_HEIGHT) { |
| 1771 |
y = ntsc_scan_pos_y - NTSC_Y_OFFSET_LOW; |
| 1772 |
if ((y & 1) == 0) { |
| 1773 |
render_line(y, NTSC_HALF_SCAN_WIDTH); |
| 1774 |
} |
| 1775 |
} else { |
| 1776 |
y = ntsc_scan_pos_y - NTSC_Y_OFFSET_HIGH; |
| 1777 |
if ((y & 1) == 0) { |
| 1778 |
render_line(y | 1, NTSC_HALF_SCAN_WIDTH); |
| 1779 |
} |
| 1780 |
} |
| 1781 |
} |
| 1782 |
ntsc_scan_pos_y++; |
| 1783 |
if (ntsc_scan_pos_y == SCAN_HEIGHT) { |
| 1784 |
reg_composer[0] |= 0x80; |
| 1785 |
if (ntsc_mode) { |
| 1786 |
new_frame = true; |
| 1787 |
frame_count++; |
| 1788 |
} |
| 1789 |
} |
| 1790 |
if (ntsc_scan_pos_y == SCAN_HEIGHT*2) { |
| 1791 |
reg_composer[0] &= ~0x80; |
| 1792 |
ntsc_scan_pos_y = 0; |
| 1793 |
if (ntsc_mode) { |
| 1794 |
new_frame = true; |
| 1795 |
frame_count++; |
| 1796 |
} |
| 1797 |
} |
| 1798 |
if (ntsc_mode) { |
| 1799 |
|
| 1800 |
if (ntsc_scan_pos_y < SCAN_HEIGHT) { |
| 1801 |
update_isr_and_coll(ntsc_scan_pos_y - NTSC_Y_OFFSET_LOW, irq_line & ~1); |
| 1802 |
} else { |
| 1803 |
update_isr_and_coll(ntsc_scan_pos_y - NTSC_Y_OFFSET_HIGH, irq_line & ~1); |
| 1804 |
} |
| 1805 |
} |
| 1806 |
} else if (midline) { |
| 1807 |
if (ntsc_mode) { |
| 1808 |
if (ntsc_scan_pos_y < SCAN_HEIGHT) { |
| 1809 |
y = ntsc_scan_pos_y - NTSC_Y_OFFSET_LOW; |
| 1810 |
if ((y & 1) == 0) { |
| 1811 |
render_line(y, ntsc_half_cnt); |
| 1812 |
} |
| 1813 |
} else { |
| 1814 |
y = ntsc_scan_pos_y - NTSC_Y_OFFSET_HIGH; |
| 1815 |
if ((y & 1) == 0) { |
| 1816 |
render_line(y | 1, ntsc_half_cnt); |
| 1817 |
} |
| 1818 |
} |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
if (new_frame && sprite_trace_active) { |
| 1826 |
sprite_trace_frame_advance((uint32_t)frame_count, sprite_trace_cpu_cycles, sprite_data); |
| 1827 |
} |
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
if (new_frame) { |
| 1839 |
vera_bandwidth_frame_end((uint32_t)(frame_count - 1)); |
| 1840 |
} |
| 1841 |
|
| 1842 |
return new_frame; |
| 1843 |
} |
| 1844 |
|
| 1845 |
const uint8_t * |
| 1846 |
video_get_framebuffer(int *width, int *height) |
| 1847 |
{ |
| 1848 |
if (width) *width = SCREEN_WIDTH; |
| 1849 |
if (height) *height = SCREEN_HEIGHT; |
| 1850 |
return framebuffer; |
| 1851 |
} |
| 1852 |
|
| 1853 |
uint32_t |
| 1854 |
video_get_frame_count(void) |
| 1855 |
{ |
| 1856 |
return (uint32_t)frame_count; |
| 1857 |
} |
| 1858 |
|
| 1859 |
|
| 1860 |
bool |
| 1861 |
video_get_irq_out() |
| 1862 |
{ |
| 1863 |
uint8_t tmp_isr = isr | (pcm_is_fifo_almost_empty() ? 8 : 0); |
| 1864 |
return (tmp_isr & ien) != 0; |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
void |
| 1872 |
video_save(SDL_RWops *f) |
| 1873 |
{ |
| 1874 |
SDL_RWwrite(f, &video_ram[0], sizeof(uint8_t), sizeof(video_ram)); |
| 1875 |
SDL_RWwrite(f, ®_composer[0], sizeof(uint8_t), sizeof(reg_composer)); |
| 1876 |
SDL_RWwrite(f, &palette[0], sizeof(uint8_t), sizeof(palette)); |
| 1877 |
SDL_RWwrite(f, ®_layer[0][0], sizeof(uint8_t), sizeof(reg_layer)); |
| 1878 |
SDL_RWwrite(f, &sprite_data[0], sizeof(uint8_t), sizeof(sprite_data)); |
| 1879 |
} |
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
static void |
| 1891 |
video_render_all_ex(bool new_frame) |
| 1892 |
{ |
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
for (int y = 0; new_frame && y < 4; y+=1+!!((reg_composer[0] & 0x0b) > 0x09)) { |
| 1901 |
for (int x = SCREEN_WIDTH - 8; x < SCREEN_WIDTH; x++) { |
| 1902 |
uint8_t b = framebuffer[(y * SCREEN_WIDTH + x) * 4 + 0]; |
| 1903 |
uint8_t g = framebuffer[(y * SCREEN_WIDTH + x) * 4 + 1]; |
| 1904 |
uint8_t r = framebuffer[(y * SCREEN_WIDTH + x) * 4 + 2]; |
| 1905 |
r = (uint32_t)r * (255 - activity_led) / 255 + activity_led; |
| 1906 |
g = (uint32_t)g * (255 - activity_led) / 255; |
| 1907 |
b = (uint32_t)b * (255 - activity_led) / 255; |
| 1908 |
framebuffer[(y * SCREEN_WIDTH + x) * 4 + 0] = b; |
| 1909 |
framebuffer[(y * SCREEN_WIDTH + x) * 4 + 1] = g; |
| 1910 |
framebuffer[(y * SCREEN_WIDTH + x) * 4 + 2] = r; |
| 1911 |
framebuffer[(y * SCREEN_WIDTH + x) * 4 + 3] = 0x00; |
| 1912 |
} |
| 1913 |
} |
| 1914 |
|
| 1915 |
SDL_UpdateTexture(sdlTexture, NULL, framebuffer, SCREEN_WIDTH * 4); |
| 1916 |
|
| 1917 |
|
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
if (new_frame && record_gif > RECORD_GIF_PAUSED) { |
| 1922 |
if(!GifWriteFrame(&gif_writer, framebuffer, SCREEN_WIDTH, SCREEN_HEIGHT, 2, 8, false)) { |
| 1923 |
|
| 1924 |
GifEnd(&gif_writer); |
| 1925 |
record_gif = RECORD_GIF_DISABLED; |
| 1926 |
printf("Unexpected end of recording.\n"); |
| 1927 |
} |
| 1928 |
if (record_gif == RECORD_GIF_SINGLE) { |
| 1929 |
record_gif = RECORD_GIF_PAUSED; |
| 1930 |
} |
| 1931 |
} |
| 1932 |
|
| 1933 |
SDL_RenderClear(renderer); |
| 1934 |
SDL_RenderCopy(renderer, sdlTexture, NULL, NULL); |
| 1935 |
|
| 1936 |
if (debugger_enabled && showDebugOnRender != 0) { |
| 1937 |
DEBUGRenderDisplay(SCREEN_WIDTH, SCREEN_HEIGHT); |
| 1938 |
} |
| 1939 |
|
| 1940 |
SDL_RenderPresent(renderer); |
| 1941 |
|
| 1942 |
#ifdef HAS_IMGUI |
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
if (imgui_renderer != NULL) { |
| 1952 |
static Uint32 last_ui_ticks = 0; |
| 1953 |
const Uint32 now = SDL_GetTicks(); |
| 1954 |
|
| 1955 |
if (now - last_ui_ticks >= 16) { |
| 1956 |
last_ui_ticks = now; |
| 1957 |
debug_ui_render(); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
#endif |
| 1961 |
} |
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
void |
| 1969 |
video_present_no_input(void) |
| 1970 |
{ |
| 1971 |
if (renderer == NULL || sdlTexture == NULL) { |
| 1972 |
return; |
| 1973 |
} |
| 1974 |
video_render_all_ex(true); |
| 1975 |
} |
| 1976 |
|
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
void |
| 1981 |
video_repaint_only(void) |
| 1982 |
{ |
| 1983 |
if (renderer == NULL || sdlTexture == NULL) { |
| 1984 |
return; |
| 1985 |
} |
| 1986 |
video_render_all_ex(false); |
| 1987 |
} |
| 1988 |
|
| 1989 |
static void |
| 1990 |
video_render_all(void) |
| 1991 |
{ |
| 1992 |
video_render_all_ex(true); |
| 1993 |
} |
| 1994 |
|
| 1995 |
#ifdef HAS_IMGUI |
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
bool |
| 2004 |
video_debug_ui_available(void) |
| 2005 |
{ |
| 2006 |
return imgui_window != NULL && imgui_renderer != NULL; |
| 2007 |
} |
| 2008 |
|
| 2009 |
static void video_imgui_debug_key(SDL_Keysym ks); |
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
bool |
| 2014 |
video_is_debug_ui_window(Uint32 window_id) |
| 2015 |
{ |
| 2016 |
return imgui_window != NULL && window_id == imgui_window_id; |
| 2017 |
} |
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
bool |
| 2028 |
video_event_targets_debug_ui(const SDL_Event *ev) |
| 2029 |
{ |
| 2030 |
if (imgui_window == NULL) { |
| 2031 |
return false; |
| 2032 |
} |
| 2033 |
switch (ev->type) { |
| 2034 |
case SDL_WINDOWEVENT: return ev->window.windowID == imgui_window_id; |
| 2035 |
case SDL_KEYDOWN: |
| 2036 |
case SDL_KEYUP: return ev->key.windowID == imgui_window_id; |
| 2037 |
case SDL_TEXTINPUT: return ev->text.windowID == imgui_window_id; |
| 2038 |
case SDL_TEXTEDITING: return ev->edit.windowID == imgui_window_id; |
| 2039 |
case SDL_MOUSEBUTTONDOWN: |
| 2040 |
case SDL_MOUSEBUTTONUP: return ev->button.windowID == imgui_window_id; |
| 2041 |
case SDL_MOUSEMOTION: return ev->motion.windowID == imgui_window_id; |
| 2042 |
case SDL_MOUSEWHEEL: return ev->wheel.windowID == imgui_window_id; |
| 2043 |
default: return false; |
| 2044 |
} |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
void |
| 2051 |
video_debug_ui_feed_event(const SDL_Event *ev) |
| 2052 |
{ |
| 2053 |
if (imgui_window != NULL) { |
| 2054 |
debug_ui_process_event(ev); |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
void |
| 2062 |
video_debug_ui_shortcut_key(const SDL_Event *ev) |
| 2063 |
{ |
| 2064 |
if (ev->type == SDL_KEYDOWN && !debug_ui_want_text_input()) { |
| 2065 |
video_imgui_debug_key(ev->key.keysym); |
| 2066 |
} |
| 2067 |
} |
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
|
| 2079 |
static void |
| 2080 |
video_imgui_debug_key(SDL_Keysym ks) |
| 2081 |
{ |
| 2082 |
const bool shift = (ks.mod & KMOD_SHIFT) != 0; |
| 2083 |
const bool ctrl = (ks.mod & KMOD_CTRL) != 0; |
| 2084 |
switch (ks.scancode) { |
| 2085 |
case SDL_SCANCODE_F5: |
| 2086 |
if (ctrl && shift) { |
| 2087 |
smc_requested_reset = true; |
| 2088 |
} else if (DEBUGIsPaused()) { |
| 2089 |
DEBUGContinue(); |
| 2090 |
} |
| 2091 |
break; |
| 2092 |
case SDL_SCANCODE_F9: |
| 2093 |
debug_ui_toggle_breakpoint_at_pc(); |
| 2094 |
break; |
| 2095 |
case SDL_SCANCODE_F10: |
| 2096 |
if (ctrl) { |
| 2097 |
debug_ui_run_to_cursor(); |
| 2098 |
} else if (DEBUGIsPaused()) { |
| 2099 |
DEBUGStepOverAuto(DEBUG_OWNER_UI); |
| 2100 |
} |
| 2101 |
break; |
| 2102 |
case SDL_SCANCODE_F11: |
| 2103 |
if (DEBUGIsPaused()) { |
| 2104 |
if (shift) { |
| 2105 |
DEBUGStepOut(DEBUG_OWNER_UI); |
| 2106 |
} else { |
| 2107 |
DEBUGStepIntoAuto(); |
| 2108 |
} |
| 2109 |
} |
| 2110 |
break; |
| 2111 |
case SDL_SCANCODE_PAUSE: |
| 2112 |
if (DEBUGIsRunning()) { |
| 2113 |
DEBUGPause(); |
| 2114 |
} |
| 2115 |
break; |
| 2116 |
default: |
| 2117 |
break; |
| 2118 |
} |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
int |
| 2128 |
video_debug_ui_pump_paused(void) |
| 2129 |
{ |
| 2130 |
SDL_Event event; |
| 2131 |
while (SDL_PollEvent(&event)) { |
| 2132 |
|
| 2133 |
debug_ui_process_event(&event); |
| 2134 |
|
| 2135 |
if (event.type == SDL_QUIT) { |
| 2136 |
return -1; |
| 2137 |
} |
| 2138 |
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) { |
| 2139 |
|
| 2140 |
|
| 2141 |
if (imgui_window && event.window.windowID == imgui_window_id) { |
| 2142 |
continue; |
| 2143 |
} |
| 2144 |
return -1; |
| 2145 |
} |
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
if (event.type == SDL_KEYDOWN && imgui_window && |
| 2150 |
event.key.windowID == imgui_window_id && |
| 2151 |
!debug_ui_want_text_input()) { |
| 2152 |
video_imgui_debug_key(event.key.keysym); |
| 2153 |
} |
| 2154 |
} |
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
video_repaint_only(); |
| 2162 |
|
| 2163 |
SDL_Delay(16); |
| 2164 |
|
| 2165 |
|
| 2166 |
|
| 2167 |
|
| 2168 |
if (!DEBUGIsPaused()) { |
| 2169 |
return 0; |
| 2170 |
} |
| 2171 |
return 1; |
| 2172 |
} |
| 2173 |
#else |
| 2174 |
bool video_debug_ui_available(void) { return false; } |
| 2175 |
int video_debug_ui_pump_paused(void) { return 1; } |
| 2176 |
bool video_is_debug_ui_window(Uint32 window_id) { (void)window_id; return false; } |
| 2177 |
bool video_event_targets_debug_ui(const SDL_Event *ev) { (void)ev; return false; } |
| 2178 |
void video_debug_ui_shortcut_key(const SDL_Event *ev) { (void)ev; } |
| 2179 |
void video_debug_ui_feed_event(const SDL_Event *ev) { (void)ev; } |
| 2180 |
#endif |
| 2181 |
|
| 2182 |
bool |
| 2183 |
video_update() |
| 2184 |
{ |
| 2185 |
static bool cmd_down = false; |
| 2186 |
static bool alt_down = false; |
| 2187 |
bool mouse_changed = false; |
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
bool debug_overlay = (debugger_enabled && showDebugOnRender != 0); |
| 2192 |
|
| 2193 |
video_render_all(); |
| 2194 |
|
| 2195 |
if (debug_overlay) { |
| 2196 |
return true; |
| 2197 |
} |
| 2198 |
|
| 2199 |
SDL_Event event; |
| 2200 |
while (SDL_PollEvent(&event)) { |
| 2201 |
#ifdef HAS_IMGUI |
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
if (imgui_window) { |
| 2206 |
debug_ui_process_event(&event); |
| 2207 |
if (video_event_targets_debug_ui(&event)) { |
| 2208 |
|
| 2209 |
if (event.type == SDL_WINDOWEVENT && |
| 2210 |
event.window.event == SDL_WINDOWEVENT_CLOSE) { |
| 2211 |
continue; |
| 2212 |
} |
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
video_debug_ui_shortcut_key(&event); |
| 2217 |
continue; |
| 2218 |
} |
| 2219 |
} |
| 2220 |
#endif |
| 2221 |
if (event.type == SDL_QUIT) { |
| 2222 |
return false; |
| 2223 |
} |
| 2224 |
#ifdef HAS_IMGUI |
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
if (event.type == SDL_WINDOWEVENT && |
| 2233 |
event.window.event == SDL_WINDOWEVENT_CLOSE) { |
| 2234 |
return false; |
| 2235 |
} |
| 2236 |
#endif |
| 2237 |
if (event.type == SDL_KEYDOWN) { |
| 2238 |
bool consumed = false; |
| 2239 |
if (cmd_down && !(disable_emu_cmd_keys || mouse_grabbed)) { |
| 2240 |
if (event.key.keysym.sym == SDLK_s) { |
| 2241 |
machine_dump("user keyboard request"); |
| 2242 |
consumed = true; |
| 2243 |
} else if (event.key.keysym.sym == SDLK_r) { |
| 2244 |
machine_reset(); |
| 2245 |
consumed = true; |
| 2246 |
} else if (event.key.keysym.sym == SDLK_BACKSPACE) { |
| 2247 |
machine_nmi(); |
| 2248 |
consumed = true; |
| 2249 |
} else if (event.key.keysym.sym == SDLK_v) { |
| 2250 |
machine_paste(SDL_GetClipboardText(), true); |
| 2251 |
consumed = true; |
| 2252 |
} else if (event.key.keysym.sym == SDLK_f || event.key.keysym.sym == SDLK_RETURN) { |
| 2253 |
is_fullscreen = !is_fullscreen; |
| 2254 |
SDL_SetWindowFullscreen(window, is_fullscreen ? SDL_WINDOW_FULLSCREEN : 0); |
| 2255 |
consumed = true; |
| 2256 |
} else if (event.key.keysym.sym == SDLK_PLUS || event.key.keysym.sym == SDLK_EQUALS) { |
| 2257 |
machine_toggle_warp(); |
| 2258 |
consumed = true; |
| 2259 |
} else if (event.key.keysym.sym == SDLK_a) { |
| 2260 |
sdcard_attach(); |
| 2261 |
consumed = true; |
| 2262 |
} else if (event.key.keysym.sym == SDLK_d) { |
| 2263 |
sdcard_detach(); |
| 2264 |
consumed = true; |
| 2265 |
#ifndef __EMSCRIPTEN__ |
| 2266 |
} else if (event.key.keysym.sym == SDLK_p) { |
| 2267 |
screenshot(); |
| 2268 |
consumed = true; |
| 2269 |
#endif |
| 2270 |
} |
| 2271 |
} |
| 2272 |
if (cmd_down) { |
| 2273 |
if (event.key.keysym.sym == SDLK_m) { |
| 2274 |
mousegrab_toggle(); |
| 2275 |
consumed = true; |
| 2276 |
} |
| 2277 |
} |
| 2278 |
if (alt_down && !disable_emu_cmd_keys) { |
| 2279 |
|
| 2280 |
if (event.key.keysym.sym == SDLK_F4) { |
| 2281 |
printf("Shutdown requested by ALT+F4\n"); |
| 2282 |
main_shutdown(); |
| 2283 |
#ifdef __EMSCRIPTEN__ |
| 2284 |
emscripten_force_exit(0); |
| 2285 |
#endif |
| 2286 |
exit(0); |
| 2287 |
} |
| 2288 |
} |
| 2289 |
if (!consumed) { |
| 2290 |
if (event.key.keysym.scancode == LSHORTCUT_KEY || event.key.keysym.scancode == RSHORTCUT_KEY) { |
| 2291 |
cmd_down = true; |
| 2292 |
} |
| 2293 |
if (event.key.keysym.scancode == SDL_SCANCODE_LALT || event.key.keysym.scancode == SDL_SCANCODE_RALT) { |
| 2294 |
alt_down = true; |
| 2295 |
} |
| 2296 |
handle_keyboard(true, event.key.keysym.sym, event.key.keysym.scancode); |
| 2297 |
} |
| 2298 |
continue; |
| 2299 |
} |
| 2300 |
if (event.type == SDL_KEYUP) { |
| 2301 |
if (event.key.keysym.scancode == LSHORTCUT_KEY || event.key.keysym.scancode == RSHORTCUT_KEY) { |
| 2302 |
cmd_down = false; |
| 2303 |
} |
| 2304 |
if (event.key.keysym.scancode == SDL_SCANCODE_LALT || event.key.keysym.scancode == SDL_SCANCODE_RALT) { |
| 2305 |
alt_down = false; |
| 2306 |
} |
| 2307 |
handle_keyboard(false, event.key.keysym.sym, event.key.keysym.scancode); |
| 2308 |
continue; |
| 2309 |
} |
| 2310 |
if (event.type == SDL_MOUSEBUTTONDOWN) { |
| 2311 |
switch (event.button.button) { |
| 2312 |
case SDL_BUTTON_LEFT: |
| 2313 |
mouse_button_down(0); |
| 2314 |
mouse_changed = true; |
| 2315 |
break; |
| 2316 |
case SDL_BUTTON_RIGHT: |
| 2317 |
mouse_button_down(1); |
| 2318 |
mouse_changed = true; |
| 2319 |
break; |
| 2320 |
case SDL_BUTTON_MIDDLE: |
| 2321 |
mouse_button_down(2); |
| 2322 |
mouse_changed = true; |
| 2323 |
break; |
| 2324 |
} |
| 2325 |
} |
| 2326 |
if (event.type == SDL_MOUSEBUTTONUP) { |
| 2327 |
switch (event.button.button) { |
| 2328 |
case SDL_BUTTON_LEFT: |
| 2329 |
mouse_button_up(0); |
| 2330 |
mouse_changed = true; |
| 2331 |
break; |
| 2332 |
case SDL_BUTTON_RIGHT: |
| 2333 |
mouse_button_up(1); |
| 2334 |
mouse_changed = true; |
| 2335 |
break; |
| 2336 |
case SDL_BUTTON_MIDDLE: |
| 2337 |
mouse_button_up(2); |
| 2338 |
mouse_changed = true; |
| 2339 |
break; |
| 2340 |
} |
| 2341 |
} |
| 2342 |
if (event.type == SDL_MOUSEMOTION) { |
| 2343 |
static int mouse_x; |
| 2344 |
static int mouse_y; |
| 2345 |
if (mouse_grabbed) { |
| 2346 |
mouse_move(event.motion.xrel, event.motion.yrel); |
| 2347 |
} else { |
| 2348 |
mouse_move(event.motion.x - mouse_x, event.motion.y - mouse_y); |
| 2349 |
} |
| 2350 |
mouse_x = event.motion.x; |
| 2351 |
mouse_y = event.motion.y; |
| 2352 |
mouse_changed = true; |
| 2353 |
} |
| 2354 |
if (event.type == SDL_MOUSEWHEEL) { |
| 2355 |
mouse_set_wheel(event.wheel.y); |
| 2356 |
mouse_changed = true; |
| 2357 |
} |
| 2358 |
|
| 2359 |
if (event.type == SDL_JOYDEVICEADDED) { |
| 2360 |
joystick_add(event.jdevice.which); |
| 2361 |
} |
| 2362 |
if (event.type == SDL_JOYDEVICEREMOVED) { |
| 2363 |
joystick_remove(event.jdevice.which); |
| 2364 |
} |
| 2365 |
if (event.type == SDL_CONTROLLERBUTTONDOWN) { |
| 2366 |
joystick_button_down(event.cbutton.which, event.cbutton.button); |
| 2367 |
} |
| 2368 |
if (event.type == SDL_CONTROLLERBUTTONUP) { |
| 2369 |
joystick_button_up(event.cbutton.which, event.cbutton.button); |
| 2370 |
} |
| 2371 |
|
| 2372 |
} |
| 2373 |
if (mouse_changed) { |
| 2374 |
mouse_send_state(); |
| 2375 |
} |
| 2376 |
return true; |
| 2377 |
} |
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
bool |
| 2385 |
video_get_layer_line_state(uint8_t layer, uint16_t line, uint8_t out_regs[7], |
| 2386 |
uint16_t *out_eff_y, bool *out_enabled, |
| 2387 |
uint16_t *out_layer_row) |
| 2388 |
{ |
| 2389 |
if (layer >= 2 || line >= SCREEN_HEIGHT || !line_state_valid[line]) { |
| 2390 |
return false; |
| 2391 |
} |
| 2392 |
if (out_regs) { |
| 2393 |
memcpy(out_regs, line_reg_layer[line][layer], 7); |
| 2394 |
} |
| 2395 |
if (out_eff_y) { |
| 2396 |
*out_eff_y = line_eff_y[line]; |
| 2397 |
} |
| 2398 |
if (out_enabled) { |
| 2399 |
*out_enabled = (line_layer_enabled[line] & (1 << layer)) != 0; |
| 2400 |
} |
| 2401 |
if (out_layer_row) { |
| 2402 |
*out_layer_row = line_layer_row[line][layer]; |
| 2403 |
} |
| 2404 |
return true; |
| 2405 |
} |
| 2406 |
|
| 2407 |
uint16_t |
| 2408 |
video_get_scanline_count(void) |
| 2409 |
{ |
| 2410 |
return SCREEN_HEIGHT; |
| 2411 |
} |
| 2412 |
|
| 2413 |
uint32_t |
| 2414 |
video_frame_dot_clocks(void) |
| 2415 |
{ |
| 2416 |
const bool ntsc_mode = reg_composer[0] & 2; |
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
return ntsc_mode ? (uint32_t)NTSC_HALF_SCAN_WIDTH * SCAN_HEIGHT |
| 2421 |
: (uint32_t)VGA_SCAN_WIDTH * SCAN_HEIGHT; |
| 2422 |
} |
| 2423 |
|
| 2424 |
uint16_t |
| 2425 |
video_frame_scanlines(void) |
| 2426 |
{ |
| 2427 |
return SCAN_HEIGHT; |
| 2428 |
} |
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
|
| 2435 |
void |
| 2436 |
video_get_active_layer_size(int *out_w, int *out_h) |
| 2437 |
{ |
| 2438 |
int hstart = reg_composer[4] << 2; |
| 2439 |
int hstop = reg_composer[5] << 2; |
| 2440 |
int vstart = reg_composer[6] << 1; |
| 2441 |
int vstop = reg_composer[7] << 1; |
| 2442 |
|
| 2443 |
if (hstart > SCREEN_WIDTH) hstart = SCREEN_WIDTH; |
| 2444 |
if (hstop > SCREEN_WIDTH) hstop = SCREEN_WIDTH; |
| 2445 |
if (vstart > SCREEN_HEIGHT) vstart = SCREEN_HEIGHT; |
| 2446 |
if (vstop > SCREEN_HEIGHT) vstop = SCREEN_HEIGHT; |
| 2447 |
|
| 2448 |
const int screen_w = hstop > hstart ? hstop - hstart : 0; |
| 2449 |
const int screen_h = vstop > vstart ? vstop - vstart : 0; |
| 2450 |
|
| 2451 |
if (out_w) { |
| 2452 |
*out_w = (screen_w * reg_composer[1]) >> 7; |
| 2453 |
} |
| 2454 |
if (out_h) { |
| 2455 |
*out_h = (screen_h * reg_composer[2]) >> 7; |
| 2456 |
} |
| 2457 |
} |
| 2458 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
bool |
| 2468 |
video_next_irq(float mhz, uint32_t *out_cycles, uint8_t *out_source) |
| 2469 |
{ |
| 2470 |
const bool ntsc_mode = reg_composer[0] & 2; |
| 2471 |
const bool vsync_en = (ien & 1) != 0; |
| 2472 |
const bool line_en = (ien & 2) != 0; |
| 2473 |
|
| 2474 |
if (!vsync_en && !line_en) { |
| 2475 |
return false; |
| 2476 |
} |
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
const int cur = ntsc_mode ? (int)(ntsc_scan_pos_y % SCAN_HEIGHT) - NTSC_Y_OFFSET_LOW |
| 2481 |
: (int)vga_scan_pos_y - VGA_Y_OFFSET; |
| 2482 |
|
| 2483 |
|
| 2484 |
#define LINES_UNTIL(target) \ |
| 2485 |
((((int)(target) - cur) + SCAN_HEIGHT) % SCAN_HEIGHT) |
| 2486 |
|
| 2487 |
int best_lines = SCAN_HEIGHT + 1; |
| 2488 |
uint8_t best_source = 0; |
| 2489 |
|
| 2490 |
if (vsync_en) { |
| 2491 |
const int d = LINES_UNTIL(SCREEN_HEIGHT); |
| 2492 |
if (d < best_lines) { |
| 2493 |
best_lines = d; |
| 2494 |
best_source = 1; |
| 2495 |
} |
| 2496 |
} |
| 2497 |
if (line_en) { |
| 2498 |
const int d = LINES_UNTIL(irq_line); |
| 2499 |
if (d < best_lines) { |
| 2500 |
best_lines = d; |
| 2501 |
best_source = 2; |
| 2502 |
} |
| 2503 |
} |
| 2504 |
#undef LINES_UNTIL |
| 2505 |
|
| 2506 |
if (!best_source) { |
| 2507 |
return false; |
| 2508 |
} |
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 2512 |
|
| 2513 |
const float scan_width = ntsc_mode ? NTSC_HALF_SCAN_WIDTH : VGA_SCAN_WIDTH; |
| 2514 |
const float cycles_per_line = scan_width * mhz / (float)PIXEL_FREQ; |
| 2515 |
const float into_line = ntsc_mode ? ntsc_half_cnt : vga_scan_pos_x; |
| 2516 |
float cycles = best_lines * cycles_per_line |
| 2517 |
- into_line * mhz / (float)PIXEL_FREQ; |
| 2518 |
if (cycles < 0) { |
| 2519 |
cycles = 0; |
| 2520 |
} |
| 2521 |
|
| 2522 |
if (out_cycles) { |
| 2523 |
*out_cycles = (uint32_t)cycles; |
| 2524 |
} |
| 2525 |
if (out_source) { |
| 2526 |
*out_source = best_source; |
| 2527 |
} |
| 2528 |
return true; |
| 2529 |
} |
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
void |
| 2534 |
video_get_irq_state(uint8_t *out_ien, uint8_t *out_isr, uint16_t *out_irq_line) |
| 2535 |
{ |
| 2536 |
if (out_ien) |
| 2537 |
*out_ien = ien; |
| 2538 |
if (out_isr) |
| 2539 |
*out_isr = isr | (pcm_is_fifo_almost_empty() ? 8 : 0); |
| 2540 |
if (out_irq_line) |
| 2541 |
*out_irq_line = irq_line; |
| 2542 |
} |
| 2543 |
|
| 2544 |
void |
| 2545 |
video_end() |
| 2546 |
{ |
| 2547 |
if (debugger_enabled) { |
| 2548 |
DEBUGFreeUI(); |
| 2549 |
} |
| 2550 |
|
| 2551 |
#ifdef HAS_IMGUI |
| 2552 |
|
| 2553 |
|
| 2554 |
|
| 2555 |
if (imgui_window && imgui_renderer) { |
| 2556 |
debug_ui_shutdown(); |
| 2557 |
} |
| 2558 |
if (imgui_renderer) { |
| 2559 |
SDL_DestroyRenderer(imgui_renderer); |
| 2560 |
imgui_renderer = NULL; |
| 2561 |
} |
| 2562 |
if (imgui_window) { |
| 2563 |
#ifdef _WIN32 |
| 2564 |
extern void video_win32_remove_move_hook(SDL_Window *window); |
| 2565 |
video_win32_remove_move_hook(imgui_window); |
| 2566 |
#endif |
| 2567 |
SDL_DestroyWindow(imgui_window); |
| 2568 |
imgui_window = NULL; |
| 2569 |
} |
| 2570 |
#endif |
| 2571 |
|
| 2572 |
if (record_gif != RECORD_GIF_DISABLED) { |
| 2573 |
GifEnd(&gif_writer); |
| 2574 |
record_gif = RECORD_GIF_DISABLED; |
| 2575 |
} |
| 2576 |
|
| 2577 |
is_fullscreen = false; |
| 2578 |
SDL_SetWindowFullscreen(window, 0); |
| 2579 |
SDL_DelEventWatch(video_move_resize_watch, NULL); |
| 2580 |
#ifdef _WIN32 |
| 2581 |
extern void video_win32_remove_move_hook(SDL_Window *window); |
| 2582 |
video_win32_remove_move_hook(window); |
| 2583 |
#endif |
| 2584 |
SDL_DestroyRenderer(renderer); |
| 2585 |
SDL_DestroyWindow(window); |
| 2586 |
} |
| 2587 |
|
| 2588 |
|
| 2589 |
static const int increments[32] = { |
| 2590 |
0, 0, |
| 2591 |
1, -1, |
| 2592 |
2, -2, |
| 2593 |
4, -4, |
| 2594 |
8, -8, |
| 2595 |
16, -16, |
| 2596 |
32, -32, |
| 2597 |
64, -64, |
| 2598 |
128, -128, |
| 2599 |
256, -256, |
| 2600 |
512, -512, |
| 2601 |
40, -40, |
| 2602 |
80, -80, |
| 2603 |
160, -160, |
| 2604 |
320, -320, |
| 2605 |
640, -640, |
| 2606 |
}; |
| 2607 |
|
| 2608 |
uint32_t |
| 2609 |
video_get_address(uint8_t sel) |
| 2610 |
{ |
| 2611 |
uint32_t address = io_addr[sel]; |
| 2612 |
return address & 0x1ffff; |
| 2613 |
} |
| 2614 |
|
| 2615 |
uint32_t |
| 2616 |
get_and_inc_address(uint8_t sel, bool write) |
| 2617 |
{ |
| 2618 |
uint32_t address = io_addr[sel]; |
| 2619 |
int16_t incr = increments[io_inc[sel]]; |
| 2620 |
|
| 2621 |
if (fx_4bit_mode && fx_nibble_incr[sel] && !incr) { |
| 2622 |
if (fx_nibble_bit[sel]) { |
| 2623 |
if ((io_inc[sel] & 1) == 0) io_addr[sel] += 1; |
| 2624 |
fx_nibble_bit[sel] = 0; |
| 2625 |
} else { |
| 2626 |
if (io_inc[sel] & 1) io_addr[sel] -= 1; |
| 2627 |
fx_nibble_bit[sel] = 1; |
| 2628 |
} |
| 2629 |
} |
| 2630 |
|
| 2631 |
if (sel == 1 && fx_16bit_hop) { |
| 2632 |
if (incr == 4) { |
| 2633 |
if (fx_16bit_hop_align == (address & 0x3)) |
| 2634 |
incr = 1; |
| 2635 |
else |
| 2636 |
incr = 3; |
| 2637 |
} else if (incr == 320) { |
| 2638 |
if (fx_16bit_hop_align == (address & 0x3)) |
| 2639 |
incr = 1; |
| 2640 |
else |
| 2641 |
incr = 319; |
| 2642 |
} |
| 2643 |
} |
| 2644 |
|
| 2645 |
io_addr[sel] += incr; |
| 2646 |
|
| 2647 |
if (sel == 1 && fx_addr1_mode == 1) { |
| 2648 |
fx_x_pixel_position += fx_x_pixel_increment; |
| 2649 |
if (fx_x_pixel_position & 0x10000) { |
| 2650 |
fx_x_pixel_position &= ~0x10000; |
| 2651 |
if (fx_4bit_mode && fx_nibble_incr[0]) { |
| 2652 |
if (fx_nibble_bit[1]) { |
| 2653 |
if ((io_inc[0] & 1) == 0) io_addr[1] += 1; |
| 2654 |
fx_nibble_bit[1] = 0; |
| 2655 |
} else { |
| 2656 |
if (io_inc[0] & 1) io_addr[1] -= 1; |
| 2657 |
fx_nibble_bit[1] = 1; |
| 2658 |
} |
| 2659 |
} |
| 2660 |
io_addr[1] += increments[io_inc[0]]; |
| 2661 |
} |
| 2662 |
} else if (fx_addr1_mode == 2 && write == false) { |
| 2663 |
fx_x_pixel_position += fx_x_pixel_increment; |
| 2664 |
fx_y_pixel_position += fx_y_pixel_increment; |
| 2665 |
fx_poly_fill_length = ((int32_t) fx_y_pixel_position >> 16) - ((int32_t) fx_x_pixel_position >> 16); |
| 2666 |
if (sel == 0 && fx_cache_byte_cycling && !fx_cache_fill) { |
| 2667 |
fx_cache_byte_index = (fx_cache_byte_index + 1) & 3; |
| 2668 |
} |
| 2669 |
if (sel == 1) { |
| 2670 |
if (fx_4bit_mode) { |
| 2671 |
io_addr[1] = io_addr[0] + (fx_x_pixel_position >> 17); |
| 2672 |
fx_nibble_bit[1] = (fx_x_pixel_position >> 16) & 1; |
| 2673 |
} else { |
| 2674 |
io_addr[1] = io_addr[0] + (fx_x_pixel_position >> 16); |
| 2675 |
} |
| 2676 |
} |
| 2677 |
} else if (sel == 1 && fx_addr1_mode == 3 && write == false) { |
| 2678 |
fx_x_pixel_position += fx_x_pixel_increment; |
| 2679 |
fx_y_pixel_position += fx_y_pixel_increment; |
| 2680 |
} |
| 2681 |
return address; |
| 2682 |
} |
| 2683 |
|
| 2684 |
void |
| 2685 |
fx_affine_prefetch(void) |
| 2686 |
{ |
| 2687 |
if (fx_addr1_mode != 3) return; |
| 2688 |
|
| 2689 |
uint32_t address; |
| 2690 |
uint8_t affine_x_tile = (fx_x_pixel_position >> 19) & 0xff; |
| 2691 |
uint8_t affine_y_tile = (fx_y_pixel_position >> 19) & 0xff; |
| 2692 |
uint8_t affine_x_sub_tile = (fx_x_pixel_position >> 16) & 0x07; |
| 2693 |
uint8_t affine_y_sub_tile = (fx_y_pixel_position >> 16) & 0x07; |
| 2694 |
|
| 2695 |
if (!fx_affine_clip) { |
| 2696 |
affine_x_tile &= fx_affine_map_size - 1; |
| 2697 |
affine_y_tile &= fx_affine_map_size - 1; |
| 2698 |
} |
| 2699 |
|
| 2700 |
if (affine_x_tile >= fx_affine_map_size || affine_y_tile >= fx_affine_map_size) { |
| 2701 |
|
| 2702 |
address = fx_affine_tile_base + (affine_y_sub_tile << (3 - fx_4bit_mode)) + (affine_x_sub_tile >> (uint8_t)fx_4bit_mode); |
| 2703 |
fx_nibble_bit[1] = (affine_x_sub_tile & 1) >> (1 - fx_4bit_mode); |
| 2704 |
} else { |
| 2705 |
|
| 2706 |
address = fx_affine_map_base + (affine_y_tile * fx_affine_map_size) + affine_x_tile; |
| 2707 |
|
| 2708 |
uint8_t affine_tile_idx = video_space_read(address); |
| 2709 |
address = fx_affine_tile_base + (affine_tile_idx << (6 - fx_4bit_mode)); |
| 2710 |
|
| 2711 |
address += (affine_y_sub_tile << (3 - fx_4bit_mode)) + (affine_x_sub_tile >> (uint8_t)fx_4bit_mode); |
| 2712 |
fx_nibble_bit[1] = (affine_x_sub_tile & 1) >> (1 - fx_4bit_mode); |
| 2713 |
} |
| 2714 |
io_addr[1] = address; |
| 2715 |
io_rddata[1] = video_space_read(address); |
| 2716 |
} |
| 2717 |
|
| 2718 |
|
| 2719 |
|
| 2720 |
|
| 2721 |
|
| 2722 |
uint8_t |
| 2723 |
video_space_read(uint32_t address) |
| 2724 |
{ |
| 2725 |
return video_ram[address & 0x1FFFF]; |
| 2726 |
} |
| 2727 |
|
| 2728 |
static void |
| 2729 |
video_space_read_range(uint8_t* dest, uint32_t address, uint32_t size) |
| 2730 |
{ |
| 2731 |
if (address >= ADDR_VRAM_START && (address+size) <= ADDR_VRAM_END) { |
| 2732 |
memcpy(dest, &video_ram[address], size); |
| 2733 |
} else { |
| 2734 |
for(int i = 0; i < size; ++i) { |
| 2735 |
*dest++ = video_space_read(address + i); |
| 2736 |
} |
| 2737 |
} |
| 2738 |
} |
| 2739 |
|
| 2740 |
void |
| 2741 |
video_space_write(uint32_t address, uint8_t value) |
| 2742 |
{ |
| 2743 |
video_ram[address & 0x1FFFF] = value; |
| 2744 |
|
| 2745 |
if (address >= ADDR_PSG_START && address < ADDR_PSG_END) { |
| 2746 |
audio_render(); |
| 2747 |
psg_writereg(address & 0x3f, value); |
| 2748 |
} else if (address >= ADDR_PALETTE_START && address < ADDR_PALETTE_END) { |
| 2749 |
palette[address & 0x1ff] = value; |
| 2750 |
video_palette.dirty = true; |
| 2751 |
} else if (address >= ADDR_SPRDATA_START && address < ADDR_SPRDATA_END) { |
| 2752 |
sprite_attr_write(address, value); |
| 2753 |
} |
| 2754 |
} |
| 2755 |
|
| 2756 |
|
| 2757 |
void |
| 2758 |
fx_video_space_write(uint32_t address, bool nibble, uint8_t value) |
| 2759 |
{ |
| 2760 |
if (fx_4bit_mode) { |
| 2761 |
if (nibble) { |
| 2762 |
if (!fx_trans_writes || (value & 0x0f) > 0) { |
| 2763 |
video_ram[address & 0x1FFFF] = (video_ram[address & 0x1FFFF] & 0xf0) | (value & 0x0f); |
| 2764 |
} |
| 2765 |
} else { |
| 2766 |
if (!fx_trans_writes || (value & 0xf0) > 0) { |
| 2767 |
video_ram[address & 0x1FFFF] = (video_ram[address & 0x1FFFF] & 0x0f) | (value & 0xf0); |
| 2768 |
} |
| 2769 |
} |
| 2770 |
} else { |
| 2771 |
if (!fx_trans_writes || value > 0) video_ram[address & 0x1FFFF] = value; |
| 2772 |
} |
| 2773 |
if (address >= ADDR_PSG_START && address < ADDR_PSG_END) { |
| 2774 |
audio_render(); |
| 2775 |
psg_writereg(address & 0x3f, value); |
| 2776 |
} else if (address >= ADDR_PALETTE_START && address < ADDR_PALETTE_END) { |
| 2777 |
palette[address & 0x1ff] = value; |
| 2778 |
video_palette.dirty = true; |
| 2779 |
} else if (address >= ADDR_SPRDATA_START && address < ADDR_SPRDATA_END) { |
| 2780 |
sprite_attr_write(address, value); |
| 2781 |
} |
| 2782 |
} |
| 2783 |
|
| 2784 |
void |
| 2785 |
fx_vram_cache_write(uint32_t address, uint8_t value, uint8_t mask) |
| 2786 |
{ |
| 2787 |
if (!fx_trans_writes || value > 0) { |
| 2788 |
switch (mask) { |
| 2789 |
case 0: |
| 2790 |
video_ram[address & 0x1FFFF] = value; |
| 2791 |
break; |
| 2792 |
case 1: |
| 2793 |
video_ram[address & 0x1FFFF] = (video_ram[address & 0x1FFFF] & 0x0f) | (value & 0xf0); |
| 2794 |
break; |
| 2795 |
case 2: |
| 2796 |
video_ram[address & 0x1FFFF] = (video_ram[address & 0x1FFFF] & 0xf0) | (value & 0x0f); |
| 2797 |
break; |
| 2798 |
case 3: |
| 2799 |
|
| 2800 |
break; |
| 2801 |
} |
| 2802 |
} |
| 2803 |
} |
| 2804 |
|
| 2805 |
uint32_t video_get_fx_accum() { |
| 2806 |
return fx_mult_accumulator; |
| 2807 |
} |
| 2808 |
uint8_t video_get_dc_value(uint8_t reg) { |
| 2809 |
switch (reg & 0x1F) { |
| 2810 |
case 0x00: |
| 2811 |
case 0x01: |
| 2812 |
case 0x02: |
| 2813 |
case 0x03: |
| 2814 |
case 0x04: |
| 2815 |
case 0x05: |
| 2816 |
case 0x06: |
| 2817 |
case 0x07: |
| 2818 |
case 0x08: |
| 2819 |
case 0x09: |
| 2820 |
case 0x0a: |
| 2821 |
case 0x0c: |
| 2822 |
case 0x0d: |
| 2823 |
case 0x0e: |
| 2824 |
case 0x0f: |
| 2825 |
return reg_composer[reg]; |
| 2826 |
break; |
| 2827 |
case 0x0b: |
| 2828 |
return reg_composer[reg] & 0x3f; |
| 2829 |
break; |
| 2830 |
case 0x10: |
| 2831 |
return (fx_x_pixel_position >> 16) & 0xff; |
| 2832 |
break; |
| 2833 |
case 0x11: |
| 2834 |
return ((fx_x_pixel_position >> 24) & 0x07) | (fx_x_pixel_position & 0x80); |
| 2835 |
break; |
| 2836 |
case 0x12: |
| 2837 |
return (fx_y_pixel_position >> 16) & 0xff; |
| 2838 |
break; |
| 2839 |
case 0x13: |
| 2840 |
return ((fx_y_pixel_position >> 24) & 0x07) | (fx_y_pixel_position & 0x80); |
| 2841 |
break; |
| 2842 |
case 0x14: |
| 2843 |
return (fx_x_pixel_position >> 8) & 0xff; |
| 2844 |
break; |
| 2845 |
case 0x15: |
| 2846 |
return (fx_y_pixel_position >> 8) & 0xff; |
| 2847 |
break; |
| 2848 |
case 0x16: |
| 2849 |
if (fx_poly_fill_length >= 768) { |
| 2850 |
return ((fx_2bit_poly && fx_addr1_mode == 2) ? 0x00 : 0x80); |
| 2851 |
} |
| 2852 |
if (fx_4bit_mode) { |
| 2853 |
if (fx_2bit_poly && fx_addr1_mode == 2) { |
| 2854 |
return ((fx_y_pixel_position & 0x00008000) >> 8) | |
| 2855 |
((fx_x_pixel_position >> 11) & 0x60) | |
| 2856 |
((fx_x_pixel_position >> 14) & 0x10) | |
| 2857 |
((fx_poly_fill_length & 0x0007) << 1) | |
| 2858 |
((fx_x_pixel_position & 0x00008000) >> 15); |
| 2859 |
} else { |
| 2860 |
return ((!!(fx_poly_fill_length & 0xfff8)) << 7) | |
| 2861 |
((fx_x_pixel_position >> 11) & 0x60) | |
| 2862 |
((fx_x_pixel_position >> 14) & 0x10) | |
| 2863 |
((fx_poly_fill_length & 0x0007) << 1); |
| 2864 |
} |
| 2865 |
} else { |
| 2866 |
return ((!!(fx_poly_fill_length & 0xfff0)) << 7) | |
| 2867 |
((fx_x_pixel_position >> 11) & 0x60) | |
| 2868 |
((fx_poly_fill_length & 0x000f) << 1); |
| 2869 |
} |
| 2870 |
break; |
| 2871 |
case 0x17: |
| 2872 |
return ((fx_poly_fill_length & 0x03f8) >> 2); |
| 2873 |
break; |
| 2874 |
case 0x18: |
| 2875 |
return fx_cache[0]; |
| 2876 |
break; |
| 2877 |
case 0x19: |
| 2878 |
return fx_cache[1]; |
| 2879 |
break; |
| 2880 |
case 0x1a: |
| 2881 |
return fx_cache[2]; |
| 2882 |
break; |
| 2883 |
case 0x1b: |
| 2884 |
return fx_cache[3]; |
| 2885 |
break; |
| 2886 |
default: |
| 2887 |
break; |
| 2888 |
} |
| 2889 |
|
| 2890 |
return vera_version_string[reg % 4]; |
| 2891 |
} |
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
|
| 2896 |
|
| 2897 |
|
| 2898 |
static void check_not_readonly(uint8_t reg) { |
| 2899 |
bool wrong = false; |
| 2900 |
switch(io_dcsel) { |
| 2901 |
case 5: { |
| 2902 |
switch(reg) { |
| 2903 |
case 0x0b: |
| 2904 |
case 0x0c: |
| 2905 |
wrong=true; |
| 2906 |
break; |
| 2907 |
} |
| 2908 |
break; |
| 2909 |
} |
| 2910 |
case 63: { |
| 2911 |
switch(reg) { |
| 2912 |
case 0x09: |
| 2913 |
case 0x0a: |
| 2914 |
case 0x0b: |
| 2915 |
case 0x0c: |
| 2916 |
wrong=true; |
| 2917 |
break; |
| 2918 |
} |
| 2919 |
break; |
| 2920 |
} |
| 2921 |
} |
| 2922 |
|
| 2923 |
if(wrong) |
| 2924 |
printf("Warning: %04X wrote to read-only VERA register at 9F%02X (DCSEL=%d)\n", opcode_addr, reg+0x20, io_dcsel); |
| 2925 |
} |
| 2926 |
|
| 2927 |
static void check_not_writeonly(uint8_t reg) { |
| 2928 |
bool wrong = false; |
| 2929 |
switch(io_dcsel) { |
| 2930 |
case 2: { |
| 2931 |
switch(reg) { |
| 2932 |
case 0x0a: |
| 2933 |
case 0x0b: |
| 2934 |
case 0x0c: |
| 2935 |
wrong=true; |
| 2936 |
break; |
| 2937 |
} |
| 2938 |
break; |
| 2939 |
} |
| 2940 |
case 3: |
| 2941 |
case 4: { |
| 2942 |
switch(reg) { |
| 2943 |
case 0x09: |
| 2944 |
case 0x0a: |
| 2945 |
case 0x0b: |
| 2946 |
case 0x0c: |
| 2947 |
wrong=true; |
| 2948 |
break; |
| 2949 |
} |
| 2950 |
break; |
| 2951 |
} |
| 2952 |
case 5: { |
| 2953 |
switch(reg) { |
| 2954 |
case 0x09: |
| 2955 |
case 0x0a: |
| 2956 |
wrong=true; |
| 2957 |
break; |
| 2958 |
} |
| 2959 |
break; |
| 2960 |
} |
| 2961 |
case 6: { |
| 2962 |
switch(reg) { |
| 2963 |
case 0x0b: |
| 2964 |
case 0x0c: |
| 2965 |
wrong=true; |
| 2966 |
break; |
| 2967 |
} |
| 2968 |
break; |
| 2969 |
} |
| 2970 |
} |
| 2971 |
|
| 2972 |
if(wrong) |
| 2973 |
printf("Warning: %04X read from write-only VERA register at 9F%02X (DCSEL=%d)\n", opcode_addr, reg+0x20, io_dcsel); |
| 2974 |
} |
| 2975 |
|
| 2976 |
uint8_t video_read(uint8_t reg, bool debugOn) { |
| 2977 |
bool ntsc_mode = reg_composer[0] & 2; |
| 2978 |
uint16_t scanline = ntsc_mode ? ntsc_scan_pos_y % SCAN_HEIGHT : vga_scan_pos_y; |
| 2979 |
if (scanline >= 512) scanline=511; |
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
|
| 2984 |
|
| 2985 |
if (!debugOn) { |
| 2986 |
check_not_writeonly(reg); |
| 2987 |
} |
| 2988 |
|
| 2989 |
switch (reg & 0x1F) { |
| 2990 |
case 0x00: return io_addr[io_addrsel] & 0xff; |
| 2991 |
case 0x01: return (io_addr[io_addrsel] >> 8) & 0xff; |
| 2992 |
case 0x02: return (io_addr[io_addrsel] >> 16) | (fx_nibble_bit[io_addrsel] << 1) | (fx_nibble_incr[io_addrsel] << 2) | (io_inc[io_addrsel] << 3); |
| 2993 |
case 0x03: |
| 2994 |
case 0x04: { |
| 2995 |
if (debugOn) { |
| 2996 |
return io_rddata[reg - 3]; |
| 2997 |
} |
| 2998 |
|
| 2999 |
bool addr_nibble = fx_nibble_bit[reg - 3]; |
| 3000 |
uint32_t address = get_and_inc_address(reg - 3, false); |
| 3001 |
|
| 3002 |
uint8_t value = io_rddata[reg - 3]; |
| 3003 |
|
| 3004 |
|
| 3005 |
|
| 3006 |
|
| 3007 |
|
| 3008 |
note_port_traffic(false, 1); |
| 3009 |
|
| 3010 |
if (reg == 4 && fx_addr1_mode == 3) { |
| 3011 |
|
| 3012 |
|
| 3013 |
note_port_fx_affine(); |
| 3014 |
fx_affine_prefetch(); |
| 3015 |
} else |
| 3016 |
io_rddata[reg - 3] = video_space_read(io_addr[reg - 3]); |
| 3017 |
|
| 3018 |
if (fx_cache_fill) { |
| 3019 |
if (fx_4bit_mode) { |
| 3020 |
uint8_t nibble_read = (addr_nibble ? ((value & 0x0f) << 4) : (value & 0xf0)); |
| 3021 |
|
| 3022 |
if (fx_cache_nibble_index) { |
| 3023 |
fx_cache[fx_cache_byte_index] = (fx_cache[fx_cache_byte_index] & 0xf0) | (nibble_read >> 4); |
| 3024 |
fx_cache_nibble_index = 0; |
| 3025 |
fx_cache_byte_index = ((fx_cache_byte_index + 1) & 0x3); |
| 3026 |
} else { |
| 3027 |
fx_cache[fx_cache_byte_index] = (fx_cache[fx_cache_byte_index] & 0x0f) | (nibble_read); |
| 3028 |
fx_cache_nibble_index = 1; |
| 3029 |
} |
| 3030 |
} else { |
| 3031 |
fx_cache[fx_cache_byte_index] = value; |
| 3032 |
if (fx_cache_increment_mode) |
| 3033 |
fx_cache_byte_index = (fx_cache_byte_index & 0x2) | ((fx_cache_byte_index + 1) & 0x1); |
| 3034 |
else |
| 3035 |
fx_cache_byte_index = ((fx_cache_byte_index + 1) & 0x3); |
| 3036 |
} |
| 3037 |
} |
| 3038 |
|
| 3039 |
if (log_video) { |
| 3040 |
printf("READ video_space[$%X] = $%02X\n", address, value); |
| 3041 |
} |
| 3042 |
return value; |
| 3043 |
} |
| 3044 |
case 0x05: return (io_dcsel << 1) | io_addrsel; |
| 3045 |
case 0x06: return ((irq_line & 0x100) >> 1) | ((scanline & 0x100) >> 2) | (ien & 0xF); |
| 3046 |
case 0x07: return isr | (pcm_is_fifo_almost_empty() ? 8 : 0); |
| 3047 |
case 0x08: return scanline & 0xFF; |
| 3048 |
|
| 3049 |
case 0x09: |
| 3050 |
case 0x0A: |
| 3051 |
case 0x0B: |
| 3052 |
case 0x0C: { |
| 3053 |
int i = reg - 0x09 + (io_dcsel << 2); |
| 3054 |
if (debugOn) return video_get_dc_value(i); |
| 3055 |
switch (i) { |
| 3056 |
case 0x00: |
| 3057 |
case 0x01: |
| 3058 |
case 0x02: |
| 3059 |
case 0x03: |
| 3060 |
case 0x04: |
| 3061 |
case 0x05: |
| 3062 |
case 0x06: |
| 3063 |
case 0x07: |
| 3064 |
case 0x08: |
| 3065 |
case 0x16: |
| 3066 |
case 0x17: |
| 3067 |
return video_get_dc_value(i); |
| 3068 |
break; |
| 3069 |
case 0x18: |
| 3070 |
fx_mult_accumulator = 0; |
| 3071 |
|
| 3072 |
break; |
| 3073 |
case 0x19: |
| 3074 |
; |
| 3075 |
int32_t m_result = (int16_t)((fx_cache[1] << 8) | fx_cache[0]) * (int16_t)((fx_cache[3] << 8) | fx_cache[2]); |
| 3076 |
if (fx_subtract) |
| 3077 |
fx_mult_accumulator -= m_result; |
| 3078 |
else |
| 3079 |
fx_mult_accumulator += m_result; |
| 3080 |
|
| 3081 |
break; |
| 3082 |
default: |
| 3083 |
|
| 3084 |
|
| 3085 |
|
| 3086 |
break; |
| 3087 |
} |
| 3088 |
return vera_version_string[i % 4]; |
| 3089 |
break; |
| 3090 |
} |
| 3091 |
case 0x0D: |
| 3092 |
case 0x0E: |
| 3093 |
case 0x0F: |
| 3094 |
case 0x10: |
| 3095 |
case 0x11: |
| 3096 |
case 0x12: |
| 3097 |
case 0x13: return reg_layer[0][reg - 0x0D]; |
| 3098 |
|
| 3099 |
case 0x14: |
| 3100 |
case 0x15: |
| 3101 |
case 0x16: |
| 3102 |
case 0x17: |
| 3103 |
case 0x18: |
| 3104 |
case 0x19: |
| 3105 |
case 0x1A: return reg_layer[1][reg - 0x14]; |
| 3106 |
|
| 3107 |
case 0x1B: |
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
if (!debugOn) { |
| 3112 |
audio_render(); |
| 3113 |
} |
| 3114 |
return pcm_read_ctrl(); |
| 3115 |
case 0x1C: return pcm_read_rate(); |
| 3116 |
case 0x1D: return 0; |
| 3117 |
|
| 3118 |
case 0x1E: |
| 3119 |
case 0x1F: |
| 3120 |
|
| 3121 |
|
| 3122 |
|
| 3123 |
|
| 3124 |
if (debugOn) { |
| 3125 |
return vera_spi_peek(reg & 1); |
| 3126 |
} |
| 3127 |
return vera_spi_read(reg & 1); |
| 3128 |
} |
| 3129 |
return 0; |
| 3130 |
} |
| 3131 |
|
| 3132 |
void video_write(uint8_t reg, uint8_t value) { |
| 3133 |
|
| 3134 |
|
| 3135 |
|
| 3136 |
|
| 3137 |
|
| 3138 |
check_not_readonly(reg); |
| 3139 |
|
| 3140 |
switch (reg & 0x1F) { |
| 3141 |
case 0x00: |
| 3142 |
if (fx_2bit_poly && fx_4bit_mode && fx_addr1_mode == 2 && io_addrsel == 1) { |
| 3143 |
fx_2bit_poking = true; |
| 3144 |
io_addr[1] = (io_addr[1] & 0x1fffc) | (value & 0x3); |
| 3145 |
} else { |
| 3146 |
io_addr[io_addrsel] = (io_addr[io_addrsel] & 0x1ff00) | value; |
| 3147 |
if (fx_16bit_hop && io_addrsel == 1) |
| 3148 |
fx_16bit_hop_align = value & 3; |
| 3149 |
} |
| 3150 |
io_rddata[io_addrsel] = video_space_read(io_addr[io_addrsel]); |
| 3151 |
break; |
| 3152 |
case 0x01: |
| 3153 |
io_addr[io_addrsel] = (io_addr[io_addrsel] & 0x100ff) | (value << 8); |
| 3154 |
io_rddata[io_addrsel] = video_space_read(io_addr[io_addrsel]); |
| 3155 |
break; |
| 3156 |
case 0x02: |
| 3157 |
io_addr[io_addrsel] = (io_addr[io_addrsel] & 0x0ffff) | ((value & 0x1) << 16); |
| 3158 |
fx_nibble_bit[io_addrsel] = (value >> 1) & 0x1; |
| 3159 |
fx_nibble_incr[io_addrsel] = (value >> 2) & 0x1; |
| 3160 |
io_inc[io_addrsel] = value >> 3; |
| 3161 |
io_rddata[io_addrsel] = video_space_read(io_addr[io_addrsel]); |
| 3162 |
break; |
| 3163 |
case 0x03: |
| 3164 |
case 0x04: { |
| 3165 |
if (fx_2bit_poking && fx_addr1_mode) { |
| 3166 |
fx_2bit_poking = false; |
| 3167 |
|
| 3168 |
|
| 3169 |
note_port_traffic(true, 1); |
| 3170 |
uint8_t mask = value >> 6; |
| 3171 |
switch (mask) { |
| 3172 |
case 0x00: |
| 3173 |
video_ram[io_addr[1] & 0x1FFFF] = (fx_cache[fx_cache_byte_index] & 0xc0) | (io_rddata[1] & 0x3f); |
| 3174 |
break; |
| 3175 |
case 0x01: |
| 3176 |
video_ram[io_addr[1] & 0x1FFFF] = (fx_cache[fx_cache_byte_index] & 0x30) | (io_rddata[1] & 0xcf); |
| 3177 |
break; |
| 3178 |
case 0x02: |
| 3179 |
video_ram[io_addr[1] & 0x1FFFF] = (fx_cache[fx_cache_byte_index] & 0x0c) | (io_rddata[1] & 0xf3); |
| 3180 |
break; |
| 3181 |
case 0x03: |
| 3182 |
video_ram[io_addr[1] & 0x1FFFF] = (fx_cache[fx_cache_byte_index] & 0x03) | (io_rddata[1] & 0xfc); |
| 3183 |
break; |
| 3184 |
} |
| 3185 |
break; |
| 3186 |
} |
| 3187 |
|
| 3188 |
if (enable_midline) |
| 3189 |
video_step(MHZ, 0, true); |
| 3190 |
bool nibble = fx_nibble_bit[reg - 3]; |
| 3191 |
uint32_t address = get_and_inc_address(reg - 3, true); |
| 3192 |
if (log_video) { |
| 3193 |
printf("WRITE video_space[$%X] = $%02X\n", address, value); |
| 3194 |
} |
| 3195 |
|
| 3196 |
uint8_t wrdata_to_use; |
| 3197 |
uint8_t ram_wrdata[4]; |
| 3198 |
uint8_t nibble_mask[4]; |
| 3199 |
uint8_t cache_to_use[4]; |
| 3200 |
|
| 3201 |
if (fx_multiplier) { |
| 3202 |
int32_t m_result = (int16_t)((fx_cache[1] << 8) | fx_cache[0]) * (int16_t)((fx_cache[3] << 8) | fx_cache[2]); |
| 3203 |
if (fx_subtract) |
| 3204 |
m_result = fx_mult_accumulator - m_result; |
| 3205 |
else |
| 3206 |
m_result = fx_mult_accumulator + m_result; |
| 3207 |
cache_to_use[0] = (m_result) & 0xff; |
| 3208 |
cache_to_use[1] = (m_result >> 8) & 0xff; |
| 3209 |
cache_to_use[2] = (m_result >> 16) & 0xff; |
| 3210 |
cache_to_use[3] = (m_result >> 24) & 0xff; |
| 3211 |
} else { |
| 3212 |
memcpy(cache_to_use, fx_cache, sizeof(fx_cache)); |
| 3213 |
} |
| 3214 |
|
| 3215 |
if (fx_cache_byte_cycling) { |
| 3216 |
wrdata_to_use = fx_cache[fx_cache_byte_index]; |
| 3217 |
} else { |
| 3218 |
wrdata_to_use = value; |
| 3219 |
} |
| 3220 |
|
| 3221 |
if (fx_cache_write && !fx_cache_byte_cycling) { |
| 3222 |
ram_wrdata[0] = cache_to_use[0]; |
| 3223 |
ram_wrdata[1] = cache_to_use[1]; |
| 3224 |
ram_wrdata[2] = cache_to_use[2]; |
| 3225 |
ram_wrdata[3] = cache_to_use[3]; |
| 3226 |
} else { |
| 3227 |
ram_wrdata[0] = wrdata_to_use; |
| 3228 |
ram_wrdata[1] = wrdata_to_use; |
| 3229 |
ram_wrdata[2] = wrdata_to_use; |
| 3230 |
ram_wrdata[3] = wrdata_to_use; |
| 3231 |
} |
| 3232 |
|
| 3233 |
if (fx_cache_write) { |
| 3234 |
address &= 0x1fffc; |
| 3235 |
|
| 3236 |
|
| 3237 |
|
| 3238 |
note_port_traffic(true, 4); |
| 3239 |
note_port_fx_cache_write(); |
| 3240 |
if (fx_trans_writes) { |
| 3241 |
if (fx_4bit_mode) { |
| 3242 |
nibble_mask[0] = (((ram_wrdata[0] & 0xf0) == 0) << 1) | ((ram_wrdata[0] & 0x0f) == 0); |
| 3243 |
nibble_mask[1] = (((ram_wrdata[1] & 0xf0) == 0) << 1) | ((ram_wrdata[1] & 0x0f) == 0); |
| 3244 |
nibble_mask[2] = (((ram_wrdata[2] & 0xf0) == 0) << 1) | ((ram_wrdata[2] & 0x0f) == 0); |
| 3245 |
nibble_mask[3] = (((ram_wrdata[3] & 0xf0) == 0) << 1) | ((ram_wrdata[3] & 0x0f) == 0); |
| 3246 |
} else { |
| 3247 |
nibble_mask[0] = (ram_wrdata[0] != 0) ? 0 : 3; |
| 3248 |
nibble_mask[1] = (ram_wrdata[1] != 0) ? 0 : 3; |
| 3249 |
nibble_mask[2] = (ram_wrdata[2] != 0) ? 0 : 3; |
| 3250 |
nibble_mask[3] = (ram_wrdata[3] != 0) ? 0 : 3; |
| 3251 |
} |
| 3252 |
} else { |
| 3253 |
nibble_mask[0] = value & 0x3; |
| 3254 |
nibble_mask[1] = (value >> 2) & 0x3; |
| 3255 |
nibble_mask[2] = (value >> 4) & 0x3; |
| 3256 |
nibble_mask[3] = (value >> 6) & 0x3; |
| 3257 |
} |
| 3258 |
|
| 3259 |
fx_vram_cache_write(address+0, ram_wrdata[0], nibble_mask[0]); |
| 3260 |
fx_vram_cache_write(address+1, ram_wrdata[1], nibble_mask[1]); |
| 3261 |
fx_vram_cache_write(address+2, ram_wrdata[2], nibble_mask[2]); |
| 3262 |
fx_vram_cache_write(address+3, ram_wrdata[3], nibble_mask[3]); |
| 3263 |
} else { |
| 3264 |
note_port_traffic(true, 1); |
| 3265 |
fx_video_space_write(address, nibble, wrdata_to_use); |
| 3266 |
} |
| 3267 |
|
| 3268 |
io_rddata[reg - 3] = video_space_read(io_addr[reg - 3]); |
| 3269 |
break; |
| 3270 |
} |
| 3271 |
case 0x05: |
| 3272 |
if (value & 0x80) { |
| 3273 |
video_reset(); |
| 3274 |
} |
| 3275 |
io_dcsel = (value >> 1) & 0x3f; |
| 3276 |
io_addrsel = value & 1; |
| 3277 |
break; |
| 3278 |
case 0x06: |
| 3279 |
irq_line = (irq_line & 0xFF) | ((value >> 7) << 8); |
| 3280 |
ien = value & 0xF; |
| 3281 |
break; |
| 3282 |
case 0x07: |
| 3283 |
isr &= value ^ 0xff; |
| 3284 |
break; |
| 3285 |
case 0x08: |
| 3286 |
irq_line = (irq_line & 0x100) | value; |
| 3287 |
break; |
| 3288 |
|
| 3289 |
case 0x09: |
| 3290 |
case 0x0A: |
| 3291 |
case 0x0B: |
| 3292 |
case 0x0C: { |
| 3293 |
video_step(MHZ, 0, true); |
| 3294 |
int i = reg - 0x09 + (io_dcsel << 2); |
| 3295 |
if (i == 0) { |
| 3296 |
|
| 3297 |
|
| 3298 |
|
| 3299 |
if (((reg_composer[0] & 0x8) == 0 && (value & 0x8)) || |
| 3300 |
((reg_composer[0] & 0x3) == 1 && (value & 0x3) > 1 && (value & 0x8))) { |
| 3301 |
memset(framebuffer, 0x00, SCREEN_WIDTH * SCREEN_HEIGHT * 4); |
| 3302 |
|
| 3303 |
|
| 3304 |
|
| 3305 |
|
| 3306 |
memset(line_state_valid, 0, sizeof(line_state_valid)); |
| 3307 |
} |
| 3308 |
|
| 3309 |
|
| 3310 |
reg_composer[0] = (reg_composer[0] & ~0x7f) | (value & 0x7f); |
| 3311 |
video_palette.dirty = true; |
| 3312 |
} else { |
| 3313 |
reg_composer[i] = value; |
| 3314 |
} |
| 3315 |
|
| 3316 |
switch (i) { |
| 3317 |
case 0x08: |
| 3318 |
fx_addr1_mode = value & 0x03; |
| 3319 |
fx_4bit_mode = (value & 0x04) >> 2; |
| 3320 |
fx_16bit_hop = (value & 0x08) >> 3; |
| 3321 |
fx_cache_byte_cycling = (value & 0x10) >> 4; |
| 3322 |
fx_cache_fill = (value & 0x20) >> 5; |
| 3323 |
fx_cache_write = (value & 0x40) >> 6; |
| 3324 |
fx_trans_writes = (value & 0x80) >> 7; |
| 3325 |
break; |
| 3326 |
case 0x09: |
| 3327 |
fx_affine_tile_base = (value & 0xfc) << 9; |
| 3328 |
fx_affine_clip = (value & 0x02) >> 1; |
| 3329 |
fx_2bit_poly = (value & 0x01); |
| 3330 |
break; |
| 3331 |
case 0x0a: |
| 3332 |
fx_affine_map_base = (value & 0xfc) << 9; |
| 3333 |
fx_affine_map_size = 2 << ((value & 0x03) << 1); |
| 3334 |
break; |
| 3335 |
case 0x0b: |
| 3336 |
fx_cache_increment_mode = value & 0x01; |
| 3337 |
fx_cache_nibble_index = (value & 0x02) >> 1; |
| 3338 |
fx_cache_byte_index = (value & 0x0c) >> 2; |
| 3339 |
fx_multiplier = (value & 0x10) >> 4; |
| 3340 |
fx_subtract = (value & 0x20) >> 5; |
| 3341 |
if (value & 0x40) { |
| 3342 |
int32_t m_result = (int16_t)((fx_cache[1] << 8) | fx_cache[0]) * (int16_t)((fx_cache[3] << 8) | fx_cache[2]); |
| 3343 |
if (fx_subtract) |
| 3344 |
fx_mult_accumulator -= m_result; |
| 3345 |
else |
| 3346 |
fx_mult_accumulator += m_result; |
| 3347 |
} |
| 3348 |
if (value & 0x80) { |
| 3349 |
fx_mult_accumulator = 0; |
| 3350 |
} |
| 3351 |
break; |
| 3352 |
case 0x0c: |
| 3353 |
fx_x_pixel_increment = ((((reg_composer[0x0d] & 0x7f) << 15) + (reg_composer[0x0c] << 7)) |
| 3354 |
| ((reg_composer[0x0d] & 0x40) ? 0xffc00000 : 0)) |
| 3355 |
<< 5*(!!(reg_composer[0x0d] & 0x80)); |
| 3356 |
break; |
| 3357 |
case 0x0d: |
| 3358 |
fx_x_pixel_increment = ((((reg_composer[0x0d] & 0x7f) << 15) + (reg_composer[0x0c] << 7)) |
| 3359 |
| ((reg_composer[0x0d] & 0x40) ? 0xffc00000 : 0)) |
| 3360 |
<< 5*(!!(reg_composer[0x0d] & 0x80)); |
| 3361 |
if (fx_addr1_mode == 1 || fx_addr1_mode == 2) { |
| 3362 |
|
| 3363 |
fx_x_pixel_position = (fx_x_pixel_position & 0x07ff0000) | 0x00008000; |
| 3364 |
} |
| 3365 |
break; |
| 3366 |
case 0x0e: |
| 3367 |
fx_y_pixel_increment = ((((reg_composer[0x0f] & 0x7f) << 15) + (reg_composer[0x0e] << 7)) |
| 3368 |
| ((reg_composer[0x0f] & 0x40) ? 0xffc00000 : 0)) |
| 3369 |
<< 5*(!!(reg_composer[0x0f] & 0x80)); |
| 3370 |
break; |
| 3371 |
case 0x0f: |
| 3372 |
fx_y_pixel_increment = ((((reg_composer[0x0f] & 0x7f) << 15) + (reg_composer[0x0e] << 7)) |
| 3373 |
| ((reg_composer[0x0f] & 0x40) ? 0xffc00000 : 0)) |
| 3374 |
<< 5*(!!(reg_composer[0x0f] & 0x80)); |
| 3375 |
if (fx_addr1_mode == 1 || fx_addr1_mode == 2) { |
| 3376 |
|
| 3377 |
fx_y_pixel_position = (fx_y_pixel_position & 0x07ff0000) | 0x00008000; |
| 3378 |
} |
| 3379 |
break; |
| 3380 |
case 0x10: |
| 3381 |
fx_x_pixel_position = (fx_x_pixel_position & 0x0700ff80) | (value << 16); |
| 3382 |
fx_affine_prefetch(); |
| 3383 |
break; |
| 3384 |
case 0x11: |
| 3385 |
fx_x_pixel_position = (fx_x_pixel_position & 0x00ffff00) | ((value & 0x7) << 24) | (value & 0x80); |
| 3386 |
fx_affine_prefetch(); |
| 3387 |
break; |
| 3388 |
case 0x12: |
| 3389 |
fx_y_pixel_position = (fx_y_pixel_position & 0x0700ff80) | (value << 16); |
| 3390 |
fx_affine_prefetch(); |
| 3391 |
break; |
| 3392 |
case 0x13: |
| 3393 |
fx_y_pixel_position = (fx_y_pixel_position & 0x00ffff00) | ((value & 0x7) << 24) | (value & 0x80); |
| 3394 |
fx_affine_prefetch(); |
| 3395 |
break; |
| 3396 |
case 0x14: |
| 3397 |
fx_x_pixel_position = (fx_x_pixel_position & 0x07ff0080) | (value << 8); |
| 3398 |
break; |
| 3399 |
case 0x15: |
| 3400 |
fx_y_pixel_position = (fx_y_pixel_position & 0x07ff0080) | (value << 8); |
| 3401 |
break; |
| 3402 |
case 0x18: |
| 3403 |
fx_cache[0] = value; |
| 3404 |
break; |
| 3405 |
case 0x19: |
| 3406 |
fx_cache[1] = value; |
| 3407 |
break; |
| 3408 |
case 0x1a: |
| 3409 |
fx_cache[2] = value; |
| 3410 |
break; |
| 3411 |
case 0x1b: |
| 3412 |
fx_cache[3] = value; |
| 3413 |
break; |
| 3414 |
} |
| 3415 |
break; |
| 3416 |
} |
| 3417 |
|
| 3418 |
case 0x0D: |
| 3419 |
case 0x0E: |
| 3420 |
case 0x0F: |
| 3421 |
case 0x10: |
| 3422 |
case 0x11: |
| 3423 |
case 0x12: |
| 3424 |
case 0x13: |
| 3425 |
video_step(MHZ, 0, true); |
| 3426 |
reg_layer[0][reg - 0x0D] = value; |
| 3427 |
refresh_layer_properties(0); |
| 3428 |
break; |
| 3429 |
|
| 3430 |
case 0x14: |
| 3431 |
case 0x15: |
| 3432 |
case 0x16: |
| 3433 |
case 0x17: |
| 3434 |
case 0x18: |
| 3435 |
case 0x19: |
| 3436 |
case 0x1A: |
| 3437 |
video_step(MHZ, 0, true); |
| 3438 |
reg_layer[1][reg - 0x14] = value; |
| 3439 |
refresh_layer_properties(1); |
| 3440 |
break; |
| 3441 |
|
| 3442 |
case 0x1B: audio_render(); pcm_write_ctrl(value); break; |
| 3443 |
case 0x1C: audio_render(); pcm_write_rate(value); break; |
| 3444 |
case 0x1D: audio_render(); pcm_write_fifo(value); break; |
| 3445 |
|
| 3446 |
case 0x1E: |
| 3447 |
case 0x1F: |
| 3448 |
vera_spi_write(reg & 1, value); |
| 3449 |
break; |
| 3450 |
} |
| 3451 |
} |
| 3452 |
|
| 3453 |
void |
| 3454 |
video_update_title(const char* window_title) |
| 3455 |
{ |
| 3456 |
SDL_SetWindowTitle(window, window_title); |
| 3457 |
} |
| 3458 |
|
| 3459 |
bool video_is_tilemap_address(int addr) |
| 3460 |
{ |
| 3461 |
for (int l = 0; l < 2; ++l) { |
| 3462 |
struct video_layer_properties *props = &layer_properties[l]; |
| 3463 |
if (addr < props->map_base) { |
| 3464 |
continue; |
| 3465 |
} |
| 3466 |
if (addr >= props->map_base + (2 << (props->mapw_log2 + props->maph_log2))) { |
| 3467 |
continue; |
| 3468 |
} |
| 3469 |
|
| 3470 |
return true; |
| 3471 |
} |
| 3472 |
return false; |
| 3473 |
} |
| 3474 |
|
| 3475 |
bool video_is_tiledata_address(int addr) |
| 3476 |
{ |
| 3477 |
for (int l = 0; l < 2; ++l) { |
| 3478 |
struct video_layer_properties *props = &layer_properties[l]; |
| 3479 |
if (addr < props->tile_base) { |
| 3480 |
continue; |
| 3481 |
} |
| 3482 |
int tile_size = props->tilew * props->tileh * props->bits_per_pixel / 8; |
| 3483 |
if (addr >= props->tile_base + tile_size * (props->bits_per_pixel == 1 ? 256 : 1024)) { |
| 3484 |
continue; |
| 3485 |
} |
| 3486 |
|
| 3487 |
return true; |
| 3488 |
} |
| 3489 |
return false; |
| 3490 |
} |
| 3491 |
|
| 3492 |
bool video_is_special_address(int addr) |
| 3493 |
{ |
| 3494 |
return addr >= 0x1F9C0; |
| 3495 |
} |
| 3496 |
|
| 3497 |
void |
| 3498 |
stop6502(uint16_t address, uint8_t bank) { |
| 3499 |
|
| 3500 |
|
| 3501 |
|
| 3502 |
|
| 3503 |
if (testbench) { |
| 3504 |
printf("STP\n"); |
| 3505 |
fflush(stdout); |
| 3506 |
} else if (debugger_enabled || imgui_debugger_enabled) { |
| 3507 |
|
| 3508 |
|
| 3509 |
|
| 3510 |
|
| 3511 |
DEBUGBreakToDebugger(); |
| 3512 |
|
| 3513 |
|
| 3514 |
|
| 3515 |
|
| 3516 |
DEBUGSetStopReason("breakpoint"); |
| 3517 |
} else { |
| 3518 |
int return_btn; |
| 3519 |
char error_message[80]; |
| 3520 |
const SDL_MessageBoxButtonData btns[2] = { |
| 3521 |
{SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Reset Machine"}, |
| 3522 |
{SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, -1, "Ignore"} |
| 3523 |
}; |
| 3524 |
const SDL_MessageBoxData msg_box = { |
| 3525 |
SDL_MESSAGEBOX_ERROR, window, "Error", error_message, |
| 3526 |
2, btns, NULL |
| 3527 |
}; |
| 3528 |
|
| 3529 |
if (is_gen2) { |
| 3530 |
sprintf(error_message, "Encountered stop instruction at address $%06X. CPU cannot continue.", (regs.k << 16) | address); |
| 3531 |
} else { |
| 3532 |
sprintf(error_message, "Encountered stop instruction at address $%04X. CPU cannot continue.", address); |
| 3533 |
} |
| 3534 |
if (SDL_ShowMessageBox(&msg_box, &return_btn) == 0 && return_btn == 0) { |
| 3535 |
machine_reset(); |
| 3536 |
}; |
| 3537 |
} |
| 3538 |
} |