Skip to content

Commit

Permalink
vga-nextgen
Browse files Browse the repository at this point in the history
  • Loading branch information
xrip committed Feb 13, 2024
1 parent 926e306 commit d69457e
Show file tree
Hide file tree
Showing 26 changed files with 610 additions and 2,986 deletions.
150 changes: 89 additions & 61 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,51 +1,103 @@
cmake_minimum_required(VERSION 3.13...3.23)

include(pico_sdk_import.cmake)

set(OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
project(gameboy-color C CXX ASM)
SET(BUILD_NAME "${PROJECT_NAME}")

option(I2S "Enable I2S sound" OFF)
option(I2S_CS4334 "Enable I2S CS4334 sound" OFF)
option(VGA "Enable VGA" OFF)
option(TFT "Enable TFT display" OFF)
option(ILI9341 "Enable TFT ILI9341 display" OFF)
option(HDMI "Enable HDMI display" OFF)
option(TV "Enable TV composite output" OFF)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffast-math -feliminate-unused-debug-types -ffunction-sections -fdata-sections -O2")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -feliminate-unused-debug-types -ffunction-sections -fdata-sections -O2")

pico_sdk_init()

set(OUTPUT_DIR "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIR}")

add_subdirectory(drivers/fatfs)
add_subdirectory(drivers/sdcard)
add_subdirectory(drivers/ps2kbd)
add_subdirectory(drivers/vga)
add_subdirectory(drivers/nespad)
add_subdirectory(drivers/audio)

add_subdirectory(drivers/vga-nextgen)
add_subdirectory(drivers/hdmi)
add_subdirectory(drivers/st7789)
add_subdirectory(drivers/tv)

add_subdirectory(drivers/graphics)

add_executable(${PROJECT_NAME}
ext/minigb_apu/minigb_apu.c
src/main.cpp
)

if(OVERCLOCKING)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_NAME}-${OVERCLOCKING}MHz")

target_compile_definitions(${PROJECT_NAME} PRIVATE OVERCLOCKING=${OVERCLOCKING})
pico_define_boot_stage2(slower_boot2 ${PICO_DEFAULT_BOOT_STAGE2_FILE})
target_compile_definitions(slower_boot2 PRIVATE PICO_FLASH_SPI_CLKDIV=4)
pico_set_boot_stage2(${PROJECT_NAME} slower_boot2)

pico_define_boot_stage2(slower_boot2 ${PICO_DEFAULT_BOOT_STAGE2_FILE})
target_compile_definitions(slower_boot2 PRIVATE PICO_FLASH_SPI_CLKDIV=4)
pico_set_boot_stage2(${PROJECT_NAME} slower_boot2)
set(FAMILY rp2040)
set(BOARD pico_sdk)

message(STATUS "==================================")
message(STATUS "Overclocking ${OVERCLOCKING}MHz enabled")
message(STATUS "==================================")
message(STATUS "")
endif ()
pico_set_program_name(gameboy-color "GameBody Emulator by xrip")
pico_set_program_version(gameboy-color "test")

target_include_directories(${PROJECT_NAME} PRIVATE inc ext/minigb_apu)
target_include_directories(${PROJECT_NAME} PRIVATE
src
inc
ext/minigb_apu
)

