From a91f03d56e75b7862c040f56951169697c9e75d3 Mon Sep 17 00:00:00 2001 From: Sorgelig Date: Wed, 8 Feb 2023 20:47:35 +0800 Subject: [PATCH] Support for YC phase_inc override. --- cfg.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ cfg.h | 8 +++++++ video.cpp | 24 +++++++++++++++++--- yc.txt | 14 ++++++++++++ 4 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 yc.txt diff --git a/cfg.cpp b/cfg.cpp index 34ebdcf..19c1bb6 100644 --- a/cfg.cpp +++ b/cfg.cpp @@ -579,3 +579,70 @@ void cfg_print() } printf("--------------\n"); } + +static int yc_parse_mode(char* buf, yc_mode *mode) +{ + int i = 0; + while (1) + { + if (buf[i] == '=' || CHAR_IS_SPACE(buf[i])) + { + buf[i] = 0; + break; + } + else if (!buf[i]) return 0; + i++; + } + + i++; + while (buf[i] == '=' || CHAR_IS_SPACE(buf[i])) i++; + ini_parser_debugf("Got yc_mode '%s' with VALUE %s", buf, buf + i); + + snprintf(mode->key, sizeof(mode->key), "%s", buf); + mode->phase_inc = strtoull(buf + i, 0, 0); + if (!mode->phase_inc) + { + printf("ERROR: cannot parse YC phase_inc: '%s'\n", buf + i); + return 0; + } + + return 1; +} + +void yc_parse(yc_mode *yc_table, int max) +{ + memset(yc_table, 0, max * sizeof(yc_mode)); + + static char line[INI_LINE_SIZE]; + int eof; + + memset(line, 0, sizeof(line)); + memset(&ini_file, 0, sizeof(ini_file)); + + const char *corename = user_io_get_core_name(1); + int corename_len = strlen(corename); + + const char *name = "yc.txt"; + if (!FileOpen(&ini_file, name)) return; + + ini_parser_debugf("Opened file %s with size %llu bytes.", name, ini_file.size); + + ini_pt = 0; + int n = 0; + + while (n < max) + { + // get line + eof = ini_getline(line); + if (!strncasecmp(line, corename, corename_len)) + { + int res = yc_parse_mode(line, &yc_table[n]); + if (res) n++; + } + + // if end of file, stop + if (eof) break; + } + + FileClose(&ini_file); +} diff --git a/cfg.h b/cfg.h index 4bb4ea3..1805dfe 100644 --- a/cfg.h +++ b/cfg.h @@ -101,4 +101,12 @@ bool cfg_has_video_sections(); void cfg_error(const char *fmt, ...); bool cfg_check_errors(char *msg, size_t max_len); +struct yc_mode +{ + char key[64]; + int64_t phase_inc; +}; + +void yc_parse(yc_mode *yc_table, int max); + #endif // __CFG_H__ diff --git a/video.cpp b/video.cpp index 15fd199..36b3828 100644 --- a/video.cpp +++ b/video.cpp @@ -77,6 +77,8 @@ static VideoInfo current_video_info; static int support_FHD = 0; +yc_mode yc_modes[10]; + struct vrr_cap_t { uint8_t active; @@ -2394,6 +2396,8 @@ void video_cfg_reset() void video_init() { + yc_parse(yc_modes, sizeof(yc_modes) / sizeof(yc_modes[0])); + fb_init(); hdmi_config_init(); hdmi_config_set_hdr(); @@ -2725,7 +2729,8 @@ static void set_yc_mode() { if (cfg.vga_mode_int >= 2) { - int pal = (!current_video_info.vtime || (100000000 / current_video_info.vtime) < 55); + float fps = current_video_info.vtime ? (100000000.f / current_video_info.vtime) : 0.f; + int pal = fps < 55.f; double CLK_REF = pal ? 4.43361875f : 3.579545f; double CLK_VIDEO = current_video_info.ctime * 100.f / current_video_info.ptime; @@ -2735,6 +2740,21 @@ static void set_yc_mode() int COLORBURST_END = (int)(9.0f * (CLK_VIDEO / CLK_REF)) + COLORBURST_START; int COLORBURST_RANGE = (COLORBURST_START << 10) | COLORBURST_END; + char yc_key[64]; + sprintf(yc_key, "%s_%.1f%s", user_io_get_core_name(1), fps, current_video_info.interlaced ? "i" : ""); + + printf("Calculated YC parameters for '%s': %s PHASE_INC=%lld, COLORBURST_START=%d, COLORBURST_END=%d\n", yc_key, pal ? "PAL" : "NTSC", PHASE_INC, COLORBURST_START, COLORBURST_END); + + for (uint i = 0; i < sizeof(yc_modes) / sizeof(yc_modes[0]); i++) + { + if (!strcasecmp(yc_modes[i].key, yc_key)) + { + printf("Override YC PHASE_INC with value: %lld\n", yc_modes[i].phase_inc); + PHASE_INC = yc_modes[i].phase_inc; + break; + } + } + spi_uio_cmd_cont(UIO_SET_YC_PAR); spi_w((pal ? 4 : 0) | ((cfg.vga_mode_int == 3) ? 3 : 1)); spi_w(PHASE_INC); @@ -2743,8 +2763,6 @@ static void set_yc_mode() spi_w(COLORBURST_RANGE); spi_w(COLORBURST_RANGE >> 16); DisableIO(); - - printf("Send YC parameters: %s PHASE_INC=%lld, COLORBURST_START=%d, COLORBURST_END=%d\n", pal ? "PAL" : "NTSC", PHASE_INC, COLORBURST_START, COLORBURST_END); } else { diff --git a/yc.txt b/yc.txt new file mode 100644 index 0000000..4a964e7 --- /dev/null +++ b/yc.txt @@ -0,0 +1,14 @@ +; +; PHASE_INC override per mode. +; It affects only CVBS video output. +; S-Video doesn't need this file. +; + +SNES_60.1=91625968981 +SNES_60.0i=91625968981 +SNES_50.0=114532461227 +SNES_49.9i=114532461227 +NEOGEO_59.2=81994830080 +NEOGEO_59.6=81425421603 +NEOGEO_50.1=101558653516 +NEOGEO_50.4=100853152548