Add custom palette loading

This commit is contained in:
U-COOKIE\kitri
2018-11-30 14:13:59 -06:00
parent 478f4c65b4
commit a7ecdccc43
6 changed files with 137 additions and 33 deletions

View File

@@ -49,7 +49,7 @@ module emu
output VGA_HS,
output VGA_VS,
output VGA_DE, // = ~(VBlank | HBlank)
output VGA_F1,
output VGA_F1,
output [1:0] VGA_SL,
output LED_USER, // 1 - ON, 0 - OFF.
@@ -97,14 +97,14 @@ module emu
output SDRAM_nCS,
output SDRAM_nCAS,
output SDRAM_nRAS,
output SDRAM_nWE,
input UART_CTS,
output UART_RTS,
input UART_RXD,
output UART_TXD,
output UART_DTR,
input UART_DSR
output SDRAM_nWE,
input UART_CTS,
output UART_RTS,
input UART_RXD,
output UART_TXD,
output UART_DTR,
input UART_DSR
);
assign {DDRAM_CLK, DDRAM_BURSTCNT, DDRAM_ADDR, DDRAM_DIN, DDRAM_BE, DDRAM_RD, DDRAM_WE} = 0;
@@ -115,7 +115,7 @@ assign {UART_RTS, UART_TXD, UART_DTR} = 0;
assign {SD_SCK, SD_MOSI, SD_CS} = 'Z;
assign LED_USER = ioctl_download;
assign LED_DISK = 0;
assign LED_DISK = palette_download;
assign LED_POWER = 0;
assign VIDEO_ARX = status[3] ? 8'd16 : 8'd4;
@@ -124,17 +124,22 @@ assign VIDEO_ARY = status[3] ? 8'd9 : 8'd3;
assign AUDIO_MIX = status[8:7];
`include "build_id.v"
localparam CONF_STR = {
localparam CONF_STR1 = {
"GAMEBOY;;",
"-;",
"F,GB;",
"-;",
"O1,LCD tint,White,Yellow;",
"O4,Inverted,No,Yes;",
"O4,Inverted color,No,Yes;",
"O1,Palette,Grayscale,Custom;",
};
localparam CONF_STR2 = {
",GBP;",
"-;",
"O3,Aspect ratio,4:3,16:9;",
"O78,Stereo mix,none,25%,50%,100%;",
"-;",
"O2,Boot,Normal,Fast;",
"O2,Boot,Normal,Fast;",
"-;",
"R6,Reset;",
"J1,A,B,Select,Start;",
@@ -168,19 +173,21 @@ reg ioctl_wait;
wire [15:0] joystick_0, joystick_1;
wire [15:0] joystick = joystick_0 | joystick_1;
wire [7:0] filetype;
hps_io #(.STRLEN($size(CONF_STR)>>3), .WIDE(1)) hps_io
hps_io #(.STRLEN(($size(CONF_STR1)>>3) + ($size(CONF_STR2)>>3) + 1), .WIDE(1)) hps_io
(
.clk_sys(clk_sys),
.HPS_BUS(HPS_BUS),
.conf_str(CONF_STR),
.conf_str({CONF_STR1,status[1]?"F":"+",CONF_STR2}),
.ioctl_download(ioctl_download),
.ioctl_wr(ioctl_wr),
.ioctl_addr(ioctl_addr),
.ioctl_dout(ioctl_dout),
.ioctl_wait(ioctl_wait),
.ioctl_index(filetype),
.buttons(buttons),
.status(status),
@@ -191,13 +198,16 @@ hps_io #(.STRLEN($size(CONF_STR)>>3), .WIDE(1)) hps_io
///////////////////////////////////////////////////
wire cart_download = ioctl_download && (filetype == 8'h01);
wire palette_download = ioctl_download && (filetype == 8'h04);
// TODO: ds for cart ram write
wire [1:0] sdram_ds = ioctl_download ? 2'b11 : {cart_addr[0], ~cart_addr[0]};
wire [1:0] sdram_ds = cart_download ? 2'b11 : {cart_addr[0], ~cart_addr[0]};
wire [15:0] sdram_do;
wire [15:0] sdram_di = ioctl_download ? ioctl_dout : {cart_di, cart_di};
wire [23:0] sdram_addr = ioctl_download? ioctl_addr[24:1]: {3'b000, mbc_bank, cart_addr[12:1]};
wire sdram_oe = ~ioctl_download & cart_rd;
wire sdram_we = ioctl_download ? dn_write : cart_ram_wr;
wire [15:0] sdram_di = cart_download ? ioctl_dout : {cart_di, cart_di};
wire [23:0] sdram_addr = cart_download? ioctl_addr[24:1]: {3'b000, mbc_bank, cart_addr[12:1]};
wire sdram_oe = ~cart_download & cart_rd;
wire sdram_we = cart_download? dn_write : cart_ram_wr;
assign SDRAM_CKE = 1;
@@ -405,18 +415,23 @@ wire [8:0] mbc_bank =
// HuC3?HuC3_addr:
{7'b0000000, cart_addr[14:13]}; // no MBC, 32k linear address
reg [127:0] palette = 128'h8201547153165C639200000000000000;
always @(posedge clk_sys) begin
if(!pll_locked) begin
cart_mbc_type <= 8'h00;
cart_rom_size <= 8'h00;
cart_ram_size <= 8'h00;
end else begin
if(ioctl_download & ioctl_wr) begin
if(cart_download & ioctl_wr) begin
case(ioctl_addr)
'h146: cart_mbc_type <= ioctl_dout[15:8];
'h148: { cart_ram_size, cart_rom_size } <= ioctl_dout;
endcase
end
end
end
if (palette_download & ioctl_wr) begin
palette[127:0] <= {palette[111:0], ioctl_dout[7:0], ioctl_dout[15:8]};
end
end
@@ -439,7 +454,7 @@ wire lcd_on;
assign AUDIO_S = 0;
wire reset = (RESET | status[0] | status[6] | buttons[1] | ioctl_download);
wire reset = (RESET | status[0] | status[6] | buttons[1] | cart_download);
// the gameboy itself
@@ -480,6 +495,12 @@ lcd lcd (
.tint ( status[1] ),
.inv ( status[4] ),
// Palettes
.pal1 (palette[127:110]),
.pal2 (palette[109:92]),
.pal3 (palette[91:74]),
.pal4 (palette[73:56]),
// serial interface
.clkena ( lcd_clkena ),
.data ( lcd_data ),

24
lcd.v
View File

@@ -8,6 +8,12 @@ module lcd (
input clkena,
input [1:0] data,
input [1:0] mode,
//palette
input [17:0] pal1,
input [17:0] pal2,
input [17:0] pal3,
input [17:0] pal4,
input tint,
input inv,
@@ -137,18 +143,18 @@ end
wire [1:0] pixel = on? (pixel_reg ^ {inv,inv}) :2'b00;
// gameboy "color" palette
wire [5:0] yellow_r = (pixel==0)?6'b100111:(pixel==1)?6'b100000: // 1:100011
(pixel==2)?6'b001100:6'b000111;
wire [5:0] yellow_g = (pixel==0)?6'b101111:(pixel==1)?6'b101000: // 1:101011
(pixel==2)?6'b011001:6'b000100;
wire [5:0] yellow_b = (pixel==0)?6'b000100:(pixel==1)?6'b000010: // 1:000100
(pixel==2)?6'b001100:6'b000100;
wire [5:0] pal_r = (pixel==0)?pal1[17:12]:(pixel==1)?pal2[17:12]:
(pixel==2)?pal3[17:12]:pal4[17:12];
wire [5:0] pal_g = (pixel==0)?pal1[11:6] :(pixel==1)?pal2[11:6] :
(pixel==2)?pal3[11:6] :pal4[11:6] ;
wire [5:0] pal_b = (pixel==0)?pal1[5:0] :(pixel==1)?pal2[5:0] :
(pixel==2)?pal3[5:0] :pal4[5:0] ;
// greyscale
wire [5:0] grey = (pixel==0)?6'd63:(pixel==1)?6'd42:(pixel==2)?6'd24:6'd0;
assign r = blank?6'b000000:tint?yellow_r:grey;
assign g = blank?6'b000000:tint?yellow_g:grey;
assign b = blank?6'b000000:tint?yellow_b:grey;
assign r = blank?6'b000000:tint?pal_r:grey;
assign g = blank?6'b000000:tint?pal_g:grey;
assign b = blank?6'b000000:tint?pal_b:grey;
endmodule

BIN
palettes/Biverted.gbp Normal file

Binary file not shown.

BIN
palettes/DMG.gbp Normal file

Binary file not shown.

BIN
palettes/Yellow.gbp Normal file

Binary file not shown.

77
palettes/gbpalette.c Normal file
View File

@@ -0,0 +1,77 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
// GBP file format is simple. There are 4 palette colors using 6 bit
// rgb, starting from lightest ending in darkest for a total of 9 bytes.
// The last several bytes are 0, reserved for future use.
void gbp_bitcopy(__uint128_t *dest, uint32_t dest_from, uint32_t source,
uint32_t source_from, uint32_t len)
{
for (uint32_t x = 0; x < len; x++) {
uint8_t bit = (uint8_t) ((source & (1 << (source_from - x))) ? 1 : 0);
*dest |= (__uint128_t) bit << (dest_from - x);
}
}
uint32_t gbp_get_color(void)
{
char buffer[100];
printf ("Red (0-255): ");
fgets(buffer, sizeof(buffer), stdin);
uint8_t red = (uint8_t) strtol(buffer, NULL, 10);
printf ("Green (0-255): ");
fgets(buffer, sizeof(buffer), stdin);
uint8_t green = (uint8_t) strtol(buffer, NULL, 10);
printf ("Blue (0-255): ");
fgets(buffer, sizeof(buffer), stdin);
uint8_t blue = (uint8_t) strtol(buffer, NULL, 10);
return (uint32_t) (((red >> 2) << 12) | ((green >> 2) << 6) | (blue >> 2));
}
int main (int argc, char **argv)
{
__uint128_t palette = 0;
if (argc < 1) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *f = fopen(argv[1], "w");
if (!f) {
printf("Unable to open %s for writing.\n", argv[1]);
return 1;
}
printf("Color 1:\n");
uint32_t color1 = gbp_get_color();
printf("Color 2:\n");
uint32_t color2 = gbp_get_color();
printf("Color 3:\n");
uint32_t color3 = gbp_get_color();
printf("Color 4:\n");
uint32_t color4 = gbp_get_color();
gbp_bitcopy(&palette, 127, color1, 17, 18);
gbp_bitcopy(&palette, 109, color2, 17, 18);
gbp_bitcopy(&palette, 91, color3, 17, 18);
gbp_bitcopy(&palette, 73, color4, 17, 18);
uint8_t *pal = (uint8_t *) &palette;
for (uint32_t x = 0; x < 16; x++) {
fwrite(&pal[15 - x], 1, 1, f);
}
fclose(f);
return 0;
}