target_link_libraries(${PROJECT_NAME}
IF(TFT)
target_link_libraries(${PROJECT_NAME} PRIVATE st7789)
target_compile_definitions(${PROJECT_NAME} PRIVATE TFT)
SET(BUILD_NAME "${BUILD_NAME}-TFT")
IF(ILI9341)
SET(BUILD_NAME "${BUILD_NAME}-ILI9341")
target_compile_definitions(${PROJECT_NAME} PRIVATE ILI9341)
ELSE()
SET(BUILD_NAME "${BUILD_NAME}-ST7789")
ENDIF()
ELSEIF(HDMI)
target_link_libraries(${PROJECT_NAME} PRIVATE hdmi)
target_compile_definitions(${PROJECT_NAME} PRIVATE HDMI)
SET(BUILD_NAME "${BUILD_NAME}-HDMI")
ELSEIF(TV)
target_compile_definitions(${PROJECT_NAME} PRIVATE TV)
target_link_libraries(${PROJECT_NAME} PRIVATE tv)
SET(BUILD_NAME "${BUILD_NAME}-TV")
ELSE()
target_compile_definitions(${PROJECT_NAME} PRIVATE VGA)
target_link_libraries(${PROJECT_NAME} PRIVATE vga-nextgen)
SET(BUILD_NAME "${BUILD_NAME}-VGA")
ENDIF()

IF(NOT I2S)
target_compile_definitions(${PROJECT_NAME} PRIVATE AUDIO_PWM_PIN=26)
ELSEIF(I2S_CS4334)
target_compile_definitions(${PROJECT_NAME} PRIVATE I2S_CS4334)
SET(BUILD_NAME "${BUILD_NAME}-I2S-CS4334")
ELSE()
SET(BUILD_NAME "${BUILD_NAME}-I2S-TDA1387")
ENDIF()

set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${BUILD_NAME}")

target_link_libraries(${PROJECT_NAME} PRIVATE
audio
graphics

vga
ps2kbd
sdcard
fatfs
Expand All @@ -59,30 +111,14 @@ target_link_libraries(${PROJECT_NAME}
pico_stdio
pico_multicore

hardware_clocks hardware_pio hardware_vreg hardware_pio hardware_pwm
hardware_sync hardware_pll hardware_spi hardware_irq hardware_dma
pico_binary_info
tinyusb_board
tinyusb_device
)

target_compile_definitions(${PROJECT_NAME} PRIVATE
# 16MB flash
PICO_FLASH_SIZE_BYTES=16777216

PARAM_ASSERTIONS_DISABLE_ALL=1
PICO_ENTER_USB_BOOT_ON_EXIT=1
PICO_STDIO_ENABLE_CRLF_SUPPORT=0
PICO_STDIO_DEFAULT_CRLF=0
PICO_PRINTF_SUPPORT_FLOAT=0
PICO_PRINTF_SUPPORT_EXPONENTIAL=0
PICO_PRINTF_SUPPORT_LONG_LONG=1
PICO_PRINTF_SUPPORT_PTRDIFF_T=0

# VGA
VGA_GPIO_FIRST=6
VGA_GPIO_OUTNUM=6
VGA_GPIO_SYNC=12
VGA_VSYNC=13

# SDCARD
SDCARD_PIN_SPI0_CS=5
SDCARD_PIN_SPI0_SCK=2
Expand All @@ -94,31 +130,23 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE

# NES Gamepad
NES_GPIO_CLK=14
NES_GPIO_LAT=15
NES_GPIO_DATA=16
NES_GPIO_LAT=15

# AUDIO
AUDIO_PWM_PIN=26

# Other
FF_USE_FIND
)

# VGA 8 pins starts from pin:
VGA_BASE_PIN=6

function(pico_add_verbose_dis_output TARGET)
add_custom_command(TARGET ${TARGET} POST_BUILD
COMMAND ${CMAKE_OBJDUMP} -h $<TARGET_FILE:${TARGET}> >$<IF:$<BOOL:$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>>,$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>,$<TARGET_PROPERTY:${TARGET},NAME>>.dis
COMMAND ${CMAKE_OBJDUMP} -drwCSl $<TARGET_FILE:${TARGET}> >>$<IF:$<BOOL:$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>>,$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>,$<TARGET_PROPERTY:${TARGET},NAME>>.dis
)
endfunction()
# HDMI 8 pins starts from pin:
HDMI_BASE_PIN=6

pico_set_program_name(gameboy-color "GameBoy Emulator by xrip")
pico_set_program_version(gameboy-color "development version")
# TFT
TFT_CS_PIN=6
TFT_RST_PIN=8
TFT_LED_PIN=9
TFT_DC_PIN=10
TFT_DATA_PIN=12
TFT_CLK_PIN=13
)

#pico_set_binary_type(${PROJECT_NAME} copy_to_ram)
#pico_set_binary_type(${PROJECT_NAME} no_flash)
pico_enable_stdio_usb(${PROJECT_NAME} 1)
pico_enable_stdio_uart(${PROJECT_NAME} 0)
#pico_add_verbose_dis_output(${PROJECT_NAME})
#pico_add_bin_output(${PROJECT_NAME})
pico_add_uf2_output(${PROJECT_NAME})
target_link_options(${PROJECT_NAME} PRIVATE -Xlinker --print-memory-usage --data-sections)
pico_add_extra_outputs(${PROJECT_NAME})
10 changes: 5 additions & 5 deletions drivers/audio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
add_library(audio INTERFACE)

target_sources(audio INTERFACE
${CMAKE_CURRENT_LIST_DIR}/audio.c
${CMAKE_CURRENT_LIST_DIR}/audio.h
${CMAKE_CURRENT_LIST_DIR}/audio.c
${CMAKE_CURRENT_LIST_DIR}/audio.h
)

target_link_libraries(audio INTERFACE hardware_pio hardware_clocks)
target_link_libraries(audio INTERFACE hardware_pio hardware_clocks hardware_pwm)

target_include_directories(audio INTERFACE
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}
)

