Skip to content

Commit

Permalink
MEGA65: experimental colour effects
Browse files Browse the repository at this point in the history
Control is in UI. No CLI/cfg stuff yet.
  • Loading branch information
lgblgblgb committed Jul 27, 2023
1 parent c6e30b5 commit 037fe31
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 11 deletions.
18 changes: 18 additions & 0 deletions targets/mega65/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,27 @@ static void ui_cb_mega65_model ( const struct menu_st *m, int *query )
WARNING_WINDOW("It is recommended to reset emulation at this point");
}

static void ui_cb_colour_effect ( const struct menu_st *m, int *query )
{
XEMUGUI_RETURN_CHECKED_ON_QUERY(query, VOIDPTR_TO_INT(m->user_data) == emulation_colour_effect);
vic4_set_emulation_colour_effect(VOIDPTR_TO_INT(m->user_data));
}


/**** MENU SYSTEM ****/


static const struct menu_st menu_colour_effects[] = {
{ "Normal colours", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)0 },
{ "Grayscale", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)1 },
{ "Reduced red channel", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)2 },
{ "Missing red channel", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)3 },
{ "Reduced green channel", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)4 },
{ "Missing green channel", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)5 },
{ "Reduced blue channel", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)6 },
{ "Missing blue channel", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_colour_effect, (void*)7 },
{ NULL }
};
static const struct menu_st menu_mega65_model[] = {
{ "MEGA65 r1", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_mega65_model, (void*)0x01 },
{ "MEGA65 r2", XEMUGUI_MENUID_CALLABLE | XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_mega65_model, (void*)0x02 },
Expand Down Expand Up @@ -713,6 +730,7 @@ static const struct menu_st menu_display[] = {
{ "Window size / fullscreen", XEMUGUI_MENUID_SUBMENU, NULL, menu_window_size },
{ "Video standard", XEMUGUI_MENUID_SUBMENU, NULL, menu_video_standard },
{ "Show scanlines at V200", XEMUGUI_MENUID_SUBMENU, NULL, menu_show_scanlines },
{ "Colour effects", XEMUGUI_MENUID_SUBMENU, NULL, menu_colour_effects },
{ "Show full borders", XEMUGUI_MENUID_CALLABLE |
XEMUGUI_MENUFLAG_QUERYBACK, ui_cb_fullborders, NULL },
{ "Show drive LED", XEMUGUI_MENUID_CALLABLE |
Expand Down
9 changes: 9 additions & 0 deletions targets/mega65/vic4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1683,6 +1683,15 @@ int vic4_textinsert ( const char *text )
}


void vic4_set_emulation_colour_effect ( const int val )
{
if (emulation_colour_effect != val) {
emulation_colour_effect = val;
vic4_revalidate_all_palette();
}
}


/* --- SNAPSHOT RELATED --- */


Expand Down
3 changes: 3 additions & 0 deletions targets/mega65/vic4.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ extern int vic4_disallow_videostd_change;
extern int vic4_registered_screenshot_request;
extern int vic_vidp_legacy, vic_chrp_legacy, vic_sprp_legacy;

extern int emulation_colour_effect; // for real, it's in vic4_palette.c

extern const char *iomode_names[4];

extern void vic_init ( void );
Expand All @@ -263,6 +265,7 @@ extern int vic4_query_screen_width ( void );
extern int vic4_query_screen_height ( void );
extern char *vic4_textshot ( void );
extern int vic4_textinsert ( const char *text );
extern void vic4_set_emulation_colour_effect ( const int val );

#ifdef XEMU_SNAPSHOT_SUPPORT
#include "xemu/emutools_snapshot.h"
Expand Down
57 changes: 47 additions & 10 deletions targets/mega65/vic4_palette.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* A work-in-progess MEGA65 (Commodore 65 clone origins) emulator
Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
Copyright (C)2016-2021 LGB (Gábor Lénárt) <[email protected]>
Copyright (C)2016-2023 LGB (Gábor Lénárt) <[email protected]>
MEGA65 palette handling for VIC-IV with compatibility for C65 style
VIC-III palette.
Expand Down Expand Up @@ -40,6 +40,7 @@ static struct {
Uint32 alpha_shift, alpha_mask, alpha_revmask;
} sdlpalinfo;
unsigned int palregaccofs;
int emulation_colour_effect = 0;


static XEMU_INLINE Uint8 swap_nibbles ( Uint8 i )
Expand All @@ -48,14 +49,47 @@ static XEMU_INLINE Uint8 swap_nibbles ( Uint8 i )
}


static void recalc_sdl_palval ( const unsigned int i )
{
Uint8 red = swap_nibbles(vic_palette_bytes_red [i] & 0xEF);
Uint8 green = swap_nibbles(vic_palette_bytes_green[i]);
Uint8 blue = swap_nibbles(vic_palette_bytes_blue [i]);
switch (emulation_colour_effect) {
case 0: // normal
break;
case 1: // grayscale
red = (Uint8)(float)(0.299 * (float)red + 0.587 * (float)green + 0.114 * (float)blue);
green = red;
blue = red;
break;
case 2: // reduced red
red >>= 1;
break;
case 3: // missing red
red = 0;
break;
case 4: // reduced green
green >>= 1;
break;
case 5: // missing green
green = 0;
break;
case 6: // reduced blue
blue >>= 1;
break;
case 7: // missing blue
blue = 0;
break;

}
vic_palettes[i] = sdlpalinfo.alpha_mask | (red << sdlpalinfo.red_shift) | (green << sdlpalinfo.green_shift) | (blue << sdlpalinfo.blue_shift);
}


void vic4_revalidate_all_palette ( void )
{
for (int i = 0; i < NO_OF_PALETTE_REGS; i++)
vic_palettes[i] =
sdlpalinfo.alpha_mask |
((swap_nibbles(vic_palette_bytes_red [i] & 0xEF)) << sdlpalinfo.red_shift ) |
( swap_nibbles(vic_palette_bytes_green[i]) << sdlpalinfo.green_shift) |
( swap_nibbles(vic_palette_bytes_blue [i]) << sdlpalinfo.blue_shift ) ;
for (unsigned int i = 0; i < NO_OF_PALETTE_REGS; i++)
recalc_sdl_palval(i);
}


Expand Down Expand Up @@ -127,7 +161,8 @@ void vic4_write_palette_reg_red ( unsigned int num, Uint8 data )
{
num = (num & 0xFF) + palregaccofs;
vic_palette_bytes_red[num] = data;
vic_palettes[num] = (vic_palettes[num] & sdlpalinfo.red_revmask) | ((swap_nibbles(data & 0xEF)) << sdlpalinfo.red_shift);
//vic_palettes[num] = (vic_palettes[num] & sdlpalinfo.red_revmask) | ((swap_nibbles(data & 0xEF)) << sdlpalinfo.red_shift);
recalc_sdl_palval(num);
if (num >= 0x300 && num <= 0x30F) // first 16 entries of bank #3 forms the "ROM palette" of C65
vic_palettes[num + 0x100] = vic_palettes[num];
if (num >= 0x10 && num <= 0xFF) // rest of the entires of bank #0, also the "ROM palette" emulation stuff
Expand All @@ -138,7 +173,8 @@ void vic4_write_palette_reg_green ( unsigned int num, Uint8 data )
{
num = (num & 0xFF) + palregaccofs;
vic_palette_bytes_green[num] = data;
vic_palettes[num] = (vic_palettes[num] & sdlpalinfo.green_revmask) | (swap_nibbles(data) << sdlpalinfo.green_shift);
//vic_palettes[num] = (vic_palettes[num] & sdlpalinfo.green_revmask) | (swap_nibbles(data) << sdlpalinfo.green_shift);
recalc_sdl_palval(num);
if (num >= 0x300 && num <= 0x30F)
vic_palettes[num + 0x100] = vic_palettes[num];
if (num >= 0x10 && num <= 0xFF)
Expand All @@ -149,7 +185,8 @@ void vic4_write_palette_reg_blue ( unsigned int num, Uint8 data )
{
num = (num & 0xFF) + palregaccofs;
vic_palette_bytes_blue[num] = data;
vic_palettes[num] = (vic_palettes[num] & sdlpalinfo.blue_revmask) | (swap_nibbles(data) << sdlpalinfo.blue_shift);
//vic_palettes[num] = (vic_palettes[num] & sdlpalinfo.blue_revmask) | (swap_nibbles(data) << sdlpalinfo.blue_shift);
recalc_sdl_palval(num);
if (num >= 0x300 && num <= 0x30F)
vic_palettes[num + 0x100] = vic_palettes[num];
if (num >= 0x10 && num <= 0xFF)
Expand Down
2 changes: 1 addition & 1 deletion targets/mega65/vic4_palette.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* A work-in-progess MEGA65 (Commodore 65 clone origins) emulator
Part of the Xemu project, please visit: https://github.com/lgblgblgb/xemu
Copyright (C)2016-2021 LGB (Gábor Lénárt) <[email protected]>
Copyright (C)2016-2023 LGB (Gábor Lénárt) <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down

0 comments on commit 037fe31

Please sign in to comment.