pico_generate_pio_header(audio
${CMAKE_CURRENT_LIST_DIR}/audio_i2s.pio
${CMAKE_CURRENT_LIST_DIR}/audio_i2s.pio
)
17 changes: 12 additions & 5 deletions drivers/audio/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ void i2s_init(i2s_config_t *i2s_config) {
gpio_set_function(i2s_config->clock_pin_base+1, func);

i2s_config->sm = pio_claim_unused_sm(i2s_config->pio, true);

uint offset = pio_add_program(i2s_config->pio, &audio_i2s_program);

audio_i2s_program_init(i2s_config->pio, i2s_config->sm , offset, i2s_config->data_pin , i2s_config->clock_pin_base);

/* Set PIO clock */
uint32_t system_clock_frequency = clock_get_hz(clk_sys);
uint32_t divider = system_clock_frequency * 4 / i2s_config->sample_freq; // avoid arithmetic overflow

#ifdef I2S_CS4334
uint offset = pio_add_program(i2s_config->pio, &audio_i2s_cs4334_program);
audio_i2s_cs4334_program_init(i2s_config->pio, i2s_config->sm , offset, i2s_config->data_pin , i2s_config->clock_pin_base);
divider >>= 3;
#else
uint offset = pio_add_program(i2s_config->pio, &audio_i2s_program);
audio_i2s_program_init(i2s_config->pio, i2s_config->sm , offset, i2s_config->data_pin , i2s_config->clock_pin_base);

#endif

pio_sm_set_clkdiv_int_frac(i2s_config->pio, i2s_config->sm , divider >> 8u, divider & 0xffu);

pio_sm_set_enabled(i2s_config->pio, i2s_config->sm, false);
Expand Down Expand Up @@ -156,7 +163,7 @@ void i2s_dma_write(i2s_config_t *i2s_config,const int16_t *samples) {
#ifdef AUDIO_PWM_PIN
for(uint16_t i=0;i<i2s_config->dma_trans_count*2;i++) {

i2s_config->dma_buf[i] = (65536/2+(samples[i]))>>(5+i2s_config->volume);
i2s_config->dma_buf[i] = (65536/2+(samples[i]))>>(4+i2s_config->volume);

}
#else
Expand Down
46 changes: 46 additions & 0 deletions drivers/audio/audio_i2s.pio
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@ bitloop0:
public entry_point:
set x, 14 side 0b11

.program audio_i2s_cs4334
.side_set 2
public entry_point:
out pins, 31 side 0b11
.wrap_target
loop:
set y,4 side 0b10
set x,15 side 0b11
l0:
out pins, 1 side 0b10
nop side 0b11
l1:
nop side 0b10
jmp y--,l1 side 0b11
set y,5 side 0b10
jmp x--,l0 side 0b11

set y,4 side 0b00
set x,15 side 0b01

l2:
out pins, 1 side 0b00
nop side 0b01
l3:
nop side 0b00
jmp y--,l3 side 0b01
set y,5 side 0b00

jmp x--,l2 side 0b01

% c-sdk {

static inline void audio_i2s_program_init(PIO pio, uint sm, uint offset, uint data_pin, uint clock_pin_base) {
Expand All @@ -60,4 +90,20 @@ static inline void audio_i2s_program_init(PIO pio, uint sm, uint offset, uint da
pio_sm_exec(pio, sm, pio_encode_jmp(offset + audio_i2s_offset_entry_point));
}


static inline void audio_i2s_cs4334_program_init(PIO pio, uint sm, uint offset, uint data_pin, uint clock_pin_base) {
pio_sm_config sm_config = audio_i2s_cs4334_program_get_default_config(offset);

sm_config_set_out_pins(&sm_config, data_pin, 1);
sm_config_set_sideset_pins(&sm_config, clock_pin_base);
sm_config_set_out_shift(&sm_config, false, true, 32);

pio_sm_init(pio, sm, offset, &sm_config);

uint pin_mask = (1u << data_pin) | (3u << clock_pin_base);
pio_sm_set_pindirs_with_mask(pio, sm, pin_mask, pin_mask);
pio_sm_set_pins(pio, sm, 0); // clear pins

//pio_sm_exec(pio, sm, pio_encode_jmp(offset + 0));
}
%}
2 changes: 1 addition & 1 deletion drivers/fatfs/fatfs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ if (NOT TARGET fatfs)
target_link_libraries(fatfs INTERFACE pico_stdlib hardware_clocks hardware_spi)
target_include_directories(fatfs INTERFACE ${CMAKE_CURRENT_LIST_DIR})

endif()
endif ()
17 changes: 9 additions & 8 deletions drivers/fatfs/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/ 3: f_lseek() function is removed in addition to 2. */


#define FF_USE_FIND 1
#define FF_USE_FIND 0
/* This option switches filtered directory read functions, f_findfirst() and
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */

Expand All @@ -34,11 +34,11 @@
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */


#define FF_USE_FASTSEEK 0
#define FF_USE_FASTSEEK 1
/* This option switches fast seek function. (0:Disable or 1:Enable) */


#define FF_USE_EXPAND 1
#define FF_USE_EXPAND 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */


Expand All @@ -56,7 +56,7 @@
/* This option switches f_forward() function. (0:Disable or 1:Enable) */


#define FF_USE_STRFUNC 1
#define FF_USE_STRFUNC 0
#define FF_PRINT_LLI 0
#define FF_PRINT_FLOAT 0
#define FF_STRF_ENCODE 3
Expand Down Expand Up @@ -84,7 +84,7 @@
/ Locale and Namespace Configurations
/---------------------------------------------------------------------------*/

#define FF_CODE_PAGE 932
#define FF_CODE_PAGE 866
/* This option specifies the OEM code page to be used on the target system.
/ Incorrect code page setting can cause a file open failure.
/
Expand Down Expand Up @@ -153,7 +153,7 @@
/ on character encoding. When LFN is not enabled, these options have no effect. */


#define FF_FS_RPATH 2
#define FF_FS_RPATH 0
/* This option configures support for relative path.
/
/ 0: Disable relative path and remove related functions.
Expand All @@ -166,6 +166,7 @@
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/

// TODO: calulate really required volumes
#define FF_VOLUMES 1
/* Number of volumes (logical drives) to be used. (1-10) */

Expand Down Expand Up @@ -238,9 +239,9 @@


#define FF_FS_NORTC 1
#define FF_NORTC_MON 1
#define FF_NORTC_MON 1
#define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2020
#define FF_NORTC_YEAR 2024
/* The option FF_FS_NORTC switches timestamp function. If the system does not have
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp
Expand Down
Loading

0 comments on commit d69457e

Please sign in to comment.