diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..ea43558 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: moononournation +custom: "https://paypal.me/moononournation" diff --git a/.gitignore b/.gitignore index f688810..dd5a3dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -build/ +build +sdkconfig sdkconfig.old *.nes - +.* \ No newline at end of file diff --git a/Makefile b/Makefile index 3281d01..6283749 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # project subdirectory. # -PROJECT_NAME := nesemu +PROJECT_NAME := esp32-nesemu include $(IDF_PATH)/make/project.mk diff --git a/README.rst b/README.rst index ca2a031..bab595d 100644 --- a/README.rst +++ b/README.rst @@ -1,3 +1,8 @@ +Arduino Compatible Version +========================== + +This is a special fork version of esp32-nesemu aim for revise source code compatible to Arduino as much as possible. + ESP32-NESEMU, a Nintendo Entertainment System emulator for the ESP32 ==================================================================== diff --git a/components/nofrendo-esp32/Kconfig.projbuild b/components/nofrendo-esp32/Kconfig.projbuild deleted file mode 100644 index bd18b15..0000000 --- a/components/nofrendo-esp32/Kconfig.projbuild +++ /dev/null @@ -1,168 +0,0 @@ -menu "Nofrendo ESP32-specific configuration" - -choice NOFRENDO_HARDWARE - prompt "Hardware to run on" - default ESP_WROVER_KIT_V2_ILI - help - This emulator can run on various types of hardware. Select what you have here. - -config HW_WROVERKIT_V1 - bool "ESP_Wrover_Kit v1 (red PCB)" - -config HW_WROVERKIT_V2 - bool "ESP_Wrover_Kit v2 (black PCB)" - -config HW_CUSTOM - bool "Custom hardware" - -endchoice - -choice HW_LCD_TYPE_SEL - prompt "LCD type" - depends on HW_CUSTOM || HW_WROVERKIT_V2 - -config HW_LCD_TYPE_ILI - bool "ILI9341 LCD" - -config HW_LCD_TYPE_ST - bool "ST7789V LCD" - -endchoice - -config HW_WROVERKIT - bool - default n if HW_CUSTOM - default y if HW_WROVERKIT_V1 - default y if HW_WROVERKIT_V2 - - -config HW_LCD_TYPE - int - default 0 if HW_WROVERKIT_V1 - default 0 if HW_LCD_TYPE_ILI - default 1 if HW_LCD_TYPE_ST - - -config HW_LCD_MOSI_GPIO_CUST - int "LCD MOSI pin" - depends on HW_CUSTOM - range 1 35 - default 25 - -config HW_LCD_CLK_GPIO_CUST - int "LCD CLK pin" - depends on HW_CUSTOM - range 1 35 - default 23 - -config HW_LCD_CS_GPIO_CUST - int "LCD CS pin" - depends on HW_CUSTOM - range 1 35 - default 19 - -config HW_LCD_DC_GPIO_CUST - int "LCD DC pin" - depends on HW_CUSTOM - range 1 35 - default 22 - -config HW_LCD_RESET_GPIO_CUST - int "LCD RESET pin" - depends on HW_CUSTOM - range 1 35 - default 21 - -config HW_LCD_BL_GPIO_CUST - int "LCD Backlight Enable pin" - depends on HW_CUSTOM - range 1 35 - default 5 - -config HW_INV_BL_CUST - bool "Invert backlight output" - default n - depends on HW_CUSTOM - - -config HW_INV_BL - bool - default HW_INBV_BL_CUST if HW_CUSTOM - default n if HW_WROVERKIT_V1 - default y if HW_WROVERKIT_V2 - -config HW_LCD_MISO_GPIO - int - default HW_LCD_MISO_GPIO_CUST if HW_CUSTOM - default 25 if HW_WROVERKIT - -config HW_LCD_MOSI_GPIO - int - default HW_LCD_MOSI_GPIO_CUST if HW_CUSTOM - default 23 if HW_WROVERKIT - -config HW_LCD_CLK_GPIO - int - default HW_LCD_CLK_GPIO_CUST if HW_CUSTOM - default 19 if HW_WROVERKIT - -config HW_LCD_CS_GPIO - int - default HW_LCD_CS_GPIO_CUST if HW_CUSTOM - default 22 if HW_WROVERKIT - -config HW_LCD_DC_GPIO - int - default HW_LCD_DC_GPIO_CUST if HW_CUSTOM - default 21 if HW_WROVERKIT - -config HW_LCD_RESET_GPIO - int - default HW_LCD_RESET_GPIO_CUST if HW_CUSTOM - default 18 if HW_WROVERKIT - -config HW_LCD_BL_GPIO - int - default HW_LCD_BL_GPIO_CUST if HW_CUSTOM - default 5 if HW_WROVERKIT - - -config SOUND_ENA - bool "Analog audio on GPIO26" - default n - help - ESP32 will output 0-3.3V analog audio signal on GPIO26. - - -config HW_PSX_ENA - bool "Enable PSX controller input" - default y - help - If you connect a PSX/PS2 controller to the following GPIOs, you can control the NES. - - -config HW_PSX_CLK - int "PSX controller CLK GPIO pin" - depends on HW_PSX_ENA - range 1 35 - default 14 - -config HW_PSX_DAT - int "PSX controller DATa GPIO pin" - depends on HW_PSX_ENA - range 1 35 - default 27 - -config HW_PSX_ATT - int "PSX controller ATTention GPIO pin" - depends on HW_PSX_ENA - range 1 35 - default 16 - -config HW_PSX_CMD - int "PSX controller CoMmanD GPIO pin" - depends on HW_PSX_ENA - range 1 35 - default 2 - -endmenu \ No newline at end of file diff --git a/components/nofrendo-esp32/component.mk b/components/nofrendo-esp32/component.mk deleted file mode 100644 index c0c11f3..0000000 --- a/components/nofrendo-esp32/component.mk +++ /dev/null @@ -1,12 +0,0 @@ -# -# Component Makefile -# -# This Makefile can be left empty. By default, it will take the sources in the -# src/ directory, compile them and link them into lib(subdirectory_name).a -# in the build directory. This behaviour is entirely configurable, -# please read the ESP-IDF documents if you need to do this. -# - -COMPONENT_DEPENDS := nofrendo -COMPONENT_ADD_INCLUDEDIRS := . - diff --git a/components/nofrendo-esp32/psxcontroller.c b/components/nofrendo-esp32/psxcontroller.c deleted file mode 100644 index e466296..0000000 --- a/components/nofrendo-esp32/psxcontroller.c +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "freertos/queue.h" - - -#include "driver/gpio.h" -#include "soc/gpio_struct.h" -#include "psxcontroller.h" -#include "sdkconfig.h" - -#define PSX_CLK CONFIG_HW_PSX_CLK -#define PSX_DAT CONFIG_HW_PSX_DAT -#define PSX_ATT CONFIG_HW_PSX_ATT -#define PSX_CMD CONFIG_HW_PSX_CMD - -#define DELAY() asm("nop; nop; nop; nop;nop; nop; nop; nop;nop; nop; nop; nop;nop; nop; nop; nop;") - - -#if CONFIG_HW_PSX_ENA - -/* Sends and receives a byte from/to the PSX controller using SPI */ -static int psxSendRecv(int send) { - int x; - int ret=0; - volatile int delay; - -#if 0 - while(1) { - GPIO.out_w1ts=(1<>=1; - send>>=1; - if (GPIO.in&(1< -#include -#include "sdkconfig.h" -#include "rom/ets_sys.h" -#include "rom/gpio.h" -#include "soc/gpio_reg.h" -#include "soc/gpio_sig_map.h" -#include "soc/gpio_struct.h" -#include "soc/io_mux_reg.h" -#include "soc/spi_reg.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "driver/periph_ctrl.h" -#include "spi_lcd.h" - -#define PIN_NUM_MISO CONFIG_HW_LCD_MISO_GPIO -#define PIN_NUM_MOSI CONFIG_HW_LCD_MOSI_GPIO -#define PIN_NUM_CLK CONFIG_HW_LCD_CLK_GPIO -#define PIN_NUM_CS CONFIG_HW_LCD_CS_GPIO -#define PIN_NUM_DC CONFIG_HW_LCD_DC_GPIO -#define PIN_NUM_RST CONFIG_HW_LCD_RESET_GPIO -#define PIN_NUM_BCKL CONFIG_HW_LCD_BL_GPIO -#define LCD_SEL_CMD() GPIO.out_w1tc = (1 << PIN_NUM_DC) // Low to send command -#define LCD_SEL_DATA() GPIO.out_w1ts = (1 << PIN_NUM_DC) // High to send data -#define LCD_RST_SET() GPIO.out_w1ts = (1 << PIN_NUM_RST) -#define LCD_RST_CLR() GPIO.out_w1tc = (1 << PIN_NUM_RST) - -#if CONFIG_HW_INV_BL -#define LCD_BKG_ON() GPIO.out_w1tc = (1 << PIN_NUM_BCKL) // Backlight ON -#define LCD_BKG_OFF() GPIO.out_w1ts = (1 << PIN_NUM_BCKL) //Backlight OFF -#else -#define LCD_BKG_ON() GPIO.out_w1ts = (1 << PIN_NUM_BCKL) // Backlight ON -#define LCD_BKG_OFF() GPIO.out_w1tc = (1 << PIN_NUM_BCKL) //Backlight OFF -#endif - -#define SPI_NUM 0x3 - -#define LCD_TYPE_ILI 0 -#define LCD_TYPE_ST 1 - - -static void spi_write_byte(const uint8_t data){ - SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 0x7, SPI_USR_MOSI_DBITLEN_S); - WRITE_PERI_REG((SPI_W0_REG(SPI_NUM)), data); - SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); - while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM))&SPI_USR); -} - -static void LCD_WriteCommand(const uint8_t cmd) -{ - LCD_SEL_CMD(); - spi_write_byte(cmd); -} - -static void LCD_WriteData(const uint8_t data) -{ - LCD_SEL_DATA(); - spi_write_byte(data); -} - -static void ILI9341_INITIAL () -{ - LCD_BKG_ON(); - //------------------------------------Reset Sequence-----------------------------------------// - - LCD_RST_SET(); - ets_delay_us(100000); - - LCD_RST_CLR(); - ets_delay_us(200000); - - LCD_RST_SET(); - ets_delay_us(200000); - - -#if (CONFIG_HW_LCD_TYPE == LCD_TYPE_ILI) - //************* Start Initial Sequence **********// - LCD_WriteCommand(0xCF); - LCD_WriteData(0x00); - LCD_WriteData(0x83); - LCD_WriteData(0X30); - - LCD_WriteCommand(0xED); - LCD_WriteData(0x64); - LCD_WriteData(0x03); - LCD_WriteData(0X12); - LCD_WriteData(0X81); - - LCD_WriteCommand(0xE8); - LCD_WriteData(0x85); - LCD_WriteData(0x01); //i - LCD_WriteData(0x79); //i - - LCD_WriteCommand(0xCB); - LCD_WriteData(0x39); - LCD_WriteData(0x2C); - LCD_WriteData(0x00); - LCD_WriteData(0x34); - LCD_WriteData(0x02); - - LCD_WriteCommand(0xF7); - LCD_WriteData(0x20); - - LCD_WriteCommand(0xEA); - LCD_WriteData(0x00); - LCD_WriteData(0x00); - - LCD_WriteCommand(0xC0); //Power control - LCD_WriteData(0x26); //i //VRH[5:0] - - LCD_WriteCommand(0xC1); //Power control - LCD_WriteData(0x11); //i //SAP[2:0];BT[3:0] - - LCD_WriteCommand(0xC5); //VCM control - LCD_WriteData(0x35); //i - LCD_WriteData(0x3E); //i - - LCD_WriteCommand(0xC7); //VCM control2 - LCD_WriteData(0xBE); //i //»òÕß B1h - - LCD_WriteCommand(0x36); // Memory Access Control - LCD_WriteData(0x28); //i //was 0x48 - - LCD_WriteCommand(0x3A); - LCD_WriteData(0x55); - - LCD_WriteCommand(0xB1); - LCD_WriteData(0x00); - LCD_WriteData(0x1B); //18 - - LCD_WriteCommand(0xF2); // 3Gamma Function Disable - LCD_WriteData(0x08); - - LCD_WriteCommand(0x26); //Gamma curve selected - LCD_WriteData(0x01); - - LCD_WriteCommand(0xE0); //Set Gamma - LCD_WriteData(0x1F); - LCD_WriteData(0x1A); - LCD_WriteData(0x18); - LCD_WriteData(0x0A); - LCD_WriteData(0x0F); - LCD_WriteData(0x06); - LCD_WriteData(0x45); - LCD_WriteData(0X87); - LCD_WriteData(0x32); - LCD_WriteData(0x0A); - LCD_WriteData(0x07); - LCD_WriteData(0x02); - LCD_WriteData(0x07); - LCD_WriteData(0x05); - LCD_WriteData(0x00); - - LCD_WriteCommand(0XE1); //Set Gamma - LCD_WriteData(0x00); - LCD_WriteData(0x25); - LCD_WriteData(0x27); - LCD_WriteData(0x05); - LCD_WriteData(0x10); - LCD_WriteData(0x09); - LCD_WriteData(0x3A); - LCD_WriteData(0x78); - LCD_WriteData(0x4D); - LCD_WriteData(0x05); - LCD_WriteData(0x18); - LCD_WriteData(0x0D); - LCD_WriteData(0x38); - LCD_WriteData(0x3A); - LCD_WriteData(0x1F); - - LCD_WriteCommand(0x2A); - LCD_WriteData(0x00); - LCD_WriteData(0x00); - LCD_WriteData(0x00); - LCD_WriteData(0xEF); - - LCD_WriteCommand(0x2B); - LCD_WriteData(0x00); - LCD_WriteData(0x00); - LCD_WriteData(0x01); - LCD_WriteData(0x3f); - LCD_WriteCommand(0x2C); - - LCD_WriteCommand(0xB7); - LCD_WriteData(0x07); - - LCD_WriteCommand(0xB6); // Display Function Control - LCD_WriteData(0x0A); //8 82 27 - LCD_WriteData(0x82); - LCD_WriteData(0x27); - LCD_WriteData(0x00); - - //LCD_WriteCommand(0xF6); //not there - //LCD_WriteData(0x01); - //LCD_WriteData(0x30); - -#endif -#if (CONFIG_HW_LCD_TYPE == LCD_TYPE_ST) - -//212 -//122 - LCD_WriteCommand(0x36); - LCD_WriteData((1<<5)|(1<<6)); //MV 1, MX 1 - - LCD_WriteCommand(0x3A); - LCD_WriteData(0x55); - - LCD_WriteCommand(0xB2); - LCD_WriteData(0x0c); - LCD_WriteData(0x0c); - LCD_WriteData(0x00); - LCD_WriteData(0x33); - LCD_WriteData(0x33); - - LCD_WriteCommand(0xB7); - LCD_WriteData(0x35); - - LCD_WriteCommand(0xBB); - LCD_WriteData(0x2B); - - LCD_WriteCommand(0xC0); - LCD_WriteData(0x2C); - - LCD_WriteCommand(0xC2); - LCD_WriteData(0x01); - LCD_WriteData(0xFF); - - LCD_WriteCommand(0xC3); - LCD_WriteData(0x11); - - LCD_WriteCommand(0xC4); - LCD_WriteData(0x20); - - LCD_WriteCommand(0xC6); - LCD_WriteData(0x0f); - - LCD_WriteCommand(0xD0); - LCD_WriteData(0xA4); - LCD_WriteData(0xA1); - - LCD_WriteCommand(0xE0); - LCD_WriteData(0xD0); - LCD_WriteData(0x00); - LCD_WriteData(0x05); - LCD_WriteData(0x0E); - LCD_WriteData(0x15); - LCD_WriteData(0x0D); - LCD_WriteData(0x37); - LCD_WriteData(0x43); - LCD_WriteData(0x47); - LCD_WriteData(0x09); - LCD_WriteData(0x15); - LCD_WriteData(0x12); - LCD_WriteData(0x16); - LCD_WriteData(0x19); - - LCD_WriteCommand(0xE1); - LCD_WriteData(0xD0); - LCD_WriteData(0x00); - LCD_WriteData(0x05); - LCD_WriteData(0x0D); - LCD_WriteData(0x0C); - LCD_WriteData(0x06); - LCD_WriteData(0x2D); - LCD_WriteData(0x44); - LCD_WriteData(0x40); - LCD_WriteData(0x0E); - LCD_WriteData(0x1C); - LCD_WriteData(0x18); - LCD_WriteData(0x16); - LCD_WriteData(0x19); - -#endif - - - LCD_WriteCommand(0x11); //Exit Sleep - ets_delay_us(100000); - LCD_WriteCommand(0x29); //Display on - ets_delay_us(100000); - - -} -//.............LCD API END---------- - -static void ili_gpio_init() -{ - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO21_U,2); //DC PIN - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO18_U,2); //RESET PIN - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO5_U,2); //BKL PIN - WRITE_PERI_REG(GPIO_ENABLE_W1TS_REG, BIT21|BIT18|BIT5); -} - -static void spi_master_init() -{ - periph_module_enable(PERIPH_VSPI_MODULE); - periph_module_enable(PERIPH_SPI_DMA_MODULE); - - ets_printf("lcd spi pin mux init ...\r\n"); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO19_U,2); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO23_U,2); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO22_U,2); - PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO25_U,2); - WRITE_PERI_REG(GPIO_ENABLE_W1TS_REG, BIT19|BIT23|BIT22); - - ets_printf("lcd spi signal init\r\n"); - gpio_matrix_in(PIN_NUM_MISO, VSPIQ_IN_IDX,0); - gpio_matrix_out(PIN_NUM_MOSI, VSPID_OUT_IDX,0,0); - gpio_matrix_out(PIN_NUM_CLK, VSPICLK_OUT_IDX,0,0); - gpio_matrix_out(PIN_NUM_CS, VSPICS0_OUT_IDX,0,0); - ets_printf("Hspi config\r\n"); - - CLEAR_PERI_REG_MASK(SPI_SLAVE_REG(SPI_NUM), SPI_TRANS_DONE << 5); - SET_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_CS_SETUP); - CLEAR_PERI_REG_MASK(SPI_PIN_REG(SPI_NUM), SPI_CK_IDLE_EDGE); - CLEAR_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_CK_OUT_EDGE); - CLEAR_PERI_REG_MASK(SPI_CTRL_REG(SPI_NUM), SPI_WR_BIT_ORDER); - CLEAR_PERI_REG_MASK(SPI_CTRL_REG(SPI_NUM), SPI_RD_BIT_ORDER); - CLEAR_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_DOUTDIN); - WRITE_PERI_REG(SPI_USER1_REG(SPI_NUM), 0); - SET_PERI_REG_BITS(SPI_CTRL2_REG(SPI_NUM), SPI_MISO_DELAY_MODE, 0, SPI_MISO_DELAY_MODE_S); - CLEAR_PERI_REG_MASK(SPI_SLAVE_REG(SPI_NUM), SPI_SLAVE_MODE); - - WRITE_PERI_REG(SPI_CLOCK_REG(SPI_NUM), (1 << SPI_CLKCNT_N_S) | (1 << SPI_CLKCNT_L_S));//40MHz - //WRITE_PERI_REG(SPI_CLOCK_REG(SPI_NUM), SPI_CLK_EQU_SYSCLK); // 80Mhz - - SET_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_CS_SETUP | SPI_CS_HOLD | SPI_USR_MOSI); - SET_PERI_REG_MASK(SPI_CTRL2_REG(SPI_NUM), ((0x4 & SPI_MISO_DELAY_NUM) << SPI_MISO_DELAY_NUM_S)); - CLEAR_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_USR_COMMAND); - SET_PERI_REG_BITS(SPI_USER2_REG(SPI_NUM), SPI_USR_COMMAND_BITLEN, 0, SPI_USR_COMMAND_BITLEN_S); - CLEAR_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_USR_ADDR); - SET_PERI_REG_BITS(SPI_USER1_REG(SPI_NUM), SPI_USR_ADDR_BITLEN, 0, SPI_USR_ADDR_BITLEN_S); - CLEAR_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_USR_MISO); - SET_PERI_REG_MASK(SPI_USER_REG(SPI_NUM), SPI_USR_MOSI); - char i; - for (i = 0; i < 16; ++i) { - WRITE_PERI_REG((SPI_W0_REG(SPI_NUM) + (i << 2)), 0); - } -} - -#define U16x2toU32(m,l) ((((uint32_t)(l>>8|(l&0xFF)<<8))<<16)|(m>>8|(m&0xFF)<<8)) - -extern uint16_t myPalette[]; - -void ili9341_write_frame(const uint16_t xs, const uint16_t ys, const uint16_t width, const uint16_t height, const uint8_t * data[]){ - int x, y; - int i; - uint16_t x1, y1; - uint32_t xv, yv, dc; - uint32_t temp[16]; - dc = (1 << PIN_NUM_DC); - - for (y=0; y -#include -#include -#include -//Nes stuff wants to define this as well... -#undef false -#undef true -#undef bool - - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "driver/i2s.h" -#include "sdkconfig.h" -#include - -#include - -#define DEFAULT_SAMPLERATE 22100 -#define DEFAULT_FRAGSIZE 128 - -#define DEFAULT_WIDTH 256 -#define DEFAULT_HEIGHT NES_VISIBLE_HEIGHT - - -TimerHandle_t timer; - -//Seemingly, this will be called only once. Should call func with a freq of frequency, -int osd_installtimer(int frequency, void *func, int funcsize, void *counter, int countersize) -{ - printf("Timer install, freq=%d\n", frequency); - timer=xTimerCreate("nes",configTICK_RATE_HZ/frequency, pdTRUE, NULL, func); - xTimerStart(timer, 0); - return 0; -} - - -/* -** Audio -*/ -static void (*audio_callback)(void *buffer, int length) = NULL; -#if CONFIG_SOUND_ENA -QueueHandle_t queue; -static uint16_t *audio_frame; -#endif - -static void do_audio_frame() { - -#if CONFIG_SOUND_ENA - int left=DEFAULT_SAMPLERATE/NES_REFRESH_RATE; - while(left) { - int n=DEFAULT_FRAGSIZE; - if (n>left) n=left; - audio_callback(audio_frame, n); //get more data - //16 bit mono -> 32-bit (16 bit r+l) - for (int i=n-1; i>=0; i--) { - audio_frame[i*2+1]=audio_frame[i]; - audio_frame[i*2]=audio_frame[i]; - } - i2s_write_bytes(0, audio_frame, 4*n, portMAX_DELAY); - left-=n; - } -#endif -} - -void osd_setsound(void (*playfunc)(void *buffer, int length)) -{ - //Indicates we should call playfunc() to get more data. - audio_callback = playfunc; -} - -static void osd_stopsound(void) -{ - audio_callback = NULL; -} - - -static int osd_init_sound(void) -{ -#if CONFIG_SOUND_ENA - audio_frame=malloc(4*DEFAULT_FRAGSIZE); - i2s_config_t cfg={ - .mode=I2S_MODE_DAC_BUILT_IN|I2S_MODE_TX|I2S_MODE_MASTER, - .sample_rate=DEFAULT_SAMPLERATE, - .bits_per_sample=I2S_BITS_PER_SAMPLE_16BIT, - .channel_format=I2S_CHANNEL_FMT_RIGHT_LEFT, - .communication_format=I2S_COMM_FORMAT_I2S_MSB, - .intr_alloc_flags=0, - .dma_buf_count=4, - .dma_buf_len=512 - }; - i2s_driver_install(0, &cfg, 4, &queue); - i2s_set_pin(0, NULL); - i2s_set_dac_mode(I2S_DAC_CHANNEL_LEFT_EN); - - //I2S enables *both* DAC channels; we only need DAC1. - //ToDo: still needed now I2S supports set_dac_mode? - CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_DAC_XPD_FORCE_M); - CLEAR_PERI_REG_MASK(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_XPD_DAC_M); - -#endif - - audio_callback = NULL; - - return 0; -} - -void osd_getsoundinfo(sndinfo_t *info) -{ - info->sample_rate = DEFAULT_SAMPLERATE; - info->bps = 16; -} - -/* -** Video -*/ - -static int init(int width, int height); -static void shutdown(void); -static int set_mode(int width, int height); -static void set_palette(rgb_t *pal); -static void clear(uint8 color); -static bitmap_t *lock_write(void); -static void free_write(int num_dirties, rect_t *dirty_rects); -static void custom_blit(bitmap_t *bmp, int num_dirties, rect_t *dirty_rects); -static char fb[1]; //dummy - -QueueHandle_t vidQueue; - -viddriver_t sdlDriver = -{ - "Simple DirectMedia Layer", /* name */ - init, /* init */ - shutdown, /* shutdown */ - set_mode, /* set_mode */ - set_palette, /* set_palette */ - clear, /* clear */ - lock_write, /* lock_write */ - free_write, /* free_write */ - custom_blit, /* custom_blit */ - false /* invalidate flag */ -}; - - -bitmap_t *myBitmap; - -void osd_getvideoinfo(vidinfo_t *info) -{ - info->default_width = DEFAULT_WIDTH; - info->default_height = DEFAULT_HEIGHT; - info->driver = &sdlDriver; -} - -/* flip between full screen and windowed */ -void osd_togglefullscreen(int code) -{ -} - -/* initialise video */ -static int init(int width, int height) -{ - return 0; -} - -static void shutdown(void) -{ -} - -/* set a video mode */ -static int set_mode(int width, int height) -{ - return 0; -} - -uint16 myPalette[256]; - -/* copy nes palette over to hardware */ -static void set_palette(rgb_t *pal) -{ - uint16 c; - - int i; - - for (i = 0; i < 256; i++) - { - c=(pal[i].b>>3)+((pal[i].g>>2)<<5)+((pal[i].r>>3)<<11); - //myPalette[i]=(c>>8)|((c&0xff)<<8); - myPalette[i]=c; - } - -} - -/* clear all frames to a particular color */ -static void clear(uint8 color) -{ -// SDL_FillRect(mySurface, 0, color); -} - - - -/* acquire the directbuffer for writing */ -static bitmap_t *lock_write(void) -{ -// SDL_LockSurface(mySurface); - myBitmap = bmp_createhw((uint8*)fb, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_WIDTH*2); - return myBitmap; -} - -/* release the resource */ -static void free_write(int num_dirties, rect_t *dirty_rects) -{ - bmp_destroy(&myBitmap); -} - - -static void custom_blit(bitmap_t *bmp, int num_dirties, rect_t *dirty_rects) { - xQueueSend(vidQueue, &bmp, 0); - do_audio_frame(); -} - - -//This runs on core 1. -static void videoTask(void *arg) { - int x, y; - bitmap_t *bmp=NULL; - x = (320-DEFAULT_WIDTH)/2; - y = ((240-DEFAULT_HEIGHT)/2); - while(1) { -// xQueueReceive(vidQueue, &bmp, portMAX_DELAY);//skip one frame to drop to 30 - xQueueReceive(vidQueue, &bmp, portMAX_DELAY); - ili9341_write_frame(x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT, (const uint8_t **)bmp->line); - } -} - - -/* -** Input -*/ - -static void osd_initinput() -{ - psxcontrollerInit(); -} - -void osd_getinput(void) -{ - const int ev[16]={ - event_joypad1_select,0,0,event_joypad1_start,event_joypad1_up,event_joypad1_right,event_joypad1_down,event_joypad1_left, - 0,0,0,0,event_soft_reset,event_joypad1_a,event_joypad1_b,event_hard_reset - }; - static int oldb=0xffff; - int b=psxReadInput(); - int chg=b^oldb; - int x; - oldb=b; - event_t evh; -// printf("Input: %x\n", b); - for (x=0; x<16; x++) { - if (chg&1) { - evh=event_get(ev[x]); - if (evh) evh((b&1)?INP_STATE_BREAK:INP_STATE_MAKE); - } - chg>>=1; - b>>=1; - } -} - -static void osd_freeinput(void) -{ -} - -void osd_getmouse(int *x, int *y, int *button) -{ -} - -/* -** Shutdown -*/ - -/* this is at the bottom, to eliminate warnings */ -void osd_shutdown() -{ - osd_stopsound(); - osd_freeinput(); -} - -static int logprint(const char *string) -{ - return printf("%s", string); -} - -/* -** Startup -*/ - -int osd_init() -{ - log_chain_logfunc(logprint); - - if (osd_init_sound()) - return -1; - - ili9341_init(); - ili9341_write_frame(0,0,320,240,NULL); - vidQueue=xQueueCreate(1, sizeof(bitmap_t *)); - xTaskCreatePinnedToCore(&videoTask, "videoTask", 2048, NULL, 5, NULL, 1); - osd_initinput(); - return 0; -} diff --git a/components/nofrendo/bitmap.c b/components/nofrendo/bitmap.c index 58d32e2..97d44ad 100644 --- a/components/nofrendo/bitmap.c +++ b/components/nofrendo/bitmap.c @@ -25,8 +25,9 @@ #include #include -#include -#include + +#include "noftypes.h" +#include "bitmap.h" void bmp_clear(const bitmap_t *bitmap, uint8 color) { @@ -44,7 +45,7 @@ static bitmap_t *_make_bitmap(uint8 *data_addr, bool hw, int width, return NULL; /* Make sure to add in space for line pointers */ - bitmap = malloc(sizeof(bitmap_t) + (sizeof(uint8 *) * height)); + bitmap = NOFRENDO_MALLOC(sizeof(bitmap_t) + (sizeof(uint8 *) * height)); if (NULL == bitmap) return NULL; @@ -81,7 +82,7 @@ bitmap_t *bmp_create(int width, int height, int overdraw) int pitch; pitch = width + (overdraw * 2); /* left and right */ - addr = malloc((pitch * height) + 3); /* add max 32-bit aligned adjustment */ + addr = NOFRENDO_MALLOC((pitch * height) + 3); /* add max 32-bit aligned adjustment */ if (NULL == addr) return NULL; @@ -100,8 +101,8 @@ void bmp_destroy(bitmap_t **bitmap) if (*bitmap) { if ((*bitmap)->data && false == (*bitmap)->hardware) - free((*bitmap)->data); - free(*bitmap); + NOFRENDO_FREE((*bitmap)->data); + NOFRENDO_FREE(*bitmap); *bitmap = NULL; } } diff --git a/components/nofrendo/bitmap.h b/components/nofrendo/bitmap.h index fe3c624..4e3ce7a 100644 --- a/components/nofrendo/bitmap.h +++ b/components/nofrendo/bitmap.h @@ -26,7 +26,7 @@ #ifndef _BITMAP_H_ #define _BITMAP_H_ -#include +#include "noftypes.h" /* a bitmap rectangle */ typedef struct rect_s diff --git a/components/nofrendo/component.mk b/components/nofrendo/component.mk index 0d213e5..eb5f5f2 100644 --- a/components/nofrendo/component.mk +++ b/components/nofrendo/component.mk @@ -10,4 +10,4 @@ COMPONENT_ADD_INCLUDEDIRS := cpu libsnss nes sndhrdw . COMPONENT_SRCDIRS := cpu libsnss nes sndhrdw mappers . -CFLAGS += -Wno-error=char-subscripts -Wno-error=attributes -DNOFRENDO_DEBUG +CFLAGS += -Wno-error=char-subscripts -Wno-error=attributes diff --git a/components/nofrendo/config.c b/components/nofrendo/config.c index 1739575..29a5307 100644 --- a/components/nofrendo/config.c +++ b/components/nofrendo/config.c @@ -8,11 +8,11 @@ #include #include -#include -#include -#include -#include -#include +#include "noftypes.h" +#include "log.h" +#include "osd.h" +#include "nofconfig.h" +#include "version.h" typedef struct myvar_s { @@ -29,19 +29,19 @@ static void my_destroy(myvar_t **var) ASSERT(*var); if ((*var)->group) - free((*var)->group); + NOFRENDO_FREE((*var)->group); if ((*var)->key) - free((*var)->key); + NOFRENDO_FREE((*var)->key); if ((*var)->value) - free((*var)->value); - free(*var); + NOFRENDO_FREE((*var)->value); + NOFRENDO_FREE(*var); } static myvar_t *my_create(const char *group, const char *key, const char *value) { myvar_t *var; - var = malloc(sizeof(*var)); + var = NOFRENDO_MALLOC(sizeof(*var)); if (NULL == var) { return 0; @@ -50,9 +50,9 @@ static myvar_t *my_create(const char *group, const char *key, const char *value) var->less = var->greater = NULL; var->group = var->key = var->value = NULL; - if ((var->group = malloc(strlen(group) + 1)) - && (var->key = malloc(strlen(key) + 1)) - && (var->value = malloc(strlen(value) + 1))) + if ((var->group = NOFRENDO_MALLOC(strlen(group) + 1)) + && (var->key = NOFRENDO_MALLOC(strlen(key) + 1)) + && (var->value = NOFRENDO_MALLOC(strlen(value) + 1))) { strcpy(var->group, group); strcpy(var->key, key); @@ -146,13 +146,13 @@ static char *my_getline(FILE *stream) if (NULL == (fgets(buf, sizeof(buf), stream))) { if (dynamic) - free(dynamic); + NOFRENDO_FREE(dynamic); return 0; } if (NULL == dynamic) { - dynamic = malloc(strlen(buf) + 1); + dynamic = NOFRENDO_MALLOC(strlen(buf) + 1); if (NULL == dynamic) { return 0; @@ -163,12 +163,12 @@ static char *my_getline(FILE *stream) { /* a mini-version of realloc that works with our memory manager */ char *temp = NULL; - temp = malloc(strlen(dynamic) + strlen(buf) + 1); + temp = NOFRENDO_MALLOC(strlen(dynamic) + strlen(buf) + 1); if (NULL == temp) return 0; strcpy(temp, dynamic); - free(dynamic); + NOFRENDO_FREE(dynamic); dynamic = temp; strcat(dynamic, buf); @@ -220,14 +220,14 @@ static int load_config(char *filename) case '[': if (group) - free(group); + NOFRENDO_FREE(group); group = ++s; s = strchr(s, ']'); if (NULL == s) { - log_printf("load_config: missing ']' after group\n"); + nofrendo_log_printf("load_config: missing ']' after group\n"); s = group + strlen(group); } else @@ -235,7 +235,7 @@ static int load_config(char *filename) *s++ = '\0'; } - if ((value = malloc(strlen(group) + 1))) + if ((value = NOFRENDO_MALLOC(strlen(group) + 1))) { strcpy(value, group); } @@ -247,7 +247,7 @@ static int load_config(char *filename) s = strchr(s, '='); if (NULL == s) { - log_printf("load_config: missing '=' after key\n"); + nofrendo_log_printf("load_config: missing '=' after key\n"); s = key + strlen(key); } else @@ -268,7 +268,7 @@ static int load_config(char *filename) myvar_t *var = my_create(group ? group : "", key, s); if (NULL == var) { - log_printf("load_config: my_create failed\n"); + nofrendo_log_printf("load_config: my_create failed\n"); return -1; } @@ -278,11 +278,11 @@ static int load_config(char *filename) } } while (*s); - free(line); + NOFRENDO_FREE(line); } if (group) - free(group); + NOFRENDO_FREE(group); fclose(config_file); } @@ -299,7 +299,7 @@ static int save_config(char *filename) config_file = fopen(filename, "w"); if (NULL == config_file) { - log_printf("save_config failed\n"); + nofrendo_log_printf("save_config failed\n"); return -1; } @@ -337,7 +337,7 @@ static void write_int(const char *group, const char *key, int value) var = my_create(group, key, buf); if (NULL == var) { - log_printf("write_int failed\n"); + nofrendo_log_printf("write_int failed\n"); return; } @@ -371,7 +371,7 @@ static void write_string(const char *group, const char *key, const char *value) var = my_create(group, key, value); if (NULL == var) { - log_printf("write_string failed\n"); + nofrendo_log_printf("write_string failed\n"); return; } diff --git a/components/nofrendo/cpu/dis6502.c b/components/nofrendo/cpu/dis6502.c index 197b425..9e8eda1 100644 --- a/components/nofrendo/cpu/dis6502.c +++ b/components/nofrendo/cpu/dis6502.c @@ -24,7 +24,8 @@ */ #include -#include + +#include "../noftypes.h" #include "nes6502.h" #include "dis6502.h" @@ -518,7 +519,7 @@ char *nes6502_disasm(uint32 PC, uint8 P, uint8 A, uint8 X, uint8 Y, uint8 S) ** made sure last line of all source files is a newline ** ** Revision 1.7 2000/07/11 04:26:23 matt -** rewrote to fill up a static buffer, rather than use log_printf +** rewrote to fill up a static buffer, rather than use nofrendo_log_printf ** ** Revision 1.6 2000/07/10 05:15:58 matt ** fixed a bug in indirect x/y disassembly diff --git a/components/nofrendo/cpu/dis6502.h b/components/nofrendo/cpu/dis6502.h index 83f0abf..58f7a5e 100644 --- a/components/nofrendo/cpu/dis6502.h +++ b/components/nofrendo/cpu/dis6502.h @@ -50,7 +50,7 @@ extern char *nes6502_disasm(uint32 PC, uint8 P, uint8 A, uint8 X, uint8 Y, uint8 ** made sure last line of all source files is a newline ** ** Revision 1.5 2000/07/11 04:26:23 matt -** rewrote to fill up a static buffer, rather than use log_printf +** rewrote to fill up a static buffer, rather than use nofrendo_log_printf ** ** Revision 1.4 2000/06/09 15:12:25 matt ** initial revision diff --git a/components/nofrendo/cpu/nes6502.c b/components/nofrendo/cpu/nes6502.c index 72be869..5f2a4b9 100644 --- a/components/nofrendo/cpu/nes6502.c +++ b/components/nofrendo/cpu/nes6502.c @@ -1,3 +1,5 @@ +#pragma GCC optimize ("O3") + /* ** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com) ** @@ -24,7 +26,6 @@ */ -#include #include "nes6502.h" #include "dis6502.h" @@ -1341,7 +1342,7 @@ uint32 nes6502_getcycles(bool reset_flag) #define OPCODE_END \ if (remaining_cycles <= 0) \ goto end_execute; \ - log_printf(nes6502_disasm(PC, COMBINE_FLAGS(), A, X, Y, S)); \ + nofrendo_log_printf(nes6502_disasm(PC, COMBINE_FLAGS(), A, X, Y, S)); \ goto *opcode_table[bank_readbyte(PC++)]; #else /* !NES6520_DISASM */ @@ -1451,7 +1452,7 @@ int nes6502_execute(int timeslice_cycles) while (remaining_cycles > 0) { #ifdef NES6502_DISASM - log_printf(nes6502_disasm(PC, COMBINE_FLAGS(), A, X, Y, S)); + nofrendo_log_printf(nes6502_disasm(PC, COMBINE_FLAGS(), A, X, Y, S)); #endif /* NES6502_DISASM */ /* Fetch and execute instruction */ diff --git a/components/nofrendo/cpu/nes6502.h b/components/nofrendo/cpu/nes6502.h index c2245a1..a11fa89 100644 --- a/components/nofrendo/cpu/nes6502.h +++ b/components/nofrendo/cpu/nes6502.h @@ -29,7 +29,7 @@ #ifndef _NES6502_H_ #define _NES6502_H_ -#include +#include "../noftypes.h" /* Define this to enable decimal mode in ADC / SBC (not needed in NES) */ /*#define NES6502_DECIMAL*/ diff --git a/components/nofrendo/event.c b/components/nofrendo/event.c index 2bad388..4e12ca3 100644 --- a/components/nofrendo/event.c +++ b/components/nofrendo/event.c @@ -24,17 +24,18 @@ */ #include -#include -#include -#include -#include -#include + +#include "noftypes.h" +#include "event.h" +#include "nofrendo.h" +#include "gui.h" +#include "osd.h" /* TODO: put system specific stuff in their own files... */ -#include -#include -#include -#include +#include "nes/nes.h" +#include "nes/nesinput.h" +#include "nes/nes_pal.h" +#include "nes/nesstate.h" /* pointer to our current system's event handler table */ static event_t *system_events = NULL; diff --git a/components/nofrendo/event.h b/components/nofrendo/event.h index 929c60b..9710412 100644 --- a/components/nofrendo/event.h +++ b/components/nofrendo/event.h @@ -27,7 +27,7 @@ #ifndef _EVENT_H_ #define _EVENT_H_ -#include +#include "nofrendo.h" enum { diff --git a/components/nofrendo/gui.c b/components/nofrendo/gui.c index 317229d..e89278b 100644 --- a/components/nofrendo/gui.c +++ b/components/nofrendo/gui.c @@ -26,19 +26,20 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include +#include "noftypes.h" +#include "nes/nes_ppu.h" +#include "sndhrdw/nes_apu.h" +#include "nes/nesinput.h" +#include "nes/nes.h" +#include "log.h" +#include "osd.h" -#include -#include -#include +#include "bitmap.h" + +#include "gui.h" +#include "gui_elem.h" +#include "vid_drv.h" /* TODO: oh god */ /* 8-bit GUI color table */ @@ -61,8 +62,8 @@ rgb_t gui_pal[GUI_TOTALCOLORS] = }; /**************************************************************/ -#include -#include +#include "pcx.h" +#include "nes/nesstate.h" static bool option_drawsprites = true; /* save a PCX snapshot */ @@ -346,7 +347,7 @@ static void gui_tickdec(void) { #ifdef NOFRENDO_DEBUG static int hertz_ticks = 0; -#endif +#endif /* !NOFRENDO_DEBUG */ int ticks = gui_ticks; if (0 == ticks) @@ -360,9 +361,9 @@ static void gui_tickdec(void) if (hertz_ticks >= (10 * gui_refresh)) { hertz_ticks -= (10 * gui_refresh); - mem_checkblocks(); + // mem_checkblocks(); } -#endif +#endif /* !NOFRENDO_DEBUG */ /* TODO: bleh */ if (msg.ttl > 0) @@ -592,7 +593,7 @@ void gui_sendmsg(int color, char *format, ...) log_print("GUI: "); log_print(msg.text); log_print("\n"); -#endif +#endif /* !NOFRENDO_DEBUG */ va_end(arg); diff --git a/components/nofrendo/gui.h b/components/nofrendo/gui.h index 4875e77..c8c1e5a 100644 --- a/components/nofrendo/gui.h +++ b/components/nofrendo/gui.h @@ -52,7 +52,7 @@ enum #define GUI_TOTALCOLORS (GUI_LASTENTRY - GUI_FIRSTENTRY) /* TODO: bleh */ -#include +#include "bitmap.h" extern rgb_t gui_pal[GUI_TOTALCOLORS]; #define MAX_MSG_LENGTH 256 diff --git a/components/nofrendo/gui_elem.c b/components/nofrendo/gui_elem.c index 5592b6c..ad294b3 100644 --- a/components/nofrendo/gui_elem.c +++ b/components/nofrendo/gui_elem.c @@ -24,9 +24,10 @@ */ #include -#include -#include -#include + +#include "noftypes.h" +#include "gui.h" +#include "gui_elem.h" #define SMALL_FONT_KERN 6 #define SMALL_FONT_HEIGHT 6 diff --git a/components/nofrendo/intro.c b/components/nofrendo/intro.c index 73cf5a6..ee72189 100644 --- a/components/nofrendo/intro.c +++ b/components/nofrendo/intro.c @@ -25,8 +25,9 @@ #include #include -#include -#include + +#include "noftypes.h" +#include "intro.h" #define CODE_SIZE 0x4000 #define VROM_SIZE 0x2000 @@ -326,7 +327,7 @@ static uint8 *intro_getrom(void) { uint8 *rom; - rom = malloc(CODE_SIZE); + rom = NOFRENDO_MALLOC(CODE_SIZE); if (NULL != rom) { /* good measure */ @@ -346,7 +347,7 @@ static uint8 *intro_getvrom(void) { uint8 *vrom; - vrom = malloc(VROM_SIZE); + vrom = NOFRENDO_MALLOC(VROM_SIZE); if (NULL != vrom) { memcpy(vrom, intro_vrom, sizeof(intro_vrom)); diff --git a/components/nofrendo/intro.h b/components/nofrendo/intro.h index 79575d7..5db801b 100644 --- a/components/nofrendo/intro.h +++ b/components/nofrendo/intro.h @@ -26,7 +26,7 @@ #ifndef _INTRO_H_ #define _INTRO_H_ -#include +#include "nes/nes_rom.h" extern void intro_get_header(rominfo_t *rominfo); extern int intro_get_rom(rominfo_t *rominfo); diff --git a/components/nofrendo/libsnss/libsnss.c b/components/nofrendo/libsnss/libsnss.c index 5541d24..d58a28e 100644 --- a/components/nofrendo/libsnss/libsnss.c +++ b/components/nofrendo/libsnss/libsnss.c @@ -12,40 +12,41 @@ #include #include #include -#include + +#include "libsnss.h" /**************************************************************************/ /* This section deals with endian-specific code. */ /**************************************************************************/ static unsigned int -swap32 (unsigned int source) +swap32(unsigned int source) { #ifdef USE_LITTLE_ENDIAN char buffer[4]; - - buffer[0] = ((char *) &source)[3]; - buffer[1] = ((char *) &source)[2]; - buffer[2] = ((char *) &source)[1]; - buffer[3] = ((char *) &source)[0]; - - return *((unsigned int *) buffer); -#else /* !USE_LITTLE_ENDIAN */ + + buffer[0] = ((char *)&source)[3]; + buffer[1] = ((char *)&source)[2]; + buffer[2] = ((char *)&source)[1]; + buffer[3] = ((char *)&source)[0]; + + return *((unsigned int *)buffer); +#else /* !USE_LITTLE_ENDIAN */ return source; #endif /* !USE_LITTLE_ENDIAN */ } static unsigned short -swap16 (unsigned short source) +swap16(unsigned short source) { #ifdef USE_LITTLE_ENDIAN char buffer[2]; - - buffer[0] = ((char *) &source)[1]; - buffer[1] = ((char *) &source)[0]; - return *((unsigned short *) buffer); -#else /* !USE_LITTLE_ENDIAN */ + buffer[0] = ((char *)&source)[1]; + buffer[1] = ((char *)&source)[0]; + + return *((unsigned short *)buffer); +#else /* !USE_LITTLE_ENDIAN */ return source; #endif /* !USE_LITTLE_ENDIAN */ } @@ -54,26 +55,26 @@ swap16 (unsigned short source) /* support functions */ /**************************************************************************/ -#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) /**************************************************************************/ static SNSS_RETURN_CODE -SNSS_ReadBlockHeader (SnssBlockHeader *header, SNSS_FILE *snssFile) +SNSS_ReadBlockHeader(SnssBlockHeader *header, SNSS_FILE *snssFile) { char headerBytes[12]; - if (fread (headerBytes, 12, 1, snssFile->fp) != 1) + if (fread(headerBytes, 12, 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } - strncpy (header->tag, &headerBytes[0], TAG_LENGTH); + strncpy(header->tag, &headerBytes[0], TAG_LENGTH); header->tag[4] = '\0'; - header->blockVersion = *((unsigned int *) &headerBytes[4]); - header->blockVersion = swap32 (header->blockVersion); - header->blockLength = *((unsigned int *) &headerBytes[8]); - header->blockLength = swap32 (header->blockLength); + header->blockVersion = *((unsigned int *)&headerBytes[4]); + header->blockVersion = swap32(header->blockVersion); + header->blockLength = *((unsigned int *)&headerBytes[8]); + header->blockLength = swap32(header->blockLength); return SNSS_OK; } @@ -81,26 +82,26 @@ SNSS_ReadBlockHeader (SnssBlockHeader *header, SNSS_FILE *snssFile) /**************************************************************************/ static SNSS_RETURN_CODE -SNSS_WriteBlockHeader (SnssBlockHeader *header, SNSS_FILE *snssFile) +SNSS_WriteBlockHeader(SnssBlockHeader *header, SNSS_FILE *snssFile) { char headerBytes[12]; unsigned int tempInt; - strncpy (&headerBytes[0], header->tag, TAG_LENGTH); + strncpy(&headerBytes[0], header->tag, TAG_LENGTH); - tempInt = swap32 (header->blockVersion); - headerBytes[4] = ((char *) &tempInt)[0]; - headerBytes[5] = ((char *) &tempInt)[1]; - headerBytes[6] = ((char *) &tempInt)[2]; - headerBytes[7] = ((char *) &tempInt)[3]; + tempInt = swap32(header->blockVersion); + headerBytes[4] = ((char *)&tempInt)[0]; + headerBytes[5] = ((char *)&tempInt)[1]; + headerBytes[6] = ((char *)&tempInt)[2]; + headerBytes[7] = ((char *)&tempInt)[3]; - tempInt = swap32 (header->blockLength); - headerBytes[8] = ((char *) &tempInt)[0]; - headerBytes[9] = ((char *) &tempInt)[1]; - headerBytes[10] = ((char *) &tempInt)[2]; - headerBytes[11] = ((char *) &tempInt)[3]; + tempInt = swap32(header->blockLength); + headerBytes[8] = ((char *)&tempInt)[0]; + headerBytes[9] = ((char *)&tempInt)[1]; + headerBytes[10] = ((char *)&tempInt)[2]; + headerBytes[11] = ((char *)&tempInt)[3]; - if (fwrite (headerBytes, 12, 1, snssFile->fp) != 1) + if (fwrite(headerBytes, 12, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -111,7 +112,7 @@ SNSS_WriteBlockHeader (SnssBlockHeader *header, SNSS_FILE *snssFile) /**************************************************************************/ const char * -SNSS_GetErrorString (SNSS_RETURN_CODE code) +SNSS_GetErrorString(SNSS_RETURN_CODE code) { switch (code) { @@ -148,48 +149,48 @@ SNSS_GetErrorString (SNSS_RETURN_CODE code) /* functions for reading and writing SNSS file headers */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadFileHeader (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadFileHeader(SNSS_FILE *snssFile) { - if (fread (snssFile->headerBlock.tag, 4, 1, snssFile->fp) != 1) + if (fread(snssFile->headerBlock.tag, 4, 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } - - if (0 != strncmp(snssFile->headerBlock.tag, "SNSS", 4)) + + if (0 != strncmp(snssFile->headerBlock.tag, "SNSS", 5)) { return SNSS_BAD_FILE_TAG; } - + snssFile->headerBlock.tag[4] = '\0'; - if (fread (&snssFile->headerBlock.numberOfBlocks, sizeof (unsigned int), 1, snssFile->fp) != 1) + if (fread(&snssFile->headerBlock.numberOfBlocks, sizeof(unsigned int), 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } - snssFile->headerBlock.numberOfBlocks = swap32 (snssFile->headerBlock.numberOfBlocks); + snssFile->headerBlock.numberOfBlocks = swap32(snssFile->headerBlock.numberOfBlocks); return SNSS_OK; } /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteFileHeader (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteFileHeader(SNSS_FILE *snssFile) { unsigned int tempInt; char writeBuffer[8]; /* always place the SNSS tag in this field */ - strncpy (&writeBuffer[0], "SNSS", 4); - tempInt = swap32 (snssFile->headerBlock.numberOfBlocks); - writeBuffer[4] = ((char *) &tempInt)[0]; - writeBuffer[5] = ((char *) &tempInt)[1]; - writeBuffer[6] = ((char *) &tempInt)[2]; - writeBuffer[7] = ((char *) &tempInt)[3]; + strncpy(&writeBuffer[0], "SNSS", 5); + tempInt = swap32(snssFile->headerBlock.numberOfBlocks); + writeBuffer[4] = ((char *)&tempInt)[0]; + writeBuffer[5] = ((char *)&tempInt)[1]; + writeBuffer[6] = ((char *)&tempInt)[2]; + writeBuffer[7] = ((char *)&tempInt)[3]; - if (fwrite (writeBuffer, 8, 1, snssFile->fp) != 1) + if (fwrite(writeBuffer, 8, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -201,32 +202,32 @@ SNSS_WriteFileHeader (SNSS_FILE *snssFile) /* general file manipulation functions */ /**************************************************************************/ SNSS_RETURN_CODE -SNSS_OpenFile (SNSS_FILE **snssFile, const char *filename, SNSS_OPEN_MODE mode) +SNSS_OpenFile(SNSS_FILE **snssFile, const char *filename, SNSS_OPEN_MODE mode) { - *snssFile = malloc(sizeof(SNSS_FILE)); + *snssFile = NOFRENDO_MALLOC(sizeof(SNSS_FILE)); if (NULL == *snssFile) { return SNSS_OUT_OF_MEMORY; } - + /* zero the memory */ - memset (*snssFile, 0, sizeof(SNSS_FILE)); + memset(*snssFile, 0, sizeof(SNSS_FILE)); (*snssFile)->mode = mode; if (SNSS_OPEN_READ == mode) { - (*snssFile)->fp = fopen (filename, "rb"); + (*snssFile)->fp = fopen(filename, "rb"); } else { - (*snssFile)->fp = fopen (filename, "wb"); + (*snssFile)->fp = fopen(filename, "wb"); (*snssFile)->headerBlock.numberOfBlocks = 0; } if (NULL == (*snssFile)->fp) { - free(*snssFile); + NOFRENDO_FREE(*snssFile); *snssFile = NULL; return SNSS_OPEN_FAILED; } @@ -244,7 +245,7 @@ SNSS_OpenFile (SNSS_FILE **snssFile, const char *filename, SNSS_OPEN_MODE mode) /**************************************************************************/ SNSS_RETURN_CODE -SNSS_CloseFile (SNSS_FILE **snssFile) +SNSS_CloseFile(SNSS_FILE **snssFile) { int prevLoc; SNSS_RETURN_CODE code; @@ -269,12 +270,12 @@ SNSS_CloseFile (SNSS_FILE **snssFile) fseek((*snssFile)->fp, prevLoc, SEEK_SET); } - if (fclose ((*snssFile)->fp) != 0) + if (fclose((*snssFile)->fp) != 0) { return SNSS_CLOSE_FAILED; } - free(*snssFile); + NOFRENDO_FREE(*snssFile); *snssFile = NULL; return SNSS_OK; @@ -282,45 +283,45 @@ SNSS_CloseFile (SNSS_FILE **snssFile) /**************************************************************************/ -SNSS_RETURN_CODE -SNSS_GetNextBlockType (SNSS_BLOCK_TYPE *blockType, SNSS_FILE *snssFile) +SNSS_RETURN_CODE +SNSS_GetNextBlockType(SNSS_BLOCK_TYPE *blockType, SNSS_FILE *snssFile) { char tagBuffer[TAG_LENGTH + 1]; - if (fread (tagBuffer, TAG_LENGTH, 1, snssFile->fp) != 1) + if (fread(tagBuffer, TAG_LENGTH, 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } tagBuffer[TAG_LENGTH] = '\0'; /* reset the file pointer to the start of the block */ - if (fseek (snssFile->fp, -TAG_LENGTH, SEEK_CUR) != 0) + if (fseek(snssFile->fp, -TAG_LENGTH, SEEK_CUR) != 0) { return SNSS_READ_FAILED; } /* figure out which type of block it is */ - if (strcmp (tagBuffer, "BASR") == 0) + if (strcmp(tagBuffer, "BASR") == 0) { *blockType = SNSS_BASR; } - else if (strcmp (tagBuffer, "VRAM") == 0) + else if (strcmp(tagBuffer, "VRAM") == 0) { *blockType = SNSS_VRAM; } - else if (strcmp (tagBuffer, "SRAM") == 0) + else if (strcmp(tagBuffer, "SRAM") == 0) { *blockType = SNSS_SRAM; } - else if (strcmp (tagBuffer, "MPRD") == 0) + else if (strcmp(tagBuffer, "MPRD") == 0) { *blockType = SNSS_MPRD; } - else if (strcmp (tagBuffer, "CNTR") == 0) + else if (strcmp(tagBuffer, "CNTR") == 0) { *blockType = SNSS_CNTR; } - else if (strcmp (tagBuffer, "SOUN") == 0) + else if (strcmp(tagBuffer, "SOUN") == 0) { *blockType = SNSS_SOUN; } @@ -334,26 +335,26 @@ SNSS_GetNextBlockType (SNSS_BLOCK_TYPE *blockType, SNSS_FILE *snssFile) /**************************************************************************/ -SNSS_RETURN_CODE -SNSS_SkipNextBlock (SNSS_FILE *snssFile) +SNSS_RETURN_CODE +SNSS_SkipNextBlock(SNSS_FILE *snssFile) { unsigned int blockLength; /* skip the block's tag and version */ - if (fseek (snssFile->fp, TAG_LENGTH + sizeof (unsigned int), SEEK_CUR) != 0) + if (fseek(snssFile->fp, TAG_LENGTH + sizeof(unsigned int), SEEK_CUR) != 0) { return SNSS_READ_FAILED; } /* get the block data length */ - if (fread (&blockLength, sizeof (unsigned int), 1, snssFile->fp) != 1) + if (fread(&blockLength, sizeof(unsigned int), 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } - blockLength = swap32 (blockLength); + blockLength = swap32(blockLength); /* skip over the block data */ - if (fseek (snssFile->fp, blockLength, SEEK_CUR) != 0) + if (fseek(snssFile->fp, blockLength, SEEK_CUR) != 0) { return SNSS_READ_FAILED; } @@ -365,18 +366,18 @@ SNSS_SkipNextBlock (SNSS_FILE *snssFile) /* functions for reading and writing base register blocks */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadBaseBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadBaseBlock(SNSS_FILE *snssFile) { - char blockBytes[BASE_BLOCK_LENGTH]; + char *blockBytes = NOFRENDO_MALLOC(BASE_BLOCK_LENGTH); SnssBlockHeader header; - if (SNSS_ReadBlockHeader (&header, snssFile) != SNSS_OK) + if (SNSS_ReadBlockHeader(&header, snssFile) != SNSS_OK) { return SNSS_READ_FAILED; } - if (fread (blockBytes, MIN (header.blockLength, BASE_BLOCK_LENGTH), 1, snssFile->fp) != 1) + if (fread(blockBytes, MIN(header.blockLength, BASE_BLOCK_LENGTH), 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } @@ -386,17 +387,17 @@ SNSS_ReadBaseBlock (SNSS_FILE *snssFile) snssFile->baseBlock.regY = blockBytes[0x2]; snssFile->baseBlock.regFlags = blockBytes[0x3]; snssFile->baseBlock.regStack = blockBytes[0x4]; - snssFile->baseBlock.regPc = *((unsigned short *) &blockBytes[0x5]); - snssFile->baseBlock.regPc = swap16 (snssFile->baseBlock.regPc); + snssFile->baseBlock.regPc = *((unsigned short *)&blockBytes[0x5]); + snssFile->baseBlock.regPc = swap16(snssFile->baseBlock.regPc); snssFile->baseBlock.reg2000 = blockBytes[0x7]; snssFile->baseBlock.reg2001 = blockBytes[0x8]; - memcpy (&snssFile->baseBlock.cpuRam, &blockBytes[0x9], 0x800); - memcpy (&snssFile->baseBlock.spriteRam, &blockBytes[0x809], 0x100); - memcpy (&snssFile->baseBlock.ppuRam, &blockBytes[0x909], 0x1000); - memcpy (&snssFile->baseBlock.palette, &blockBytes[0x1909], 0x20); - memcpy (&snssFile->baseBlock.mirrorState, &blockBytes[0x1929], 0x4); - snssFile->baseBlock.vramAddress = *((unsigned short *) &blockBytes[0x192D]); - snssFile->baseBlock.vramAddress = swap16 (snssFile->baseBlock.vramAddress); + memcpy(&snssFile->baseBlock.cpuRam, &blockBytes[0x9], 0x800); + memcpy(&snssFile->baseBlock.spriteRam, &blockBytes[0x809], 0x100); + memcpy(&snssFile->baseBlock.ppuRam, &blockBytes[0x909], 0x1000); + memcpy(&snssFile->baseBlock.palette, &blockBytes[0x1909], 0x20); + memcpy(&snssFile->baseBlock.mirrorState, &blockBytes[0x1929], 0x4); + snssFile->baseBlock.vramAddress = *((unsigned short *)&blockBytes[0x192D]); + snssFile->baseBlock.vramAddress = swap16(snssFile->baseBlock.vramAddress); snssFile->baseBlock.spriteRamAddress = blockBytes[0x192F]; snssFile->baseBlock.tileXOffset = blockBytes[0x1930]; @@ -405,19 +406,19 @@ SNSS_ReadBaseBlock (SNSS_FILE *snssFile) /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteBaseBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteBaseBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; SNSS_RETURN_CODE returnCode; - char blockBytes[BASE_BLOCK_LENGTH]; + char *blockBytes = NOFRENDO_MALLOC(BASE_BLOCK_LENGTH); unsigned short tempShort; - strcpy (header.tag, "BASR"); + strcpy(header.tag, "BASR"); header.blockVersion = SNSS_BLOCK_VERSION; header.blockLength = BASE_BLOCK_LENGTH; - if ((returnCode = SNSS_WriteBlockHeader (&header, snssFile)) != SNSS_OK) + if ((returnCode = SNSS_WriteBlockHeader(&header, snssFile)) != SNSS_OK) { return returnCode; } @@ -427,23 +428,23 @@ SNSS_WriteBaseBlock (SNSS_FILE *snssFile) blockBytes[0x2] = snssFile->baseBlock.regY; blockBytes[0x3] = snssFile->baseBlock.regFlags; blockBytes[0x4] = snssFile->baseBlock.regStack; - tempShort = swap16 (snssFile->baseBlock.regPc); - blockBytes[0x5] = ((char *) &tempShort)[0]; - blockBytes[0x6] = ((char *) &tempShort)[1]; + tempShort = swap16(snssFile->baseBlock.regPc); + blockBytes[0x5] = ((char *)&tempShort)[0]; + blockBytes[0x6] = ((char *)&tempShort)[1]; blockBytes[0x7] = snssFile->baseBlock.reg2000; blockBytes[0x8] = snssFile->baseBlock.reg2001; - memcpy (&blockBytes[0x9], &snssFile->baseBlock.cpuRam, 0x800); - memcpy (&blockBytes[0x809], &snssFile->baseBlock.spriteRam, 0x100); - memcpy (&blockBytes[0x909], &snssFile->baseBlock.ppuRam, 0x1000); - memcpy (&blockBytes[0x1909], &snssFile->baseBlock.palette, 0x20); - memcpy (&blockBytes[0x1929], &snssFile->baseBlock.mirrorState, 0x4); - tempShort = swap16 (snssFile->baseBlock.vramAddress); - blockBytes[0x192D] = ((char *) &tempShort)[0]; - blockBytes[0x192E] = ((char *) &tempShort)[1]; + memcpy(&blockBytes[0x9], &snssFile->baseBlock.cpuRam, 0x800); + memcpy(&blockBytes[0x809], &snssFile->baseBlock.spriteRam, 0x100); + memcpy(&blockBytes[0x909], &snssFile->baseBlock.ppuRam, 0x1000); + memcpy(&blockBytes[0x1909], &snssFile->baseBlock.palette, 0x20); + memcpy(&blockBytes[0x1929], &snssFile->baseBlock.mirrorState, 0x4); + tempShort = swap16(snssFile->baseBlock.vramAddress); + blockBytes[0x192D] = ((char *)&tempShort)[0]; + blockBytes[0x192E] = ((char *)&tempShort)[1]; blockBytes[0x192F] = snssFile->baseBlock.spriteRamAddress; blockBytes[0x1930] = snssFile->baseBlock.tileXOffset; - if (fwrite (blockBytes, BASE_BLOCK_LENGTH, 1, snssFile->fp) != 1) + if (fwrite(blockBytes, BASE_BLOCK_LENGTH, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -457,17 +458,17 @@ SNSS_WriteBaseBlock (SNSS_FILE *snssFile) /* functions for reading and writing VRAM blocks */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadVramBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadVramBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; - if (SNSS_ReadBlockHeader (&header, snssFile) != SNSS_OK) + if (SNSS_ReadBlockHeader(&header, snssFile) != SNSS_OK) { return SNSS_READ_FAILED; } - if (fread (snssFile->vramBlock.vram, MIN (header.blockLength, VRAM_16K), 1, snssFile->fp) != 1) + if (fread(snssFile->vramBlock.vram, MIN(header.blockLength, VRAM_16K), 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } @@ -479,22 +480,22 @@ SNSS_ReadVramBlock (SNSS_FILE *snssFile) /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteVramBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteVramBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; SNSS_RETURN_CODE returnCode; - strcpy (header.tag, "VRAM"); + strcpy(header.tag, "VRAM"); header.blockVersion = SNSS_BLOCK_VERSION; header.blockLength = snssFile->vramBlock.vramSize; - if ((returnCode = SNSS_WriteBlockHeader (&header, snssFile)) != SNSS_OK) + if ((returnCode = SNSS_WriteBlockHeader(&header, snssFile)) != SNSS_OK) { return returnCode; } - if (fwrite (snssFile->vramBlock.vram, snssFile->vramBlock.vramSize, 1, snssFile->fp) != 1) + if (fwrite(snssFile->vramBlock.vram, snssFile->vramBlock.vramSize, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -508,23 +509,23 @@ SNSS_WriteVramBlock (SNSS_FILE *snssFile) /* functions for reading and writing SRAM blocks */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadSramBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadSramBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; - if (SNSS_ReadBlockHeader (&header, snssFile) != SNSS_OK) + if (SNSS_ReadBlockHeader(&header, snssFile) != SNSS_OK) { return SNSS_READ_FAILED; } - if (fread (&snssFile->sramBlock.sramEnabled, 1, 1, snssFile->fp) != 1) + if (fread(&snssFile->sramBlock.sramEnabled, 1, 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } /* read blockLength - 1 bytes to get all of the SRAM */ - if (fread (&snssFile->sramBlock.sram, MIN (header.blockLength - 1, SRAM_8K), 1, snssFile->fp) != 1) + if (fread(&snssFile->sramBlock.sram, MIN(header.blockLength - 1, SRAM_8K), 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } @@ -537,27 +538,27 @@ SNSS_ReadSramBlock (SNSS_FILE *snssFile) /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteSramBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteSramBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; SNSS_RETURN_CODE returnCode; - strcpy (header.tag, "SRAM"); + strcpy(header.tag, "SRAM"); header.blockVersion = SNSS_BLOCK_VERSION; /* length of block is size of SRAM plus SRAM enabled byte */ header.blockLength = snssFile->sramBlock.sramSize + 1; - if ((returnCode = SNSS_WriteBlockHeader (&header, snssFile)) != SNSS_OK) + if ((returnCode = SNSS_WriteBlockHeader(&header, snssFile)) != SNSS_OK) { return returnCode; } - if (fwrite (&snssFile->sramBlock.sramEnabled, 1, 1, snssFile->fp) != 1) + if (fwrite(&snssFile->sramBlock.sramEnabled, 1, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } - if (fwrite (snssFile->sramBlock.sram, snssFile->sramBlock.sramSize, 1, snssFile->fp) != 1) + if (fwrite(snssFile->sramBlock.sram, snssFile->sramBlock.sramSize, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -571,52 +572,52 @@ SNSS_WriteSramBlock (SNSS_FILE *snssFile) /* functions for reading and writing mapper data blocks */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadMapperBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadMapperBlock(SNSS_FILE *snssFile) { char *blockBytes; int i; SnssBlockHeader header; - if (SNSS_ReadBlockHeader (&header, snssFile) != SNSS_OK) + if (SNSS_ReadBlockHeader(&header, snssFile) != SNSS_OK) { return SNSS_READ_FAILED; } - if ((blockBytes = (char *) malloc (0x8 + 0x10 + 0x80)) == NULL) + if ((blockBytes = (char *)NOFRENDO_MALLOC(0x8 + 0x10 + 0x80)) == NULL) { return SNSS_OUT_OF_MEMORY; } - if (fread (blockBytes, MIN (0x8 + 0x10 + 0x80, header.blockLength), 1, snssFile->fp) != 1) + if (fread(blockBytes, MIN(0x8 + 0x10 + 0x80, header.blockLength), 1, snssFile->fp) != 1) { - free(blockBytes); + NOFRENDO_FREE(blockBytes); return SNSS_READ_FAILED; } for (i = 0; i < 4; i++) { - snssFile->mapperBlock.prgPages[i] = *((unsigned short *) &blockBytes[i * 2]); - snssFile->mapperBlock.prgPages[i] = swap16 (snssFile->mapperBlock.prgPages[i]); + snssFile->mapperBlock.prgPages[i] = *((unsigned short *)&blockBytes[i * 2]); + snssFile->mapperBlock.prgPages[i] = swap16(snssFile->mapperBlock.prgPages[i]); } for (i = 0; i < 8; i++) { - snssFile->mapperBlock.chrPages[i] = *((unsigned short *) &blockBytes[0x8 + (i * 2)]); - snssFile->mapperBlock.chrPages[i] = swap16 (snssFile->mapperBlock.chrPages[i]); + snssFile->mapperBlock.chrPages[i] = *((unsigned short *)&blockBytes[0x8 + (i * 2)]); + snssFile->mapperBlock.chrPages[i] = swap16(snssFile->mapperBlock.chrPages[i]); } - memcpy (&snssFile->mapperBlock.extraData.mapperData, &blockBytes[0x18], 0x80); + memcpy(&snssFile->mapperBlock.extraData.mapperData, &blockBytes[0x18], 0x80); - free (blockBytes); + NOFRENDO_FREE(blockBytes); return SNSS_OK; } /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteMapperBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteMapperBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; char blockBytes[MAPPER_BLOCK_LENGTH]; @@ -624,32 +625,32 @@ SNSS_WriteMapperBlock (SNSS_FILE *snssFile) int i; SNSS_RETURN_CODE returnCode; - strcpy (header.tag, "MPRD"); + strcpy(header.tag, "MPRD"); header.blockVersion = SNSS_BLOCK_VERSION; header.blockLength = MAPPER_BLOCK_LENGTH; - if ((returnCode = SNSS_WriteBlockHeader (&header, snssFile)) != SNSS_OK) + if ((returnCode = SNSS_WriteBlockHeader(&header, snssFile)) != SNSS_OK) { return returnCode; } for (i = 0; i < 4; i++) { - tempShort = swap16 (snssFile->mapperBlock.prgPages[i]); - blockBytes[(i * 2) + 0] = ((char *) &tempShort)[0]; - blockBytes[(i * 2) + 1] = ((char *) &tempShort)[1]; + tempShort = swap16(snssFile->mapperBlock.prgPages[i]); + blockBytes[(i * 2) + 0] = ((char *)&tempShort)[0]; + blockBytes[(i * 2) + 1] = ((char *)&tempShort)[1]; } for (i = 0; i < 8; i++) { - tempShort = swap16 (snssFile->mapperBlock.chrPages[i]); - blockBytes[0x8 + (i * 2) + 0] = ((char *) &tempShort)[0]; - blockBytes[0x8 + (i * 2) + 1] = ((char *) &tempShort)[1]; + tempShort = swap16(snssFile->mapperBlock.chrPages[i]); + blockBytes[0x8 + (i * 2) + 0] = ((char *)&tempShort)[0]; + blockBytes[0x8 + (i * 2) + 1] = ((char *)&tempShort)[1]; } - memcpy (&blockBytes[0x18], &snssFile->mapperBlock.extraData.mapperData, 0x80); + memcpy(&blockBytes[0x18], &snssFile->mapperBlock.extraData.mapperData, 0x80); - if (fwrite (blockBytes, MAPPER_BLOCK_LENGTH, 1, snssFile->fp) != 1) + if (fwrite(blockBytes, MAPPER_BLOCK_LENGTH, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -663,8 +664,8 @@ SNSS_WriteMapperBlock (SNSS_FILE *snssFile) /* functions for reading and writing controller data blocks */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadControllersBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadControllersBlock(SNSS_FILE *snssFile) { /* quell warnings */ snssFile = snssFile; @@ -674,8 +675,8 @@ SNSS_ReadControllersBlock (SNSS_FILE *snssFile) /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteControllersBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteControllersBlock(SNSS_FILE *snssFile) { /* quell warnings */ snssFile = snssFile; @@ -687,17 +688,17 @@ SNSS_WriteControllersBlock (SNSS_FILE *snssFile) /* functions for reading and writing sound blocks */ /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_ReadSoundBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_ReadSoundBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; - if (SNSS_ReadBlockHeader (&header, snssFile) != SNSS_OK) + if (SNSS_ReadBlockHeader(&header, snssFile) != SNSS_OK) { return SNSS_READ_FAILED; } - if (fread (snssFile->soundBlock.soundRegisters, MIN (header.blockLength, 0x16), 1, snssFile->fp) != 1) + if (fread(snssFile->soundBlock.soundRegisters, MIN(header.blockLength, 0x16), 1, snssFile->fp) != 1) { return SNSS_READ_FAILED; } @@ -707,22 +708,22 @@ SNSS_ReadSoundBlock (SNSS_FILE *snssFile) /**************************************************************************/ -static SNSS_RETURN_CODE -SNSS_WriteSoundBlock (SNSS_FILE *snssFile) +static SNSS_RETURN_CODE +SNSS_WriteSoundBlock(SNSS_FILE *snssFile) { SnssBlockHeader header; SNSS_RETURN_CODE returnCode; - strcpy (header.tag, "SOUN"); + strcpy(header.tag, "SOUN"); header.blockVersion = SNSS_BLOCK_VERSION; header.blockLength = SOUND_BLOCK_LENGTH; - if ((returnCode = SNSS_WriteBlockHeader (&header, snssFile)) != SNSS_OK) + if ((returnCode = SNSS_WriteBlockHeader(&header, snssFile)) != SNSS_OK) { return returnCode; } - if (fwrite (snssFile->soundBlock.soundRegisters, SOUND_BLOCK_LENGTH, 1, snssFile->fp) != 1) + if (fwrite(snssFile->soundBlock.soundRegisters, SOUND_BLOCK_LENGTH, 1, snssFile->fp) != 1) { return SNSS_WRITE_FAILED; } @@ -737,62 +738,62 @@ SNSS_WriteSoundBlock (SNSS_FILE *snssFile) /**************************************************************************/ SNSS_RETURN_CODE -SNSS_ReadBlock (SNSS_FILE *snssFile, SNSS_BLOCK_TYPE blockType) +SNSS_ReadBlock(SNSS_FILE *snssFile, SNSS_BLOCK_TYPE blockType) { switch (blockType) { case SNSS_BASR: - return SNSS_ReadBaseBlock (snssFile); + return SNSS_ReadBaseBlock(snssFile); case SNSS_VRAM: - return SNSS_ReadVramBlock (snssFile); + return SNSS_ReadVramBlock(snssFile); case SNSS_SRAM: - return SNSS_ReadSramBlock (snssFile); + return SNSS_ReadSramBlock(snssFile); case SNSS_MPRD: - return SNSS_ReadMapperBlock (snssFile); + return SNSS_ReadMapperBlock(snssFile); case SNSS_CNTR: - return SNSS_ReadControllersBlock (snssFile); + return SNSS_ReadControllersBlock(snssFile); case SNSS_SOUN: - return SNSS_ReadSoundBlock (snssFile); + return SNSS_ReadSoundBlock(snssFile); case SNSS_UNKNOWN_BLOCK: default: - return SNSS_UNSUPPORTED_BLOCK; + return SNSS_UNSUPPORTED_BLOCK; } } /**************************************************************************/ SNSS_RETURN_CODE -SNSS_WriteBlock (SNSS_FILE *snssFile, SNSS_BLOCK_TYPE blockType) +SNSS_WriteBlock(SNSS_FILE *snssFile, SNSS_BLOCK_TYPE blockType) { switch (blockType) { case SNSS_BASR: - return SNSS_WriteBaseBlock (snssFile); + return SNSS_WriteBaseBlock(snssFile); case SNSS_VRAM: - return SNSS_WriteVramBlock (snssFile); + return SNSS_WriteVramBlock(snssFile); case SNSS_SRAM: - return SNSS_WriteSramBlock (snssFile); + return SNSS_WriteSramBlock(snssFile); case SNSS_MPRD: - return SNSS_WriteMapperBlock (snssFile); + return SNSS_WriteMapperBlock(snssFile); case SNSS_CNTR: - return SNSS_WriteControllersBlock (snssFile); + return SNSS_WriteControllersBlock(snssFile); case SNSS_SOUN: - return SNSS_WriteSoundBlock (snssFile); + return SNSS_WriteSoundBlock(snssFile); case SNSS_UNKNOWN_BLOCK: default: - return SNSS_UNSUPPORTED_BLOCK; + return SNSS_UNSUPPORTED_BLOCK; } } diff --git a/components/nofrendo/libsnss/libsnss.h b/components/nofrendo/libsnss/libsnss.h index 576227c..39c3d59 100644 --- a/components/nofrendo/libsnss/libsnss.h +++ b/components/nofrendo/libsnss/libsnss.h @@ -14,6 +14,8 @@ #include +#include "../memguard.h" + /**************************************************************************/ /* endian customization */ /**************************************************************************/ diff --git a/components/nofrendo/log.c b/components/nofrendo/log.c index d69b304..97638c6 100644 --- a/components/nofrendo/log.c +++ b/components/nofrendo/log.c @@ -26,9 +26,9 @@ #include #include #include -#include -#include +#include "noftypes.h" +#include "log.h" //static FILE *errorlog = NULL; static int (*log_func)(const char *string) = NULL; @@ -62,13 +62,12 @@ int log_print(const char *string) log_func(string); /* Log it to disk, as well */ -// fputs(string, errorlog); -// printf("%s\n", string); + // fputs(string, errorlog); return 0; } -int log_printf(const char *format, ... ) +int nofrendo_log_printf(const char *format, ... ) { /* don't allocate on stack every call */ static char buffer[1024 + 1]; @@ -82,7 +81,7 @@ int log_printf(const char *format, ... ) log_func(buffer); } -// vfprintf(errorlog, format, arg); + // vfprintf(errorlog, format, arg); va_end(arg); return 0; /* should be number of chars written */ @@ -106,7 +105,7 @@ int log_print(const char *string) return 0; } -int log_printf(const char *format, ... ) +int nofrendo_log_printf(const char *format, ... ) { UNUSED(format); @@ -125,12 +124,12 @@ void log_assert(int expr, int line, const char *file, char *msg) return; if (NULL != msg) - log_printf("ASSERT: line %d of %s, %s\n", line, file, msg); + nofrendo_log_printf("ASSERT: line %d of %s, %s\n", line, file, msg); else - log_printf("ASSERT: line %d of %s\n", line, file); + nofrendo_log_printf("ASSERT: line %d of %s\n", line, file); asm("break.n 1"); -// exit(-1); + // exit(-1); } diff --git a/components/nofrendo/log.h b/components/nofrendo/log.h index 58d93c7..72047ea 100644 --- a/components/nofrendo/log.h +++ b/components/nofrendo/log.h @@ -31,7 +31,7 @@ extern int log_init(void); extern void log_shutdown(void); extern int log_print(const char *string); -extern int log_printf(const char *format, ...); +extern int nofrendo_log_printf(const char *format, ...); extern void log_chain_logfunc(int (*logfunc)(const char *string)); extern void log_assert(int expr, int line, const char *file, char *msg); diff --git a/components/nofrendo/mappers/map000.c b/components/nofrendo/mappers/map000.c index 5e9660c..93cb177 100644 --- a/components/nofrendo/mappers/map000.c +++ b/components/nofrendo/mappers/map000.c @@ -23,8 +23,8 @@ ** $Id: map000.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" mapintf_t map0_intf = { diff --git a/components/nofrendo/mappers/map001.c b/components/nofrendo/mappers/map001.c index 6b62336..5f6c949 100644 --- a/components/nofrendo/mappers/map001.c +++ b/components/nofrendo/mappers/map001.c @@ -24,9 +24,9 @@ */ #include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* TODO: WRAM enable ala Mark Knibbs: ================================== diff --git a/components/nofrendo/mappers/map002.c b/components/nofrendo/mappers/map002.c index 1281d50..dade025 100644 --- a/components/nofrendo/mappers/map002.c +++ b/components/nofrendo/mappers/map002.c @@ -23,10 +23,18 @@ ** $Id: map002.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 2: UNROM */ +static void map2_init() +{ + int last_bank = mmc_getinfo()->rom_banks - 1; + nofrendo_log_printf("map2_init. last_bank=%d\n", last_bank); + mmc_bankrom(16, 0xc000, last_bank); + mmc_bankrom(16, 0x8000, 0); +} + static void map2_write(uint32 address, uint8 value) { UNUSED(address); @@ -44,7 +52,7 @@ mapintf_t map2_intf = { 2, /* mapper number */ "UNROM", /* mapper name */ - NULL, /* init routine */ + map2_init, /* init routine */ NULL, /* vblank callback */ NULL, /* hblank callback */ NULL, /* get state (snss) */ diff --git a/components/nofrendo/mappers/map003.c b/components/nofrendo/mappers/map003.c index f24f0d8..8799e57 100644 --- a/components/nofrendo/mappers/map003.c +++ b/components/nofrendo/mappers/map003.c @@ -23,8 +23,8 @@ ** $Id: map003.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 3: CNROM */ static void map3_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map004.c b/components/nofrendo/mappers/map004.c index 36c6ca4..dac567d 100644 --- a/components/nofrendo/mappers/map004.c +++ b/components/nofrendo/mappers/map004.c @@ -23,10 +23,10 @@ ** $Id: map004.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" static struct { @@ -138,6 +138,11 @@ static void map4_write(uint32 address, uint8 value) break; default: + //printf("map004: unhandled write: address=%p, value=0x%x\n", (void*)address, value); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); + __asm__("nop"); break; } diff --git a/components/nofrendo/mappers/map005.c b/components/nofrendo/mappers/map005.c index 82d765d..1f2ffab 100644 --- a/components/nofrendo/mappers/map005.c +++ b/components/nofrendo/mappers/map005.c @@ -23,11 +23,11 @@ ** $Id: map005.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include -#include "mmc5_snd.h" +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../log.h" +#include "../sndhrdw/mmc5_snd.h" /* TODO: there's lots of info about this mapper now; ** let's implement it correctly/completely @@ -201,7 +201,7 @@ static void map5_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("unknown mmc5 write: $%02X to $%04X\n", value, address); + nofrendo_log_printf("unknown mmc5 write: $%02X to $%04X\n", value, address); #endif /* NOFRENDO_DEBUG */ break; } @@ -218,8 +218,8 @@ static uint8 map5_read(uint32 address) else { #ifdef NOFRENDO_DEBUG - log_printf("invalid MMC5 read: $%04X", address); -#endif + nofrendo_log_printf("invalid MMC5 read: $%04X", address); +#endif /* !NOFRENDO_DEBUG */ return 0xFF; } } diff --git a/components/nofrendo/mappers/map007.c b/components/nofrendo/mappers/map007.c index b4db7df..1ac2352 100644 --- a/components/nofrendo/mappers/map007.c +++ b/components/nofrendo/mappers/map007.c @@ -23,10 +23,10 @@ ** $Id: map007.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" +#include "../log.h" /* mapper 7: AOROM */ static void map7_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map008.c b/components/nofrendo/mappers/map008.c index 30dffc0..962a5c4 100644 --- a/components/nofrendo/mappers/map008.c +++ b/components/nofrendo/mappers/map008.c @@ -23,8 +23,8 @@ ** $Id: map008.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 8: FFE F3xxx -- what the hell uses this? */ static void map8_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map009.c b/components/nofrendo/mappers/map009.c index eab131e..d89400b 100644 --- a/components/nofrendo/mappers/map009.c +++ b/components/nofrendo/mappers/map009.c @@ -24,10 +24,10 @@ */ #include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" +#include "../libsnss/libsnss.h" static uint8 latch[2]; static uint8 regs[4]; diff --git a/components/nofrendo/mappers/map010.c b/components/nofrendo/mappers/map010.c new file mode 100644 index 0000000..6fe958c --- /dev/null +++ b/components/nofrendo/mappers/map010.c @@ -0,0 +1,161 @@ +/* +** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com) +** +** +** This program is free software; you can redistribute it and/or +** modify it under the terms of version 2 of the GNU Library General +** Public License as published by the Free Software Foundation. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +** Library General Public License for more details. To obtain a +** copy of the GNU Library General Public License, write to the Free +** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +** +** Any permitted reproduction of these routines, in whole or in part, +** must bear this legend. +** +** +** map010.c +** +** mapper 10 interface +** $Id: map010.c,v 1.0 2018/07/07 +*/ + +#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" +#include "../libsnss/libsnss.h" + +static uint8 latch[2]; +static uint8 regs[4]; + +/* Used when tile $FD/$FE is accessed */ +static void mmc10_latchfunc(uint32 address, uint8 value) +{ + if (0xFD == value || 0xFE == value) + { + int reg; + + if (address) + { + latch[1] = value; + reg = 2 + (value - 0xFD); + } + else + { + latch[0] = value; + reg = value - 0xFD; + } + + mmc_bankvrom(4, address, regs[reg]); + } +} + +/* mapper 10: MMC4 */ +/* MMC4: Fire Emblem */ +static void map10_write(uint32 address, uint8 value) +{ + switch ((address & 0xF000) >> 12) + { + case 0xA: + mmc_bankrom(16, 0x8000, value); + break; + + case 0xB: + regs[0] = value; + if (0xFD == latch[0]) + mmc_bankvrom(4, 0x0000, value); + break; + + case 0xC: + regs[1] = value; + if (0xFE == latch[0]) + mmc_bankvrom(4, 0x0000, value); + break; + + case 0xD: + regs[2] = value; + if (0xFD == latch[1]) + mmc_bankvrom(4, 0x1000, value); + break; + + case 0xE: + regs[3] = value; + if (0xFE == latch[1]) + mmc_bankvrom(4, 0x1000, value); + break; + + case 0xF: + if (value & 1) + ppu_mirror(0, 0, 1, 1); /* horizontal */ + else + ppu_mirror(0, 1, 0, 1); /* vertical */ + break; + + default: + break; + } +} + +static void map10_init(void) +{ + memset(regs, 0, sizeof(regs)); + + mmc_bankrom(16, 0x8000, 0); + mmc_bankrom(16, 0xC000, (mmc_getinfo()->rom_banks) - 1); + + latch[0] = 0xFE; + latch[1] = 0xFE; + + ppu_setlatchfunc(mmc10_latchfunc); +} + +static void map10_getstate(SnssMapperBlock *state) +{ + state->extraData.mapper10.latch[0] = latch[0]; + state->extraData.mapper10.latch[1] = latch[1]; + state->extraData.mapper10.lastB000Write = regs[0]; + state->extraData.mapper10.lastC000Write = regs[1]; + state->extraData.mapper10.lastD000Write = regs[2]; + state->extraData.mapper10.lastE000Write = regs[3]; +} + +static void map10_setstate(SnssMapperBlock *state) +{ + latch[0] = state->extraData.mapper10.latch[0]; + latch[1] = state->extraData.mapper10.latch[1]; + regs[0] = state->extraData.mapper10.lastB000Write; + regs[1] = state->extraData.mapper10.lastC000Write; + regs[2] = state->extraData.mapper10.lastD000Write; + regs[3] = state->extraData.mapper10.lastE000Write; +} + +static map_memwrite map10_memwrite[] = +{ + { 0x8000, 0xFFFF, map10_write }, + { -1, -1, NULL } +}; + +mapintf_t map10_intf = +{ + 10, /* mapper number */ + "MMC4", /* mapper name */ + map10_init, /* init routine */ + NULL, /* vblank callback */ + NULL, /* hblank callback */ + map10_getstate, /* get state (snss) */ + map10_setstate, /* set state (snss) */ + NULL, /* memory read structure */ + map10_memwrite, /* memory write structure */ + NULL /* external sound device */ +}; + +/* +** $Log: map010.c,v $ +** Revision 1.0 2018/07/07 +** initial revision based on map009.c +** +*/ diff --git a/components/nofrendo/mappers/map011.c b/components/nofrendo/mappers/map011.c index fc4a2fc..7d77b31 100644 --- a/components/nofrendo/mappers/map011.c +++ b/components/nofrendo/mappers/map011.c @@ -23,8 +23,8 @@ ** $Id: map011.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 11: Color Dreams, Wisdom Tree */ static void map11_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map015.c b/components/nofrendo/mappers/map015.c index fef6f9c..1228140 100644 --- a/components/nofrendo/mappers/map015.c +++ b/components/nofrendo/mappers/map015.c @@ -23,9 +23,9 @@ ** $Id: map015.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* mapper 15: Contra 100-in-1 */ static void map15_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map016.c b/components/nofrendo/mappers/map016.c index 487c4b6..0439654 100644 --- a/components/nofrendo/mappers/map016.c +++ b/components/nofrendo/mappers/map016.c @@ -23,10 +23,10 @@ ** $Id: map016.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" +#include "../nes/nes.h" static struct { diff --git a/components/nofrendo/mappers/map018.c b/components/nofrendo/mappers/map018.c index 6c878b4..defdcd5 100644 --- a/components/nofrendo/mappers/map018.c +++ b/components/nofrendo/mappers/map018.c @@ -23,9 +23,9 @@ ** $Id: map018.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* mapper 18: Jaleco SS8806 */ #define VRC_PBANK(bank, value, high) \ diff --git a/components/nofrendo/mappers/map019.c b/components/nofrendo/mappers/map019.c index 14a93b4..a031675 100644 --- a/components/nofrendo/mappers/map019.c +++ b/components/nofrendo/mappers/map019.c @@ -23,9 +23,9 @@ ** $Id: map019.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* TODO: shouldn't there be an h-blank IRQ handler??? */ diff --git a/components/nofrendo/mappers/map024.c b/components/nofrendo/mappers/map024.c index d6aea3c..388ba87 100644 --- a/components/nofrendo/mappers/map024.c +++ b/components/nofrendo/mappers/map024.c @@ -23,11 +23,11 @@ ** $Id: map024.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../log.h" +#include "../sndhrdw/vrcvisnd.h" static struct { @@ -147,8 +147,8 @@ static void map24_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("invalid VRC6 write: $%02X to $%04X", value, address); -#endif + nofrendo_log_printf("invalid VRC6 write: $%02X to $%04X", value, address); +#endif /* !NOFRENDO_DEBUG */ break; } } diff --git a/components/nofrendo/mappers/map032.c b/components/nofrendo/mappers/map032.c index cc22acf..11ff5ce 100644 --- a/components/nofrendo/mappers/map032.c +++ b/components/nofrendo/mappers/map032.c @@ -23,9 +23,9 @@ ** $Id: map032.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" static int select_c000 = 0; diff --git a/components/nofrendo/mappers/map033.c b/components/nofrendo/mappers/map033.c index ba2a575..e089ee9 100644 --- a/components/nofrendo/mappers/map033.c +++ b/components/nofrendo/mappers/map033.c @@ -23,9 +23,9 @@ ** $Id: map033.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* mapper 33: Taito TC0190*/ static void map33_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map034.c b/components/nofrendo/mappers/map034.c index 6333e17..8116b57 100644 --- a/components/nofrendo/mappers/map034.c +++ b/components/nofrendo/mappers/map034.c @@ -23,8 +23,8 @@ ** $Id: map034.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" static void map34_init(void) { diff --git a/components/nofrendo/mappers/map040.c b/components/nofrendo/mappers/map040.c index a6fc4d6..bd002d5 100644 --- a/components/nofrendo/mappers/map040.c +++ b/components/nofrendo/mappers/map040.c @@ -23,11 +23,11 @@ ** $Id: map040.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" #define MAP40_IRQ_PERIOD (4096 / 113.666666) diff --git a/components/nofrendo/mappers/map041.c b/components/nofrendo/mappers/map041.c index 25d44b9..2fdda79 100644 --- a/components/nofrendo/mappers/map041.c +++ b/components/nofrendo/mappers/map041.c @@ -26,11 +26,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" static uint8 register_low; static uint8 register_high; diff --git a/components/nofrendo/mappers/map042.c b/components/nofrendo/mappers/map042.c index a675771..b99fb74 100644 --- a/components/nofrendo/mappers/map042.c +++ b/components/nofrendo/mappers/map042.c @@ -26,11 +26,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" static struct { diff --git a/components/nofrendo/mappers/map046.c b/components/nofrendo/mappers/map046.c index 9c11dff..b07be58 100644 --- a/components/nofrendo/mappers/map046.c +++ b/components/nofrendo/mappers/map046.c @@ -26,11 +26,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" static uint8 prg_low_bank; static uint8 chr_low_bank; diff --git a/components/nofrendo/mappers/map050.c b/components/nofrendo/mappers/map050.c index 6cb7e03..0371aab 100644 --- a/components/nofrendo/mappers/map050.c +++ b/components/nofrendo/mappers/map050.c @@ -26,11 +26,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" static struct { diff --git a/components/nofrendo/mappers/map064.c b/components/nofrendo/mappers/map064.c index 9188815..d7bda69 100644 --- a/components/nofrendo/mappers/map064.c +++ b/components/nofrendo/mappers/map064.c @@ -23,10 +23,10 @@ ** $Id: map064.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../log.h" static struct { @@ -119,8 +119,8 @@ static void map64_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("mapper 64: unknown command #%d", command & 0xF); -#endif + nofrendo_log_printf("mapper 64: unknown command #%d", command & 0xF); +#endif /* !NOFRENDO_DEBUG */ break; } break; @@ -153,8 +153,8 @@ static void map64_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("mapper 64: Wrote $%02X to $%04X", value, address); -#endif + nofrendo_log_printf("mapper 64: Wrote $%02X to $%04X", value, address); +#endif /* !NOFRENDO_DEBUG */ break; } diff --git a/components/nofrendo/mappers/map065.c b/components/nofrendo/mappers/map065.c index d9925c0..ef28f3f 100644 --- a/components/nofrendo/mappers/map065.c +++ b/components/nofrendo/mappers/map065.c @@ -23,9 +23,9 @@ ** $Id: map065.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" static struct { diff --git a/components/nofrendo/mappers/map066.c b/components/nofrendo/mappers/map066.c index f8f8ba7..54edd9c 100644 --- a/components/nofrendo/mappers/map066.c +++ b/components/nofrendo/mappers/map066.c @@ -23,8 +23,8 @@ ** $Id: map066.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 66: GNROM */ static void map66_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map070.c b/components/nofrendo/mappers/map070.c index 51c612a..0e9580c 100644 --- a/components/nofrendo/mappers/map070.c +++ b/components/nofrendo/mappers/map070.c @@ -23,9 +23,9 @@ ** $Id: map070.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* mapper 70: Arkanoid II, Kamen Rider Club, etc. */ /* ($8000-$FFFF) D6-D4 = switch $8000-$BFFF */ diff --git a/components/nofrendo/mappers/map073.c b/components/nofrendo/mappers/map073.c index 9fd53d9..5e2ebf0 100644 --- a/components/nofrendo/mappers/map073.c +++ b/components/nofrendo/mappers/map073.c @@ -25,11 +25,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" static struct { diff --git a/components/nofrendo/mappers/map075.c b/components/nofrendo/mappers/map075.c index 2e31023..21ae6ec 100644 --- a/components/nofrendo/mappers/map075.c +++ b/components/nofrendo/mappers/map075.c @@ -23,9 +23,9 @@ ** $Id: map075.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" static uint8 latch[2]; diff --git a/components/nofrendo/mappers/map078.c b/components/nofrendo/mappers/map078.c index 172d1ed..c8cf3d2 100644 --- a/components/nofrendo/mappers/map078.c +++ b/components/nofrendo/mappers/map078.c @@ -23,9 +23,9 @@ ** $Id: map078.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* mapper 78: Holy Diver, Cosmo Carrier */ /* ($8000-$FFFF) D2-D0 = switch $8000-$BFFF */ diff --git a/components/nofrendo/mappers/map079.c b/components/nofrendo/mappers/map079.c index e9a8d6a..9e972f9 100644 --- a/components/nofrendo/mappers/map079.c +++ b/components/nofrendo/mappers/map079.c @@ -23,8 +23,8 @@ ** $Id: map079.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 79: NINA-03/06 */ static void map79_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map085.c b/components/nofrendo/mappers/map085.c index 68a86e5..d9d8e7f 100644 --- a/components/nofrendo/mappers/map085.c +++ b/components/nofrendo/mappers/map085.c @@ -23,10 +23,10 @@ ** $Id: map085.c,v 1.3 2001/05/06 01:42:03 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../log.h" static struct { @@ -127,7 +127,7 @@ static void map85_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("unhandled vrc7 write: $%02X to $%04X\n", value, address); + nofrendo_log_printf("unhandled vrc7 write: $%02X to $%04X\n", value, address); #endif /* NOFRENDO_DEBUG */ break; } diff --git a/components/nofrendo/mappers/map087.c b/components/nofrendo/mappers/map087.c index 1de1a0a..5ae7c0b 100644 --- a/components/nofrendo/mappers/map087.c +++ b/components/nofrendo/mappers/map087.c @@ -25,11 +25,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" /******************************************/ /* Mapper #87 write handler ($6000-$7FFF) */ diff --git a/components/nofrendo/mappers/map093.c b/components/nofrendo/mappers/map093.c index 8986eca..ec9489d 100644 --- a/components/nofrendo/mappers/map093.c +++ b/components/nofrendo/mappers/map093.c @@ -23,9 +23,9 @@ ** $Id: map093.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" static void map93_write(uint32 address, uint8 value) { diff --git a/components/nofrendo/mappers/map094.c b/components/nofrendo/mappers/map094.c index b01ef6b..5081d30 100644 --- a/components/nofrendo/mappers/map094.c +++ b/components/nofrendo/mappers/map094.c @@ -23,8 +23,8 @@ ** $Id: map094.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 94: Senjou no Ookami */ static void map94_write(uint32 address, uint8 value) diff --git a/components/nofrendo/mappers/map099.c b/components/nofrendo/mappers/map099.c index 1ef5833..8aa19c9 100644 --- a/components/nofrendo/mappers/map099.c +++ b/components/nofrendo/mappers/map099.c @@ -23,9 +23,9 @@ ** $Id: map099.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" /* Switch VROM for VS games */ static void map99_vromswitch(uint8 value) diff --git a/components/nofrendo/mappers/map160.c b/components/nofrendo/mappers/map160.c index f5775d7..92b23a7 100644 --- a/components/nofrendo/mappers/map160.c +++ b/components/nofrendo/mappers/map160.c @@ -23,10 +23,10 @@ ** $Id: map160.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes_ppu.h" +#include "../nes/nes.h" static struct { @@ -71,7 +71,7 @@ static void map160_write(uint32 address, uint8 value) #ifdef NOFRENDO_DEBUG else { - log_printf("mapper 160: untrapped write $%02X to $%04X\n", value, address); + nofrendo_log_printf("mapper 160: untrapped write $%02X to $%04X\n", value, address); } #endif /* NOFRENDO_DEBUG */ } diff --git a/components/nofrendo/mappers/map229.c b/components/nofrendo/mappers/map229.c index 3f17b1b..c583aed 100644 --- a/components/nofrendo/mappers/map229.c +++ b/components/nofrendo/mappers/map229.c @@ -26,11 +26,11 @@ ** */ -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../libsnss/libsnss.h" +#include "../log.h" /************************/ /* Mapper #229: 31 in 1 */ diff --git a/components/nofrendo/mappers/map231.c b/components/nofrendo/mappers/map231.c index 9abf559..c2bf50d 100644 --- a/components/nofrendo/mappers/map231.c +++ b/components/nofrendo/mappers/map231.c @@ -23,8 +23,8 @@ ** $Id: map231.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" /* mapper 231: NINA-07, used in Wally Bear and the NO! Gang */ diff --git a/components/nofrendo/mappers/mapvrc.c b/components/nofrendo/mappers/mapvrc.c index 37c6c0c..1a36801 100644 --- a/components/nofrendo/mappers/mapvrc.c +++ b/components/nofrendo/mappers/mapvrc.c @@ -23,10 +23,10 @@ ** $Id: mapvrc.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include -#include +#include "../noftypes.h" +#include "../nes/nes_mmc.h" +#include "../nes/nes.h" +#include "../log.h" #define VRC_VBANK(bank, value, high) \ { \ @@ -151,8 +151,8 @@ static void map21_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("wrote $%02X to $%04X", value, address); -#endif + nofrendo_log_printf("wrote $%02X to $%04X", value, address); +#endif /* !NOFRENDO_DEBUG */ break; } } @@ -295,8 +295,8 @@ static void map23_write(uint32 address, uint8 value) default: #ifdef NOFRENDO_DEBUG - log_printf("wrote $%02X to $%04X",value,address); -#endif + nofrendo_log_printf("wrote $%02X to $%04X",value,address); +#endif /* !NOFRENDO_DEBUG */ break; } } diff --git a/components/nofrendo/memguard.c b/components/nofrendo/memguard.c index df30211..3a5dace 100644 --- a/components/nofrendo/memguard.c +++ b/components/nofrendo/memguard.c @@ -25,43 +25,35 @@ ** $Id: memguard.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include - -/* undefine macro definitions, so we get real calls */ -#undef malloc -#undef free -#undef strdup - #include #include -#include +#include "noftypes.h" +#include "memguard.h" +#include "log.h" /* Maximum number of allocated blocks at any one time */ -#define MAX_BLOCKS 4096 +#define MAX_BLOCKS 4096 /* Memory block structure */ typedef struct memblock_s { - void *block_addr; - int block_size; - char *file_name; - int line_num; + void *block_addr; + int block_size; + char *file_name; + int line_num; } memblock_t; /* debugging flag */ -bool mem_debug = true; - +bool mem_debug = NOFRENDO_MEM_DEBUG; #ifdef NOFRENDO_DEBUG -static int mem_blockcount = 0; /* allocated block count */ +static int mem_blockcount = 0; /* allocated block count */ static memblock_t *mem_record = NULL; -#define GUARD_STRING "GgUuAaRrDdSsTtRrIiNnGgBbLlOoCcKk" -#define GUARD_LENGTH 256 /* before and after allocated block */ - +#define GUARD_STRING "GgUuAaRrDdSsTtRrIiNnGgBbLlOoCcKk" +#define GUARD_LENGTH 256 /* before and after allocated block */ /* ** Check the memory guard to make sure out of bounds writes have not @@ -73,11 +65,11 @@ static int mem_checkguardblock(void *data, int guard_size) int i, alloc_size; /* get the original pointer */ - block = ((char *) data) - guard_size; + block = ((char *)data) - guard_size; /* get the size */ - alloc_size = *((uint32 *) block); - block+=4; + alloc_size = *((uint32 *)block); + block += 4; /* check leading guard string */ check = GUARD_STRING; @@ -86,14 +78,14 @@ static int mem_checkguardblock(void *data, int guard_size) /* wrap */ if ('\0' == *check) check = GUARD_STRING; - + if (*block++ != *check++) return -1; } /* check end of block */ check = GUARD_STRING; - block = ((char *) data) + alloc_size; + block = ((char *)data) + alloc_size; for (i = 0; i < guard_size; i++) { /* wrap */ @@ -110,7 +102,7 @@ static int mem_checkguardblock(void *data, int guard_size) /* free a guard block */ static void mem_freeguardblock(void *data, int guard_size) { - char *orig = ((char *) data) - guard_size; + char *orig = ((char *)data) - guard_size; free(orig); } @@ -127,23 +119,24 @@ static void *mem_guardalloc(int alloc_size, int guard_size) alloc_size = (alloc_size + 3) & ~3; /* allocate memory */ - orig = malloc(alloc_size + (guard_size * 2)); + // orig = malloc(alloc_size + (guard_size * 2)); + orig = mem_alloc(alloc_size + (guard_size * 2), true); if (NULL == orig) return NULL; - block = (char *) orig; - + block = (char *)orig; + /* get it to the pointer we will actually return */ - orig = (void *) ((char *) orig + guard_size); + orig = (void *)((char *)orig + guard_size); /* trash it all */ - ptr = (uint32 *) orig; + ptr = (uint32 *)orig; for (i = alloc_size / 4; i; i--) *ptr++ = 0xDEADBEEF; - + /* store the size of the newly allocated block*/ - *((uint32 *) block) = alloc_size; - block+=4; + *((uint32 *)block) = alloc_size; + block += 4; /* put guard string at beginning of block */ check = GUARD_STRING; @@ -158,20 +151,19 @@ static void *mem_guardalloc(int alloc_size, int guard_size) /* put at end of block */ check = GUARD_STRING; - block = (char *) orig + alloc_size; + block = (char *)orig + alloc_size; for (i = 0; i < guard_size; i++) { /* wrap */ if ('\0' == *check) check = GUARD_STRING; - + *block++ = *check++; } return orig; } - /* Free up the space used by the memory block manager */ void mem_cleanup(void) { @@ -182,7 +174,6 @@ void mem_cleanup(void) } } - /* Allocate a bunch of memory to keep track of all memory blocks */ static void mem_init(void) { @@ -190,12 +181,13 @@ static void mem_init(void) mem_blockcount = 0; - mem_record = malloc(MAX_BLOCKS * sizeof(memblock_t)); + // mem_record = malloc(MAX_BLOCKS * sizeof(memblock_t)); + mem_record = mem_alloc(MAX_BLOCKS * sizeof(memblock_t), false); + ASSERT(mem_record); memset(mem_record, 0, MAX_BLOCKS * sizeof(memblock_t)); } - /* add a block of memory to the master record */ static void mem_addblock(void *data, int block_size, char *file, int line) { @@ -229,7 +221,7 @@ static void mem_deleteblock(void *data, char *file, int line) if (mem_checkguardblock(mem_record[i].block_addr, GUARD_LENGTH)) { sprintf(fail, "mem_deleteblock 0x%08X at line %d of %s -- block corrupt", - (uint32) data, line, file); + (uint32)data, line, file); ASSERT_MSG(fail); } @@ -239,7 +231,7 @@ static void mem_deleteblock(void *data, char *file, int line) } sprintf(fail, "mem_deleteblock 0x%08X at line %d of %s -- block not found", - (uint32) data, line, file); + (uint32)data, line, file); ASSERT_MSG(fail); } #endif /* NOFRENDO_DEBUG */ @@ -259,9 +251,10 @@ void *_my_malloc(int size, char *file, int line) if (false != mem_debug) temp = mem_guardalloc(size, GUARD_LENGTH); else - temp = malloc(size); + // temp = malloc(size); + temp = mem_alloc(size, true); - printf("Malloc: %d at %s:%d\n", size, file, line); + nofrendo_log_printf("MALLOC_CAP_8BIT malloc: %d at %s:%d\n", size, file, line); if (NULL == temp) { sprintf(fail, "malloc: out of memory at line %d of %s. block size: %d\n", @@ -317,7 +310,7 @@ char *_my_strdup(const char *string, char *file, int line) if (NULL == string) return NULL; - temp = (char *) _my_malloc(strlen(string) + 1, file, line); + temp = (char *)_my_malloc(strlen(string) + 1, file, line); if (NULL == temp) return NULL; @@ -334,7 +327,8 @@ void *_my_malloc(int size) void *temp; char fail[256]; - temp = malloc(size); + // temp = malloc(size); + temp = mem_alloc(size, true); if (NULL == temp) { @@ -368,7 +362,7 @@ char *_my_strdup(const char *string) return NULL; /* will ASSERT for us */ - temp = (char *) _my_malloc(strlen(string) + 1); + temp = (char *)_my_malloc(strlen(string) + 1); if (NULL == temp) return NULL; @@ -390,25 +384,26 @@ void mem_checkleaks(void) if (mem_blockcount) { - log_printf("memory leak - %d unfreed block%s\n\n", mem_blockcount, - mem_blockcount == 1 ? "" : "s"); + nofrendo_log_printf("memory leak - %d unfreed block%s\n\n", mem_blockcount, + mem_blockcount == 1 ? "" : "s"); for (i = 0; i < MAX_BLOCKS; i++) { if (mem_record[i].block_addr) { - log_printf("addr: 0x%08X, size: %d, line %d of %s%s\n", - (uint32) mem_record[i].block_addr, - mem_record[i].block_size, - mem_record[i].line_num, - mem_record[i].file_name, - (mem_checkguardblock(mem_record[i].block_addr, GUARD_LENGTH)) - ? " -- block corrupt" : ""); + nofrendo_log_printf("addr: 0x%08X, size: %d, line %d of %s%s\n", + (uint32)mem_record[i].block_addr, + mem_record[i].block_size, + mem_record[i].line_num, + mem_record[i].file_name, + (mem_checkguardblock(mem_record[i].block_addr, GUARD_LENGTH)) + ? " -- block corrupt" + : ""); } } } else - log_printf("no memory leaks\n"); + nofrendo_log_printf("no memory leaks\n"); #endif /* NOFRENDO_DEBUG */ } @@ -426,11 +421,11 @@ void mem_checkblocks(void) { if (mem_checkguardblock(mem_record[i].block_addr, GUARD_LENGTH)) { - log_printf("addr: 0x%08X, size: %d, line %d of %s -- block corrupt\n", - (uint32) mem_record[i].block_addr, - mem_record[i].block_size, - mem_record[i].line_num, - mem_record[i].file_name); + nofrendo_log_printf("addr: 0x%08X, size: %d, line %d of %s -- block corrupt\n", + (uint32)mem_record[i].block_addr, + mem_record[i].block_size, + mem_record[i].line_num, + mem_record[i].file_name); } } } diff --git a/components/nofrendo/memguard.h b/components/nofrendo/memguard.h index 55b99da..294e8bb 100644 --- a/components/nofrendo/memguard.h +++ b/components/nofrendo/memguard.h @@ -26,26 +26,24 @@ #ifndef _MEMGUARD_H_ #define _MEMGUARD_H_ -#ifdef strdup -#undef strdup -#endif +#include "noftypes.h" #ifdef NOFRENDO_DEBUG -#define malloc(s) _my_malloc((s), __FILE__, __LINE__) -#define free(d) _my_free((void **) &(d), __FILE__, __LINE__) -#define strdup(s) _my_strdup((s), __FILE__, __LINE__) +#define NOFRENDO_MALLOC(s) _my_malloc((s), __FILE__, __LINE__) +#define NOFRENDO_FREE(d) _my_free((void **) &(d), __FILE__, __LINE__) +#define NOFRENDO_STRDUP(s) _my_strdup((s), __FILE__, __LINE__) extern void *_my_malloc(int size, char *file, int line); extern void _my_free(void **data, char *file, int line); extern char *_my_strdup(const char *string, char *file, int line); -#else /* !NORFRENDO_DEBUG */ +#else /* !NOFRENDO_DEBUG */ /* Non-debugging versions of calls */ -#define malloc(s) _my_malloc((s)) -#define free(d) _my_free((void **) &(d)) -#define strdup(s) _my_strdup((s)) +#define NOFRENDO_MALLOC(s) _my_malloc((s)) +#define NOFRENDO_FREE(d) _my_free((void **) &(d)) +#define NOFRENDO_STRDUP(s) _my_strdup((s)) extern void *_my_malloc(int size); extern void _my_free(void **data); @@ -53,13 +51,14 @@ extern char *_my_strdup(const char *string); #endif /* !NOFRENDO_DEBUG */ - extern void mem_cleanup(void); extern void mem_checkblocks(void); extern void mem_checkleaks(void); extern bool mem_debug; +extern void *mem_alloc(int size, bool prefer_fast_memory); + #endif /* _MEMGUARD_H_ */ /* diff --git a/components/nofrendo/nes/mmclist.c b/components/nofrendo/nes/mmclist.c index 0542e34..dd9c8b2 100644 --- a/components/nofrendo/nes/mmclist.c +++ b/components/nofrendo/nes/mmclist.c @@ -23,8 +23,8 @@ ** $Id: mmclist.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include +#include "../noftypes.h" +#include "nes_mmc.h" /* mapper interfaces */ extern mapintf_t map0_intf; @@ -36,6 +36,7 @@ extern mapintf_t map5_intf; extern mapintf_t map7_intf; extern mapintf_t map8_intf; extern mapintf_t map9_intf; +extern mapintf_t map10_intf; extern mapintf_t map11_intf; extern mapintf_t map15_intf; extern mapintf_t map16_intf; diff --git a/components/nofrendo/nes/mmclist.h b/components/nofrendo/nes/mmclist.h index e7fa9f7..da2ac93 100644 --- a/components/nofrendo/nes/mmclist.h +++ b/components/nofrendo/nes/mmclist.h @@ -26,7 +26,7 @@ #ifndef _MMCLIST_H_ #define _MMCLIST_H_ -#include +#include "nes_mmc.h" extern mapintf_t *mappers[]; diff --git a/components/nofrendo/nes/nes.c b/components/nofrendo/nes/nes.c index 4055d08..aaa063a 100644 --- a/components/nofrendo/nes/nes.c +++ b/components/nofrendo/nes/nes.c @@ -26,18 +26,19 @@ #include #include #include -#include -#include "nes6502.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include + +#include "../noftypes.h" +#include "../cpu/nes6502.h" +#include "../log.h" +#include "../osd.h" +#include "../gui.h" +#include "nes.h" +#include "../sndhrdw/nes_apu.h" +#include "nes_ppu.h" +#include "nes_rom.h" +#include "nes_mmc.h" +#include "../vid_drv.h" +#include "../nofrendo.h" #define NES_CLOCK_DIVIDER 12 @@ -343,8 +344,8 @@ static void system_video(bool draw) } /* blit the NES screen to our video surface */ -// vid_blit(nes.vidbuf, 0, (NES_SCREEN_HEIGHT - NES_VISIBLE_HEIGHT) / 2, -// 0, 0, NES_SCREEN_WIDTH, NES_VISIBLE_HEIGHT); +// vid_blit(nes.vidbuf, 0, (NES_SCREEN_HEIGHT - NES_SCREEN_HEIGHT) / 2, +// 0, 0, NES_SCREEN_WIDTH, NES_SCREEN_HEIGHT); /* overlay our GUI on top of it */ gui_frame(true); @@ -442,11 +443,11 @@ void nes_destroy(nes_t **machine) if ((*machine)->cpu) { if ((*machine)->cpu->mem_page[0]) - free((*machine)->cpu->mem_page[0]); - free((*machine)->cpu); + NOFRENDO_FREE((*machine)->cpu->mem_page[0]); + NOFRENDO_FREE((*machine)->cpu); } - free(*machine); + NOFRENDO_FREE(*machine); *machine = NULL; } } @@ -509,7 +510,7 @@ nes_t *nes_create(void) sndinfo_t osd_sound; int i; - machine = malloc(sizeof(nes_t)); + machine = NOFRENDO_MALLOC(sizeof(nes_t)); if (NULL == machine) return NULL; @@ -524,14 +525,14 @@ nes_t *nes_create(void) machine->autoframeskip = true; /* cpu */ - machine->cpu = malloc(sizeof(nes6502_context)); + machine->cpu = NOFRENDO_MALLOC(sizeof(nes6502_context)); if (NULL == machine->cpu) goto _fail; memset(machine->cpu, 0, sizeof(nes6502_context)); /* allocate 2kB RAM */ - machine->cpu->mem_page[0] = malloc(NES_RAMSIZE); + machine->cpu->mem_page[0] = NOFRENDO_MALLOC(NES_RAMSIZE); if (NULL == machine->cpu->mem_page[0]) goto _fail; diff --git a/components/nofrendo/nes/nes.h b/components/nofrendo/nes/nes.h index 1b080a7..aa8a9cb 100644 --- a/components/nofrendo/nes/nes.h +++ b/components/nofrendo/nes/nes.h @@ -26,18 +26,14 @@ #ifndef _NES_H_ #define _NES_H_ -#include -#include -#include -#include -#include -#include "nes6502.h" -#include - -/* Visible (NTSC) screen height */ -#ifndef NES_VISIBLE_HEIGHT -#define NES_VISIBLE_HEIGHT 224 -#endif /* !NES_VISIBLE_HEIGHT */ +#include "../noftypes.h" +#include "../sndhrdw/nes_apu.h" +#include "nes_mmc.h" +#include "nes_ppu.h" +#include "nes_rom.h" +#include "../cpu/nes6502.h" +#include "../bitmap.h" + #define NES_SCREEN_WIDTH 256 #define NES_SCREEN_HEIGHT 240 diff --git a/components/nofrendo/nes/nes_mmc.c b/components/nofrendo/nes/nes_mmc.c index 12c434d..92e2f7c 100644 --- a/components/nofrendo/nes/nes_mmc.c +++ b/components/nofrendo/nes/nes_mmc.c @@ -24,14 +24,15 @@ */ #include -#include -#include "nes6502.h" -#include -#include -#include -#include -#include -#include + +#include "../noftypes.h" +#include "../cpu/nes6502.h" +#include "nes_mmc.h" +#include "nes_ppu.h" +#include "../libsnss/libsnss.h" +#include "../log.h" +#include "mmclist.h" +#include "nes_rom.h" #define MMC_8KROM (mmc.cart->rom_banks * 2) #define MMC_16KROM (mmc.cart->rom_banks) @@ -101,7 +102,7 @@ void mmc_bankvrom(int size, uint32 address, int bank) break; default: - log_printf("invalid VROM bank size %d\n", size); + nofrendo_log_printf("invalid VROM bank size %d\n", size); } } @@ -152,7 +153,7 @@ void mmc_bankrom(int size, uint32 address, int bank) break; default: - log_printf("invalid ROM bank size %d\n", size); + nofrendo_log_printf("invalid ROM bank size %d\n", size); break; } @@ -176,7 +177,7 @@ bool mmc_peek(int map_num) static void mmc_setpages(void) { - log_printf("setting up mapper %d\n", mmc.intf->number); + nofrendo_log_printf("setting up mapper %d\n", mmc.intf->number); /* Switch ROM into CPU space, set VROM/VRAM (done for ALL ROMs) */ mmc_bankrom(16, 0x8000, 0); @@ -217,14 +218,14 @@ void mmc_reset(void) if (mmc.intf->init) mmc.intf->init(); - log_printf("reset memory mapper\n"); + nofrendo_log_printf("reset memory mapper\n"); } void mmc_destroy(mmc_t **nes_mmc) { if (*nes_mmc) - free(*nes_mmc); + NOFRENDO_FREE(*nes_mmc); } mmc_t *mmc_create(rominfo_t *rominfo) @@ -238,7 +239,7 @@ mmc_t *mmc_create(rominfo_t *rominfo) return NULL; /* Should *never* happen */ } - temp = malloc(sizeof(mmc_t)); + temp = NOFRENDO_MALLOC(sizeof(mmc_t)); if (NULL == temp) return NULL; @@ -249,7 +250,7 @@ mmc_t *mmc_create(rominfo_t *rominfo) mmc_setcontext(temp); - log_printf("created memory mapper: %s\n", (*map_ptr)->name); + nofrendo_log_printf("created memory mapper: %s\n", (*map_ptr)->name); return temp; } diff --git a/components/nofrendo/nes/nes_mmc.h b/components/nofrendo/nes/nes_mmc.h index a8f6a10..4b8372a 100644 --- a/components/nofrendo/nes/nes_mmc.h +++ b/components/nofrendo/nes/nes_mmc.h @@ -26,8 +26,8 @@ #ifndef _NES_MMC_H_ #define _NES_MMC_H_ -#include -#include +#include "../libsnss/libsnss.h" +#include "../sndhrdw/nes_apu.h" #define MMC_LASTBANK -1 @@ -59,7 +59,7 @@ typedef struct mapintf_s } mapintf_t; -#include +#include "nes_rom.h" typedef struct mmc_s { mapintf_t *intf; diff --git a/components/nofrendo/nes/nes_pal.c b/components/nofrendo/nes/nes_pal.c index 5756ba5..f5566ec 100644 --- a/components/nofrendo/nes/nes_pal.c +++ b/components/nofrendo/nes/nes_pal.c @@ -24,9 +24,9 @@ */ #include -#include -#include -#include +#include "../noftypes.h" +#include "../bitmap.h" +#include "nes_pal.h" #ifndef PI #define PI 3.1415926535897932384626433832795 @@ -67,7 +67,7 @@ rgb_t nes_palette[64]; static float hue = 334.0f; static float tint = 0.4f; -#include +#include "../gui.h" void pal_dechue(void) { diff --git a/components/nofrendo/nes/nes_ppu.c b/components/nofrendo/nes/nes_ppu.c index 9a6875b..8ed0590 100644 --- a/components/nofrendo/nes/nes_ppu.c +++ b/components/nofrendo/nes/nes_ppu.c @@ -25,18 +25,19 @@ #include #include -#include -#include -#include -#include -#include "nes6502.h" -#include -#include -#include -#include -#include -#include +#include "../noftypes.h" +#include "nes_ppu.h" +#include "nes.h" +#include "../gui.h" +#include "../cpu/nes6502.h" +#include "../log.h" +#include "nes_mmc.h" + +#include "../bitmap.h" +#include "../vid_drv.h" +#include "nes_pal.h" +#include "nesinput.h" /* PPU access */ @@ -125,7 +126,7 @@ ppu_t *ppu_create(void) static bool pal_generated = false; ppu_t *temp; - temp = malloc(sizeof(ppu_t)); + temp = NOFRENDO_MALLOC(sizeof(ppu_t)); if (NULL == temp) return NULL; @@ -152,7 +153,7 @@ void ppu_destroy(ppu_t **src_ppu) { if (*src_ppu) { - free(*src_ppu); + NOFRENDO_FREE(*src_ppu); *src_ppu = NULL; } } @@ -372,8 +373,8 @@ uint8 ppu_read(uint32 address) if ((ppu.bg_on || ppu.obj_on) && !ppu.vram_accessible) { ppu.vdata_latch = 0xFF; - log_printf("VRAM read at $%04X, scanline %d\n", - ppu.vaddr, nes_getcontextptr()->scanline); + // nofrendo_log_printf("VRAM read at $%04X, scanline %d\n", + // ppu.vaddr, nes_getcontextptr()->scanline); } else { @@ -485,7 +486,7 @@ void ppu_write(uint32 address, uint8 value) /* VRAM only accessible during scanlines 241-260 */ if ((ppu.bg_on || ppu.obj_on) && !ppu.vram_accessible) { - log_printf("VRAM write to $%04X, scanline %d\n", + nofrendo_log_printf("VRAM write to $%04X, scanline %d\n", ppu.vaddr, nes_getcontextptr()->scanline); PPU_MEM(ppu.vaddr) = 0xFF; /* corrupt */ } @@ -787,7 +788,7 @@ typedef struct obj_s static void ppu_renderoam(uint8 *vidbuf, int scanline) { uint8 *buf_ptr; - uint32 vram_offset, savecol[2]; + uint32 vram_offset, savecol[2] = {0}; int sprite_num, spritecount; obj_t *sprite_ptr; uint8 sprite_height; @@ -1080,7 +1081,8 @@ void ppu_checknmi(void) void ppu_scanline(bitmap_t *bmp, int scanline, bool draw_flag) { - if (scanline < 240) + // if (scanline < 240) + if (scanline < NES_SCREEN_HEIGHT) { /* Lower the Max Sprite per scanline flag */ ppu.stat &= ~PPU_STATF_MAXSPRITE; diff --git a/components/nofrendo/nes/nes_ppu.h b/components/nofrendo/nes/nes_ppu.h index ab020f3..fff5e31 100644 --- a/components/nofrendo/nes/nes_ppu.h +++ b/components/nofrendo/nes/nes_ppu.h @@ -26,7 +26,7 @@ #ifndef _NES_PPU_H_ #define _NES_PPU_H_ -#include +#include "../bitmap.h" /* PPU register defines */ #define PPU_CTRL0 0x2000 diff --git a/components/nofrendo/nes/nes_rom.c b/components/nofrendo/nes/nes_rom.c index 08580f0..c482860 100644 --- a/components/nofrendo/nes/nes_rom.c +++ b/components/nofrendo/nes/nes_rom.c @@ -27,60 +27,56 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -extern char *osd_getromdata(); -/* Max length for displayed filename */ -#define ROM_DISP_MAXLEN 20 +#include "../noftypes.h" +#include "nes_rom.h" +#include "../intro.h" +#include "nes_mmc.h" +#include "nes_ppu.h" +#include "nes.h" +#include "../gui.h" +#include "../log.h" +#include "../osd.h" +/* Max length for displayed filename */ +#define ROM_DISP_MAXLEN 20 #ifdef ZLIB #include -#define _fopen gzopen -#define _fclose gzclose -#define _fread(B,N,L,F) gzread((F),(B),(L)*(N)) +#define _fopen gzopen +#define _fclose gzclose +#define _fread(B, N, L, F) gzread((F), (B), (L) * (N)) #else -#define _fopen fopen -#define _fclose fclose -#define _fread(B,N,L,F) fread((B),(N),(L),(F)) +#define _fopen fopen +#define _fclose fclose +#define _fread(B, N, L, F) fread((B), (N), (L), (F)) #endif -#define ROM_FOURSCREEN 0x08 -#define ROM_TRAINER 0x04 -#define ROM_BATTERY 0x02 -#define ROM_MIRRORTYPE 0x01 -#define ROM_INES_MAGIC "NES\x1A" +#define ROM_FOURSCREEN 0x08 +#define ROM_TRAINER 0x04 +#define ROM_BATTERY 0x02 +#define ROM_MIRRORTYPE 0x01 +#define ROM_INES_MAGIC "NES\x1A" -//ToDo: packed - JD typedef struct inesheader_s { - uint8 ines_magic[4] ; - uint8 rom_banks ; - uint8 vrom_banks ; - uint8 rom_type ; - uint8 mapper_hinybble ; - uint8 reserved[8] ; -} inesheader_t; - + uint8 ines_magic[4]; + uint8 rom_banks; + uint8 vrom_banks; + uint8 rom_type; + uint8 mapper_hinybble; + uint8 reserved[8]; +} __attribute__((packed)) inesheader_t; -#define TRAINER_OFFSET 0x1000 -#define TRAINER_LENGTH 0x200 -#define VRAM_LENGTH 0x2000 +#define TRAINER_OFFSET 0x1000 +#define TRAINER_LENGTH 0x200 +#define VRAM_LENGTH 0x2000 -#define ROM_BANK_LENGTH 0x4000 -#define VROM_BANK_LENGTH 0x2000 +#define ROM_BANK_LENGTH 0x4000 +#define VROM_BANK_LENGTH 0x2000 -#define SRAM_BANK_LENGTH 0x0400 -#define VRAM_BANK_LENGTH 0x2000 +#define SRAM_BANK_LENGTH 0x0400 +#define VRAM_BANK_LENGTH 0x2000 /* Save battery-backed RAM */ static void rom_savesram(rominfo_t *rominfo) @@ -100,7 +96,7 @@ static void rom_savesram(rominfo_t *rominfo) { fwrite(rominfo->sram, SRAM_BANK_LENGTH, rominfo->sram_banks, fp); fclose(fp); - log_printf("Wrote battery RAM to %s.\n", fn); + nofrendo_log_printf("Wrote battery RAM to %s.\n", fn); } } } @@ -123,7 +119,7 @@ static void rom_loadsram(rominfo_t *rominfo) { fread(rominfo->sram, SRAM_BANK_LENGTH, rominfo->sram_banks, fp); fclose(fp); - log_printf("Read battery RAM from %s.\n", fn); + nofrendo_log_printf("Read battery RAM from %s.\n", fn); } } } @@ -132,7 +128,7 @@ static void rom_loadsram(rominfo_t *rominfo) static int rom_allocsram(rominfo_t *rominfo) { /* Load up SRAM */ - rominfo->sram = malloc(SRAM_BANK_LENGTH * rominfo->sram_banks); + rominfo->sram = NOFRENDO_MALLOC(SRAM_BANK_LENGTH * rominfo->sram_banks); if (NULL == rominfo->sram) { gui_sendmsg(GUI_RED, "Could not allocate space for battery RAM"); @@ -145,58 +141,48 @@ static int rom_allocsram(rominfo_t *rominfo) } /* If there's a trainer, load it in at $7000 */ -static void rom_loadtrainer(unsigned char **rom, rominfo_t *rominfo) +static void rom_loadtrainer(FILE *fp, rominfo_t *rominfo) { - ASSERT(rom); + ASSERT(fp); ASSERT(rominfo); if (rominfo->flags & ROM_FLAG_TRAINER) { -// fread(rominfo->sram + TRAINER_OFFSET, TRAINER_LENGTH, 1, fp); - memcpy(rominfo->sram + TRAINER_OFFSET, *rom, TRAINER_LENGTH); - rom+=TRAINER_LENGTH; - log_printf("Read in trainer at $7000\n"); + fread(rominfo->sram + TRAINER_OFFSET, TRAINER_LENGTH, 1, fp); + nofrendo_log_printf("Read in trainer at $7000\n"); } } -static int rom_loadrom(unsigned char **rom, rominfo_t *rominfo) +static int rom_loadrom(FILE *fp, rominfo_t *rominfo) { - ASSERT(rom); + ASSERT(fp); ASSERT(rominfo); /* Allocate ROM space, and load it up! */ -/* - rominfo->rom = malloc((rominfo->rom_banks * ROM_BANK_LENGTH)); + // rominfo->rom = malloc(rominfo->rom_banks * ROM_BANK_LENGTH); + rominfo->rom = mem_alloc(rominfo->rom_banks * ROM_BANK_LENGTH, false); if (NULL == rominfo->rom) { gui_sendmsg(GUI_RED, "Could not allocate space for ROM image"); return -1; } _fread(rominfo->rom, ROM_BANK_LENGTH, rominfo->rom_banks, fp); -*/ - rominfo->rom=*rom; - *rom+=ROM_BANK_LENGTH*rominfo->rom_banks; - /* If there's VROM, allocate and stuff it in */ if (rominfo->vrom_banks) { -/* - rominfo->vrom = malloc((rominfo->vrom_banks * VROM_BANK_LENGTH)); + // rominfo->vrom = malloc((rominfo->vrom_banks * VROM_BANK_LENGTH)); + rominfo->vrom = mem_alloc(rominfo->vrom_banks * VROM_BANK_LENGTH, false); if (NULL == rominfo->vrom) { gui_sendmsg(GUI_RED, "Could not allocate space for VROM"); return -1; } _fread(rominfo->vrom, VROM_BANK_LENGTH, rominfo->vrom_banks, fp); -*/ - rominfo->vrom=*rom; - *rom+=VROM_BANK_LENGTH*rominfo->vrom_banks; - } else { - rominfo->vram = malloc(VRAM_LENGTH); + rominfo->vram = NOFRENDO_MALLOC(VRAM_LENGTH); if (NULL == rominfo->vram) { gui_sendmsg(GUI_RED, "Could not allocate space for VRAM"); @@ -236,8 +222,8 @@ static void rom_checkforpal(rominfo_t *rominfo) /* TODO: this should really be a *SYSTEM* flag */ rominfo->flags |= ROM_FLAG_VERSUS; /* TODO: bad, BAD idea, calling nes_getcontextptr... */ - ppu_setpal(nes_getcontextptr()->ppu, vs_pal); - log_printf("Game specific palette found -- assuming VS. UniSystem\n"); + // ppu_setpal(nes_getcontextptr()->ppu, vs_pal); + nofrendo_log_printf("Game specific palette found -- assuming VS. UniSystem\n"); } static FILE *rom_findrom(const char *filename, rominfo_t *rominfo) @@ -270,7 +256,7 @@ static FILE *rom_findrom(const char *filename, rominfo_t *rominfo) static int rom_adddirty(char *filename) { #ifdef NOFRENDO_DEBUG -#define MAX_BUFFER_LENGTH 255 +#define MAX_BUFFER_LENGTH 255 char buffer[MAX_BUFFER_LENGTH + 1]; bool found = false; @@ -323,22 +309,19 @@ int rom_checkmagic(const char *filename) return -1; } -static int rom_getheader(unsigned char **rom, rominfo_t *rominfo) +static int rom_getheader(FILE *fp, rominfo_t *rominfo) { -#define RESERVED_LENGTH 8 +#define RESERVED_LENGTH 8 inesheader_t head; uint8 reserved[RESERVED_LENGTH]; bool header_dirty; - ASSERT(rom); - ASSERT(*rom); + ASSERT(fp); ASSERT(rominfo); /* Read in the header */ -// _fread(&head, 1, sizeof(head), fp); - printf("Head: %p (%x %x %x %x)\n", *rom, (*rom)[0], (*rom)[1], (*rom)[2], (*rom)[3]); - memcpy(&head, *rom, sizeof(head)); - *rom+=sizeof(head); + _fread(&head, sizeof(head), 1, fp); + if (memcmp(head.ines_magic, ROM_INES_MAGIC, 4)) { @@ -376,10 +359,10 @@ static int rom_getheader(unsigned char **rom, rominfo_t *rominfo) /* @!?#@! DiskDude. */ if (('D' == head.mapper_hinybble) && (0 == memcmp(head.reserved, "iskDude!", 8))) - log_printf("`DiskDude!' found in ROM header, ignoring high mapper nybble\n"); + nofrendo_log_printf("`DiskDude!' found in ROM header, ignoring high mapper nybble\n"); else { - log_printf("ROM header dirty, possible problem\n"); + nofrendo_log_printf("ROM header dirty, possible problem\n"); rominfo->mapper_number |= (head.mapper_hinybble & 0xF0); } @@ -421,7 +404,7 @@ char *rom_getinfo(rominfo_t *rominfo) sprintf(temp, " [%d] %dk/%dk %c", rominfo->mapper_number, rominfo->rom_banks * 16, rominfo->vrom_banks * 8, (rominfo->mirror == MIRROR_VERT) ? 'V' : 'H'); - + /* Stick it on there! */ strncat(info, temp, PATH_MAX - strlen(info)); @@ -438,17 +421,24 @@ char *rom_getinfo(rominfo_t *rominfo) /* Load a ROM image into memory */ rominfo_t *rom_load(const char *filename) { - unsigned char *rom=(unsigned char*)osd_getromdata(); + FILE *fp; rominfo_t *rominfo; - rominfo = malloc(sizeof(rominfo_t)); + rominfo = NOFRENDO_MALLOC(sizeof(rominfo_t)); if (NULL == rominfo) return NULL; memset(rominfo, 0, sizeof(rominfo_t)); + fp = rom_findrom(filename, rominfo); + + if (NULL == fp) + gui_sendmsg(GUI_RED, "%s not found, will use default ROM", filename); + /* Get the header and stick it into rominfo struct */ - if (rom_getheader(&rom, rominfo)) + if (NULL == fp) + intro_get_header(rominfo); + else if (rom_getheader(fp, rominfo)) goto _fail; /* Make sure we really support the mapper */ @@ -465,21 +455,33 @@ rominfo_t *rom_load(const char *filename) if (rom_allocsram(rominfo)) goto _fail; - rom_loadtrainer(&rom, rominfo); + if (NULL != fp) + rom_loadtrainer(fp, rominfo); - if (rom_loadrom(&rom, rominfo)) + if (NULL == fp) + { + if (intro_get_rom(rominfo)) + goto _fail; + } + else if (rom_loadrom(fp, rominfo)) goto _fail; + /* Close the file */ + if (NULL != fp) + _fclose(fp); + rom_loadsram(rominfo); /* See if there's a palette we can load up */ -// rom_checkforpal(rominfo); + rom_checkforpal(rominfo); gui_sendmsg(GUI_GREEN, "ROM loaded: %s", rom_getinfo(rominfo)); return rominfo; _fail: + if (NULL != fp) + _fclose(fp); rom_free(&rominfo); return NULL; } @@ -498,21 +500,21 @@ void rom_free(rominfo_t **rominfo) { /* TODO: bad idea calling nes_getcontextptr... */ ppu_setdefaultpal(nes_getcontextptr()->ppu); - log_printf("Default NES palette restored\n"); + nofrendo_log_printf("Default NES palette restored\n"); } rom_savesram(*rominfo); if ((*rominfo)->sram) - free((*rominfo)->sram); + NOFRENDO_FREE((*rominfo)->sram); if ((*rominfo)->rom) - free((*rominfo)->rom); + NOFRENDO_FREE((*rominfo)->rom); if ((*rominfo)->vrom) - free((*rominfo)->vrom); + NOFRENDO_FREE((*rominfo)->vrom); if ((*rominfo)->vram) - free((*rominfo)->vram); + NOFRENDO_FREE((*rominfo)->vram); - free(*rominfo); + NOFRENDO_FREE(*rominfo); gui_sendmsg(GUI_GREEN, "ROM freed"); } diff --git a/components/nofrendo/nes/nes_rom.h b/components/nofrendo/nes/nes_rom.h index ba86d2f..dad1f97 100644 --- a/components/nofrendo/nes/nes_rom.h +++ b/components/nofrendo/nes/nes_rom.h @@ -27,7 +27,8 @@ #define _NES_ROM_H_ #include -#include + +#include "../osd.h" typedef enum { diff --git a/components/nofrendo/nes/nesinput.c b/components/nofrendo/nes/nesinput.c index d425f9c..5a55dbc 100644 --- a/components/nofrendo/nes/nesinput.c +++ b/components/nofrendo/nes/nesinput.c @@ -23,9 +23,9 @@ ** $Id: nesinput.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "nesinput.h" +#include "../log.h" /* TODO: make a linked list of inputs sources, so they ** can be removed if need be diff --git a/components/nofrendo/nes/nesstate.c b/components/nofrendo/nes/nesstate.c index c010f56..c461172 100644 --- a/components/nofrendo/nes/nesstate.c +++ b/components/nofrendo/nes/nesstate.c @@ -25,14 +25,15 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include "nes6502.h" + +#include "../noftypes.h" +#include "nesstate.h" +#include "../gui.h" +#include "nes.h" +#include "../log.h" +#include "../osd.h" +#include "../libsnss/libsnss.h" +#include "../cpu/nes6502.h" #define FIRST_STATE_SLOT 0 #define LAST_STATE_SLOT 9 @@ -99,7 +100,7 @@ static bool save_vramblock(nes_t *state, SNSS_FILE *snssFile) if (state->rominfo->vram_banks > 2) { - log_printf("too many VRAM banks: %d\n", state->rominfo->vram_banks); + nofrendo_log_printf("too many VRAM banks: %d\n", state->rominfo->vram_banks); return -1; } @@ -134,7 +135,7 @@ static int save_sramblock(nes_t *state, SNSS_FILE *snssFile) if (state->rominfo->sram_banks > 8) { - log_printf("Unsupported number of SRAM banks: %d\n", state->rominfo->sram_banks); + nofrendo_log_printf("Unsupported number of SRAM banks: %d\n", state->rominfo->sram_banks); return -1; } @@ -474,7 +475,7 @@ int state_load(void) case SNSS_UNKNOWN_BLOCK: default: - log_printf("unknown SNSS block type\n"); + nofrendo_log_printf("unknown SNSS block type\n"); break; } } diff --git a/components/nofrendo/nes/nesstate.h b/components/nofrendo/nes/nesstate.h index 387d8d7..cae51e8 100644 --- a/components/nofrendo/nes/nesstate.h +++ b/components/nofrendo/nes/nesstate.h @@ -26,7 +26,7 @@ #ifndef _NESSTATE_H_ #define _NESSTATE_H_ -#include +#include "nes.h" extern void state_setslot(int slot); extern int state_load(); diff --git a/components/nofrendo/nofconfig.h b/components/nofrendo/nofconfig.h index 961a017..dcb44e6 100644 --- a/components/nofrendo/nofconfig.h +++ b/components/nofrendo/nofconfig.h @@ -5,13 +5,15 @@ ** $Id: nofconfig.h,v 1.1 2001/04/27 14:37:11 neil Exp $ */ -#ifndef _CONFIG_H_ -#define _CONFIG_H_ +#ifndef _NOFCONFIG_H_ +#define _NOFCONFIG_H_ #ifndef CONFIG_FILE #define CONFIG_FILE "nofrendo.cfg" #endif +#include "noftypes.h" + typedef struct config_s { /* open loads from the disk the saved configuration. @@ -47,7 +49,7 @@ typedef struct config_s extern config_t config; -#endif /* !_CONFIG_H_ */ +#endif /* _NOFCONFIG_H_ */ /* ** $Log: nofconfig.h,v $ diff --git a/components/nofrendo/nofrendo.c b/components/nofrendo/nofrendo.c index 52de685..cc369bf 100644 --- a/components/nofrendo/nofrendo.c +++ b/components/nofrendo/nofrendo.c @@ -26,17 +26,18 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include + +#include "noftypes.h" +#include "nofrendo.h" +#include "event.h" +#include "nofconfig.h" +#include "log.h" +#include "osd.h" +#include "gui.h" +#include "vid_drv.h" /* emulated system includes */ -#include +#include "nes/nes.h" /* our global machine structure */ static struct @@ -67,12 +68,12 @@ static void shutdown_everything(void) { if (console.filename) { - free(console.filename); + NOFRENDO_FREE(console.filename); console.filename = NULL; } if (console.nextfilename) { - free(console.nextfilename); + NOFRENDO_FREE(console.nextfilename); console.nextfilename = NULL; } @@ -99,7 +100,7 @@ void main_eject(void) if (NULL != console.filename) { - free(console.filename); + NOFRENDO_FREE(console.filename); console.filename = NULL; } console.type = system_unknown; @@ -115,7 +116,7 @@ void main_quit(void) /* if there's a pending filename / system, clear */ if (NULL != console.nextfilename) { - free(console.nextfilename); + NOFRENDO_FREE(console.nextfilename); console.nextfilename = NULL; } console.nexttype = system_unknown; @@ -149,7 +150,7 @@ static int internal_insert(const char *filename, system_t type) if (system_autodetect == type) type = detect_systemtype(filename); - console.filename = strdup(filename); + console.filename = NOFRENDO_STRDUP(filename); console.type = type; /* set up the event system for this system type */ @@ -163,14 +164,14 @@ static int internal_insert(const char *filename, system_t type) console.machine.nes = nes_create(); if (NULL == console.machine.nes) { - log_printf("Failed to create NES instance.\n"); + nofrendo_log_printf("Failed to create NES instance.\n"); return -1; } if (nes_insertcart(console.filename, console.machine.nes)) return -1; - vid_setmode(NES_SCREEN_WIDTH, NES_VISIBLE_HEIGHT); + vid_setmode(NES_SCREEN_WIDTH, NES_SCREEN_HEIGHT); if (install_timer(NES_REFRESH_RATE)) return -1; @@ -180,9 +181,9 @@ static int internal_insert(const char *filename, system_t type) case system_unknown: default: - log_printf("system type unknown, playing nofrendo NES intro.\n"); + nofrendo_log_printf("system type unknown, playing nofrendo NES intro.\n"); if (NULL != console.filename) - free(console.filename); + NOFRENDO_FREE(console.filename); /* oooh, recursion */ return internal_insert(filename, system_nes); @@ -194,7 +195,7 @@ static int internal_insert(const char *filename, system_t type) /* This tells main_loop to load this next image */ void main_insert(const char *filename, system_t type) { - console.nextfilename = strdup(filename); + console.nextfilename = NOFRENDO_STRDUP(filename); console.nexttype = type; main_eject(); @@ -240,7 +241,7 @@ int main_loop(const char *filename, system_t type) return -1; printf("vid_init done\n"); - console.nextfilename = strdup(filename); + console.nextfilename = NOFRENDO_STRDUP(filename); console.nexttype = type; while (false == console.quit) diff --git a/components/nofrendo/noftypes.h b/components/nofrendo/noftypes.h index 4ea0b2f..5801a0d 100644 --- a/components/nofrendo/noftypes.h +++ b/components/nofrendo/noftypes.h @@ -23,8 +23,13 @@ ** $Id: noftypes.h,v 1.1 2001/04/27 14:37:11 neil Exp $ */ -#ifndef _TYPES_H_ -#define _TYPES_H_ +#ifndef _NOFTYPES_H_ +#define _NOFTYPES_H_ + +#include + +#define NOFRENDO_DEBUG +#define NOFRENDO_MEM_DEBUG true /* Define this if running on little-endian (x86) systems */ #define HOST_LITTLE_ENDIAN @@ -50,20 +55,8 @@ typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; -#ifndef __cplusplus -typedef enum -{ - false = 0, - true = 1 -} bool; - -#ifndef NULL -#define NULL ((void *) 0) -#endif -#endif /* !__cplusplus */ - -#include -#include +#include "memguard.h" +#include "log.h" #ifdef NOFRENDO_DEBUG @@ -77,7 +70,7 @@ typedef enum #endif /* !NOFRENDO_DEBUG */ -#endif /* _TYPES_H_ */ +#endif /* _NOFTYPES_H_ */ /* ** $Log: noftypes.h,v $ diff --git a/components/nofrendo/osd.h b/components/nofrendo/osd.h index b30d0fc..06d2677 100644 --- a/components/nofrendo/osd.h +++ b/components/nofrendo/osd.h @@ -57,8 +57,8 @@ extern void osd_setsound(void (*playfunc)(void *buffer, int size)); #ifndef NSF_PLAYER -#include -#include +#include "noftypes.h" +#include "vid_drv.h" typedef struct vidinfo_s { diff --git a/components/nofrendo/pcx.c b/components/nofrendo/pcx.c index c723cba..7302e0a 100644 --- a/components/nofrendo/pcx.c +++ b/components/nofrendo/pcx.c @@ -25,9 +25,10 @@ #include #include -#include -#include -#include + +#include "noftypes.h" +#include "bitmap.h" +#include "pcx.h" /* Save a PCX snapshot from a given NES bitmap */ int pcx_write(char *filename, bitmap_t *bmp, rgb_t *pal) diff --git a/components/nofrendo/pcx.h b/components/nofrendo/pcx.h index 7dd9a99..8509090 100644 --- a/components/nofrendo/pcx.h +++ b/components/nofrendo/pcx.h @@ -26,8 +26,8 @@ #ifndef _PCX_H_ #define _PCX_H_ -#include -#include +#include "osd.h" +#include "bitmap.h" /* Got these out of ZSoft's document */ typedef struct pcxheader_s diff --git a/components/nofrendo/sndhrdw/fds_snd.c b/components/nofrendo/sndhrdw/fds_snd.c index 7c68574..c41327a 100644 --- a/components/nofrendo/sndhrdw/fds_snd.c +++ b/components/nofrendo/sndhrdw/fds_snd.c @@ -23,9 +23,9 @@ ** $Id: fds_snd.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "nes_apu.h" +#include "fds_snd.h" static int32 fds_incsize = 0; diff --git a/components/nofrendo/sndhrdw/fds_snd.h b/components/nofrendo/sndhrdw/fds_snd.h index eb2cf54..89250f5 100644 --- a/components/nofrendo/sndhrdw/fds_snd.h +++ b/components/nofrendo/sndhrdw/fds_snd.h @@ -26,7 +26,7 @@ #ifndef _FDS_SND_H_ #define _FDS_SND_H_ -#include +#include "nes_apu.h" extern apuext_t fds_ext; diff --git a/components/nofrendo/sndhrdw/mmc5_snd.c b/components/nofrendo/sndhrdw/mmc5_snd.c index 60e8c9b..63754b6 100644 --- a/components/nofrendo/sndhrdw/mmc5_snd.c +++ b/components/nofrendo/sndhrdw/mmc5_snd.c @@ -24,9 +24,10 @@ */ #include -#include + +#include "../noftypes.h" #include "mmc5_snd.h" -#include +#include "nes_apu.h" /* TODO: encapsulate apu/mmc5 rectangle */ diff --git a/components/nofrendo/sndhrdw/mmc5_snd.h b/components/nofrendo/sndhrdw/mmc5_snd.h index 996496f..938237e 100644 --- a/components/nofrendo/sndhrdw/mmc5_snd.h +++ b/components/nofrendo/sndhrdw/mmc5_snd.h @@ -26,7 +26,7 @@ #ifndef _MMC5_SND_H_ #define _MMC5_SND_H_ -#include +#include "nes_apu.h" extern apuext_t mmc5_ext; diff --git a/components/nofrendo/sndhrdw/nes_apu.c b/components/nofrendo/sndhrdw/nes_apu.c index 0fc0e8e..57d8606 100644 --- a/components/nofrendo/sndhrdw/nes_apu.c +++ b/components/nofrendo/sndhrdw/nes_apu.c @@ -24,11 +24,11 @@ */ #include -#include -#include -#include -#include "nes6502.h" - + +#include "../noftypes.h" +#include "../log.h" +#include "nes_apu.h" +#include "../cpu/nes6502.h" #define APU_OVERSAMPLE #define APU_VOLUME_DECAY(x) ((x) -= ((x) >> 7)) @@ -999,7 +999,7 @@ apu_t *apu_create(double base_freq, int sample_rate, int refresh_rate, int sampl apu_t *temp_apu; int channel; - temp_apu = malloc(sizeof(apu_t)); + temp_apu = NOFRENDO_MALLOC(sizeof(apu_t)); if (NULL == temp_apu) return NULL; @@ -1033,7 +1033,7 @@ void apu_destroy(apu_t **src_apu) { if ((*src_apu)->ext && NULL != (*src_apu)->ext->shutdown) (*src_apu)->ext->shutdown(); - free(*src_apu); + NOFRENDO_FREE(*src_apu); *src_apu = NULL; } } diff --git a/components/nofrendo/sndhrdw/vrcvisnd.c b/components/nofrendo/sndhrdw/vrcvisnd.c index d7108ef..e748061 100644 --- a/components/nofrendo/sndhrdw/vrcvisnd.c +++ b/components/nofrendo/sndhrdw/vrcvisnd.c @@ -23,9 +23,9 @@ ** $Id: vrcvisnd.c,v 1.2 2001/04/27 14:37:11 neil Exp $ */ -#include -#include -#include +#include "../noftypes.h" +#include "vrcvisnd.h" +#include "nes_apu.h" typedef struct vrcvirectangle_s { diff --git a/components/nofrendo/sndhrdw/vrcvisnd.h b/components/nofrendo/sndhrdw/vrcvisnd.h index f1c431a..136cbc2 100644 --- a/components/nofrendo/sndhrdw/vrcvisnd.h +++ b/components/nofrendo/sndhrdw/vrcvisnd.h @@ -26,7 +26,7 @@ #ifndef _VRCVISND_H_ #define _VRCVISND_H_ -#include +#include "nes_apu.h" extern apuext_t vrcvi_ext; diff --git a/components/nofrendo/vid_drv.c b/components/nofrendo/vid_drv.c index 4699019..ea3538b 100644 --- a/components/nofrendo/vid_drv.c +++ b/components/nofrendo/vid_drv.c @@ -24,12 +24,13 @@ */ #include -#include -#include -#include -#include -#include -#include + +#include "noftypes.h" +#include "log.h" +#include "bitmap.h" +#include "vid_drv.h" +#include "gui.h" +#include "osd.h" /* hardware surface */ static bitmap_t *screen = NULL; @@ -413,7 +414,7 @@ static int vid_findmode(int width, int height, viddriver_t *osd_driver) if (driver->free_write) driver->free_write(-1, NULL); - log_printf("video driver: %s at %dx%d\n", driver->name, + nofrendo_log_printf("video driver: %s at %dx%d\n", driver->name, screen->width, screen->height); return 0; @@ -424,11 +425,10 @@ int vid_init(int width, int height, viddriver_t *osd_driver) { if (vid_findmode(width, height, osd_driver)) { - log_printf("video initialization failed for %s at %dx%d\n", + nofrendo_log_printf("video initialization failed for %s at %dx%d\n", osd_driver->name, width, height); return -1; } - log_printf("vid_init done\n"); return 0; } diff --git a/components/nofrendo/vid_drv.h b/components/nofrendo/vid_drv.h index ef0187e..e97ed03 100644 --- a/components/nofrendo/vid_drv.h +++ b/components/nofrendo/vid_drv.h @@ -26,7 +26,7 @@ #ifndef _VID_DRV_H_ #define _VID_DRV_H_ -#include +#include "bitmap.h" typedef struct viddriver_s { diff --git a/flashrom.sh b/flashrom.sh deleted file mode 100755 index ad760e2..0000000 --- a/flashrom.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -. ${IDF_PATH}/add_path.sh -esptool.py --chip esp32 --port "/dev/ttyUSB1" --baud $((230400*4)) write_flash -fs 4MB 0x100000 "$1" diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild new file mode 100644 index 0000000..3bb0c4b --- /dev/null +++ b/main/Kconfig.projbuild @@ -0,0 +1,322 @@ +menu "Nofrendo ESP32-specific configuration" + +choice NOFRENDO_HARDWARE + prompt "Hardware to run on" + default HW_ODROID_GO + help + This emulator can run on various types of hardware. Select what you have here. + +config HW_WROVERKIT_V1 + bool "ESP_Wrover_Kit v1 (red PCB)" + +config HW_WROVERKIT_V2 + bool "ESP_Wrover_Kit v2 (black PCB)" + +config HW_M5STACK + bool "M5Stack" + +config HW_ODROID_GO + bool "Odroid-Go" + +config HW_CUSTOM + bool "Custom hardware" + +endchoice + +choice HW_LCD_TYPE_SEL + prompt "LCD type" + depends on HW_CUSTOM || HW_WROVERKIT_V2 || HW_ODROID_GO + +config HW_LCD_TYPE_ILI9341 + bool "ILI9341 LCD" + +config HW_LCD_TYPE_ST7789 + bool "ST7789V LCD" + +config HW_LCD_TYPE_ST7796 + bool "ST7796 LCD" + +endchoice + +config HW_LCD_IPS + bool "IPS type LCD (inverted color)" + default n if HW_WROVERKIT_V1 + default n if HW_WROVERKIT_V2 + default n if HW_M5STACK + default n if HW_ODROID_GO + default n if HW_CUSTOM + +config HW_WROVERKIT + bool + default y if HW_WROVERKIT_V1 + default y if HW_WROVERKIT_V2 + default n if HW_M5STACK + default n if HW_ODROID_GO + default n if HW_CUSTOM + + +config HW_LCD_TYPE + int + default 0 if HW_WROVERKIT_V1 + default 0 if HW_LCD_TYPE_ILI9341 + default 1 if HW_M5STACK + default 2 if HW_LCD_TYPE_ST7789 + default 3 if HW_LCD_TYPE_ST7796 + +config HW_LCD_MISO_GPIO_CUST + int "LCD MISO pin" + depends on HW_CUSTOM + range -1 35 + default -1 + +config HW_LCD_MOSI_GPIO_CUST + int "LCD MOSI pin" + depends on HW_CUSTOM + range 1 35 + default 23 + +config HW_LCD_CLK_GPIO_CUST + int "LCD CLK pin" + depends on HW_CUSTOM + range 1 35 + default 18 + +config HW_LCD_CS_GPIO_CUST + int "LCD CS pin" + depends on HW_CUSTOM + range 1 35 + default 5 + +config HW_LCD_DC_GPIO_CUST + int "LCD DC pin" + depends on HW_CUSTOM + range 1 32 + default 27 + +config HW_LCD_RST_GPIO_CUST + int "LCD RST pin" + depends on HW_CUSTOM + range 1 35 + default 33 + +config HW_LCD_BL_GPIO_CUST + int "LCD Backlight Enable pin" + depends on HW_CUSTOM + range -1 35 + default -1 + +config HW_INV_BL_CUST + bool "Invert backlight output" + default n + depends on HW_CUSTOM + +config HW_LCD_ROTATE_180_CUST + bool "Rotate LCD 180 degree" + default n + depends on HW_CUSTOM + +config HW_LCD_CLOCK_SPEED_CUST + int "LCD SPI clock speed" + depends on HW_CUSTOM + range 10 80 + default 40 + + +config HW_LCD_MISO_GPIO + int + default HW_LCD_MISO_GPIO_CUST if HW_CUSTOM + default 25 if HW_WROVERKIT + default 19 if HW_M5STACK + default 19 if HW_ODROID_GO + +config HW_LCD_MOSI_GPIO + int + default HW_LCD_MOSI_GPIO_CUST if HW_CUSTOM + default 23 if HW_WROVERKIT + default 23 if HW_M5STACK + default 23 if HW_ODROID_GO + +config HW_LCD_CLK_GPIO + int + default HW_LCD_CLK_GPIO_CUST if HW_CUSTOM + default 19 if HW_WROVERKIT + default 18 if HW_M5STACK + default 18 if HW_ODROID_GO + +config HW_LCD_CS_GPIO + int + default HW_LCD_CS_GPIO_CUST if HW_CUSTOM + default 22 if HW_WROVERKIT + default 14 if HW_M5STACK + default 5 if HW_ODROID_GO + +config HW_LCD_DC_GPIO + int + default HW_LCD_DC_GPIO_CUST if HW_CUSTOM + default 21 if HW_WROVERKIT + default 27 if HW_M5STACK + default 21 if HW_ODROID_GO + +config HW_LCD_RST_GPIO + int + default HW_LCD_RST_GPIO_CUST if HW_CUSTOM + default 18 if HW_WROVERKIT + default 33 if HW_M5STACK + default -1 if HW_ODROID_GO + +config HW_LCD_BL_GPIO + int + default HW_LCD_BL_GPIO_CUST if HW_CUSTOM + default 5 if HW_WROVERKIT + default 32 if HW_M5STACK + default 14 if HW_ODROID_GO + +config HW_INV_BL + bool + default HW_INBV_BL_CUST if HW_CUSTOM + default n if HW_WROVERKIT_V1 + default y if HW_WROVERKIT_V2 + default n if HW_M5STACK + default n if HW_ODROID_GO + +config HW_LCD_ROTATE_180 + bool + default HW_LCD_ROTATE_180_CUST if HW_CUSTOM + default n if HW_WROVERKIT_V1 + default n if HW_WROVERKIT_V2 + default n if HW_M5STACK + default n if HW_ODROID_GO + +config HW_LCD_CLOCK_SPEED + int + default HW_LCD_CLOCK_SPEED_CUST if HW_CUSTOM + default 27 if HW_WROVERKIT + default 40 if HW_M5STACK + default 40 if HW_ODROID_GO + +config SOUND_ENA + bool "Analog audio" + default y + help + ESP32 will output 0-3.3V analog audio signal on GPIO 25 and 26. + + +choice HW_CONTROLLER_SEL + prompt "Controller type" + default HW_CONTROLLER_GPIO if HW_ODROID_GO + default HW_CONTROLLER_I2C_GP + help + Connect buttons to the following GPIOs to control the NES; + Or connect a PSX/PS2 controller to the following GPIOs. + +config HW_CONTROLLER_GPIO + bool "GPIO controller" + +config HW_CONTROLLER_PSX + bool "PSX controller" + +config HW_CONTROLLER_I2C_GP + bool "I2C Gamepad" + +config HW_CONTROLLER_I2C_M5KB + bool "I2C M5Keyboard" + +endchoice + + +config HW_PSX_CLK + int "PSX controller CLK GPIO pin" + depends on HW_CONTROLLER_PSX + range 1 39 + default 14 + +config HW_PSX_DAT + int "PSX controller DATa GPIO pin" + depends on HW_CONTROLLER_PSX + range 1 39 + default 27 + +config HW_PSX_ATT + int "PSX controller ATTention GPIO pin" + depends on HW_CONTROLLER_PSX + range 1 39 + default 16 + +config HW_PSX_CMD + int "PSX controller CoMmanD GPIO pin" + depends on HW_CONTROLLER_PSX + range 1 39 + default 2 + + +config HW_GPIO_UP + int "GPIO controller UP button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 35 if HW_ODROID_GO + default 22 + +config HW_GPIO_DOWN + int "GPIO controller DOWN button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 35 if HW_ODROID_GO + default 21 + +config HW_GPIO_LEFT + int "GPIO controller LEFT button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 34 if HW_ODROID_GO + default 19 + +config HW_GPIO_RIGHT + int "GPIO controller RIGHT button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 34 if HW_ODROID_GO + default 4 + +config HW_GPIO_MENU + int "GPIO controller MENU button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 13 if HW_ODROID_GO + default -1 + +config HW_GPIO_AUDIO + int "GPIO controller AUDIO button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 0 if HW_ODROID_GO + default -1 + +config HW_GPIO_SELECT + int "GPIO controller SELECT button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 27 if HW_ODROID_GO + default 0 + +config HW_GPIO_START + int "GPIO controller START button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 39 if HW_ODROID_GO + default 2 + +config HW_GPIO_A + int "GPIO controller A button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 32 if HW_ODROID_GO + default 12 + +config HW_GPIO_B + int "GPIO controller B button" + depends on HW_CONTROLLER_GPIO + range 0 39 + default 33 if HW_ODROID_GO + default 32 + +endmenu \ No newline at end of file diff --git a/main/config.h b/main/config.h new file mode 100644 index 0000000..bd6845f --- /dev/null +++ b/main/config.h @@ -0,0 +1,7 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +#define FSROOT "/spiffs" +#define ROM_FILE FSROOT "/rom.nes" + +#endif /* _CONFIG_H_ */ diff --git a/main/controller.c b/main/controller.c new file mode 100644 index 0000000..c1548d5 --- /dev/null +++ b/main/controller.c @@ -0,0 +1,462 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "noftypes.h" +#include "sdkconfig.h" + +#if defined(CONFIG_HW_CONTROLLER_GPIO) + +// Set buttons with pins +#define UP CONFIG_HW_GPIO_UP +#define DOWN CONFIG_HW_GPIO_DOWN +#define LEFT CONFIG_HW_GPIO_LEFT +#define RIGHT CONFIG_HW_GPIO_RIGHT +#define MENU CONFIG_HW_GPIO_MENU +#define AUDIO CONFIG_HW_GPIO_AUDIO +#define SELECT CONFIG_HW_GPIO_SELECT +#define START CONFIG_HW_GPIO_START +#define A CONFIG_HW_GPIO_A +#define B CONFIG_HW_GPIO_B + +#if (LEFT == RIGHT) && (UP == DOWN) +#define ANALOG_JOYSTICK + +#if (LEFT == ADC1_CHANNEL_0_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_0 +#endif +#if (LEFT == ADC1_CHANNEL_1_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_1 +#endif +#if (LEFT == ADC1_CHANNEL_2_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_2 +#endif +#if (LEFT == ADC1_CHANNEL_3_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_3 +#endif +#if (LEFT == ADC1_CHANNEL_4_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_4 +#endif +#if (LEFT == ADC1_CHANNEL_5_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_5 +#endif +#if (LEFT == ADC1_CHANNEL_6_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_6 +#endif +#if (LEFT == ADC1_CHANNEL_7_GPIO_NUM) +#define X_CHANNEL ADC1_CHANNEL_7 +#endif + +#if (UP == ADC1_CHANNEL_0_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_0 +#endif +#if (UP == ADC1_CHANNEL_1_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_1 +#endif +#if (UP == ADC1_CHANNEL_2_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_2 +#endif +#if (UP == ADC1_CHANNEL_3_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_3 +#endif +#if (UP == ADC1_CHANNEL_4_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_4 +#endif +#if (UP == ADC1_CHANNEL_5_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_5 +#endif +#if (UP == ADC1_CHANNEL_6_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_6 +#endif +#if (UP == ADC1_CHANNEL_7_GPIO_NUM) +#define Y_CHANNEL ADC1_CHANNEL_7 +#endif + +#define DEFAULT_VREF 1100 +static esp_adc_cal_characteristics_t characteristics; +#endif + +void controller_init() +{ + //Configure button + gpio_config_t btn_config; + btn_config.intr_type = GPIO_INTR_ANYEDGE; //Enable interrupt on both rising and falling edges + btn_config.mode = GPIO_MODE_INPUT; //Set as Input +#ifdef ANALOG_JOYSTICK + adc1_config_width(ADC_WIDTH_12Bit); + adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_11db); + esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_11db, ADC_WIDTH_BIT_12, DEFAULT_VREF, &characteristics); + + btn_config.pin_bit_mask = (uint64_t) //Bitmask + (((uint64_t)(((uint64_t)1) << MENU)) | ((uint64_t)(((uint64_t)1) << AUDIO)) | ((uint64_t)(((uint64_t)1) << SELECT)) | ((uint64_t)(((uint64_t)1) << START)) | ((uint64_t)(((uint64_t)1) << A)) | ((uint64_t)(((uint64_t)1) << B))); + btn_config.pull_up_en = GPIO_PULLUP_ENABLE; //Disable pullup + btn_config.pull_down_en = GPIO_PULLDOWN_DISABLE; //Enable pulldown + gpio_config(&btn_config); +#else + btn_config.pin_bit_mask = (uint64_t) //Bitmask + (((uint64_t)(((uint64_t)1) << UP)) | ((uint64_t)(((uint64_t)1) << DOWN)) | ((uint64_t)(((uint64_t)1) << LEFT)) | ((uint64_t)(((uint64_t)1) << RIGHT)) | ((uint64_t)(((uint64_t)1) << MENU)) | ((uint64_t)(((uint64_t)1) << AUDIO)) | ((uint64_t)(((uint64_t)1) << SELECT)) | ((uint64_t)(((uint64_t)1) << START)) | ((uint64_t)(((uint64_t)1) << A)) | ((uint64_t)(((uint64_t)1) << B))); + btn_config.pull_up_en = GPIO_PULLUP_ENABLE; //Disable pullup + btn_config.pull_down_en = GPIO_PULLDOWN_DISABLE; //Enable pulldown + gpio_config(&btn_config); +#endif +} + +int controller_read_input() +{ + int u, d, l, r, s, t, a, b, m, i; +#ifdef ANALOG_JOYSTICK + int joyX = adc1_get_raw(X_CHANNEL); + int joyY = adc1_get_raw(Y_CHANNEL); + if (joyX > 2048 + 1024) + { + l = 0; + r = 1; + } + else if (joyX > 1024) + { + l = 1; + r = 0; + } + else + { + l = 1; + r = 1; + } + if (joyY > 2048 + 1024) + { + u = 0; + d = 1; + } + else if (joyY > 1024) + { + u = 1; + d = 0; + } + else + { + u = 1; + d = 1; + } +#else + l = gpio_get_level(LEFT); + r = gpio_get_level(RIGHT); + u = gpio_get_level(UP); + d = gpio_get_level(DOWN); +#endif + + s = gpio_get_level(SELECT); + t = gpio_get_level(START); + a = gpio_get_level(A); + b = gpio_get_level(B); + m = gpio_get_level(MENU); + i = gpio_get_level(AUDIO); + + return 0xFFFF ^ ((!u << 4) | (!d << 6) | (!l << 7) | (!r << 5) | (!s << 0) | (!t << 3) | (!a << 13) | (!b << 14) | (!m << 1) | (!i << 2)); +} + +#elif defined(CONFIG_HW_CONTROLLER_PSX) + +#define PSX_CLK CONFIG_HW_PSX_CLK +#define PSX_DAT CONFIG_HW_PSX_DAT +#define PSX_ATT CONFIG_HW_PSX_ATT +#define PSX_CMD CONFIG_HW_PSX_CMD + +#define DELAY() asm("nop; nop; nop; nop;nop; nop; nop; nop;nop; nop; nop; nop;nop; nop; nop; nop;") + +/* Sends and receives a byte from/to the PSX controller using SPI */ +static int psxSendRecv(int send) +{ + int x; + int ret = 0; + volatile int delay; + +#if 0 + while(1) { + GPIO.out_w1ts=(1<>= 1; + send >>= 1; + if (GPIO.in & (1 << PSX_DAT)) + ret |= 128; + } + return ret; +} + +static void psxDone() +{ + DELAY(); + GPIO_REG_WRITE(GPIO_OUT_W1TS_REG, (1 << PSX_ATT)); +} + +void controller_init() +{ + volatile int delay; + int t; + gpio_config_t gpioconf[2] = { + {.pin_bit_mask = (1 << PSX_CLK) | (1 << PSX_CMD) | (1 << PSX_ATT), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_PIN_INTR_DISABLE}, + {.pin_bit_mask = (1 << PSX_DAT), + .mode = GPIO_MODE_INPUT, + .pull_up_en = GPIO_PULLUP_ENABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_PIN_INTR_DISABLE}}; + gpio_config(&gpioconf[0]); + gpio_config(&gpioconf[1]); + + //Send a few dummy bytes to clean the pipes. + psxSendRecv(0); + psxDone(); + for (delay = 0; delay < 500; delay++) + DELAY(); + psxSendRecv(0); + psxDone(); + for (delay = 0; delay < 500; delay++) + DELAY(); + //Try and detect the type of controller, so we can give the user some diagnostics. + psxSendRecv(0x01); + t = psxSendRecv(0x00); + psxDone(); + if (t == 0 || t == 0xff) + { + nofrendo_log_printf("No PSX/PS2 controller detected (0x%X). You will not be able to control the game.\n", t); + } + else + { + nofrendo_log_printf("PSX controller type 0x%X\n", t); + } +} + +int controller_read_input() +{ + int b1, b2; + + psxSendRecv(0x01); //wake up + psxSendRecv(0x42); //get data + psxSendRecv(0xff); //should return 0x5a + b1 = psxSendRecv(0xff); //buttons byte 1 + b2 = psxSendRecv(0xff); //buttons byte 2 + psxDone(); + return (b2 << 8) | b1; +} + +#elif defined(CONFIG_HW_CONTROLLER_I2C_GP) + +#define I2C_GAMEPAD_SCL_IO 22 /*!< gpio number for I2C master clock */ +#define I2C_GAMEPAD_SDA_IO 21 /*!< gpio number for I2C master data */ +#define I2C_GAMEPAD_NUM I2C_NUM_0 /*!< I2C port number for master dev */ +#define I2C_GAMEPAD_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ +#define I2C_GAMEPAD_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ +#define I2C_GAMEPAD_FREQ_HZ 50000 /*!< I2C master clock frequency */ +#define I2C_GAMEPAD_ADDR 0x08 + +#define READ_BIT I2C_MASTER_READ /*!< I2C master read */ +#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/ +#define NACK_VAL 0x1 /*!< I2C nack value */ + +void controller_init() +{ + int i2c_master_port = I2C_GAMEPAD_NUM; + i2c_config_t conf; + conf.mode = I2C_MODE_MASTER; + conf.sda_io_num = I2C_GAMEPAD_SDA_IO; + conf.sda_pullup_en = GPIO_PULLUP_ENABLE; + conf.scl_io_num = I2C_GAMEPAD_SCL_IO; + conf.scl_pullup_en = GPIO_PULLUP_ENABLE; + conf.master.clk_speed = I2C_GAMEPAD_FREQ_HZ; + i2c_param_config(i2c_master_port, &conf); + i2c_driver_install(i2c_master_port, conf.mode, + I2C_GAMEPAD_RX_BUF_DISABLE, + I2C_GAMEPAD_TX_BUF_DISABLE, 0); +} + +/** + * @brief test code to write esp-i2c-slave + * + * 1. set mode + * _________________________________________________________________ + * | start | slave_addr + wr_bit + ack | write 1 byte + ack | stop | + * --------|---------------------------|---------------------|------| + * 2. wait more than 24 ms + * 3. read data + * ______________________________________________________________________________________ + * | start | slave_addr + rd_bit + ack | read 1 byte + ack | read 1 byte + nack | stop | + * --------|---------------------------|--------------------|--------------------|------| + */ +int controller_read_input() +{ + uint8_t data; + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (I2C_GAMEPAD_ADDR << 1) | READ_BIT, ACK_CHECK_EN); + i2c_master_read_byte(cmd, &data, NACK_VAL); + i2c_master_stop(cmd); + esp_err_t ret = i2c_master_cmd_begin(I2C_GAMEPAD_NUM, cmd, 1000 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + if (ret == 0) + { + nofrendo_log_printf("I2C read %d, return %d\n", data, ret); + + return 0xFFFF ^ ((((0x01 & data) > 0) << 4) | (((0x02 & data) > 0) << 6) | (((0x04 & data) > 0) << 7) | (((0x08 & data) > 0) << 5) | (((0x10 & data) > 0) << 0) | (((0x20 & data) > 0) << 3) | (((0x40 & data) > 0) << 13) | (((0x80 & data) > 0) << 14)); + } + else + { + return 0xFFFF; + } +} + +#elif defined(CONFIG_HW_CONTROLLER_I2C_M5KB) + +#define I2C_KEYBOARD_SCL_IO 22 /*!< gpio number for I2C master clock */ +#define I2C_KEYBOARD_SDA_IO 21 /*!< gpio number for I2C master data */ +#define I2C_KEYBOARD_NUM I2C_NUM_0 /*!< I2C port number for master dev */ +#define I2C_KEYBOARD_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ +#define I2C_KEYBOARD_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */ +#define I2C_KEYBOARD_FREQ_HZ 100000 /*!< I2C master clock frequency */ +#define I2C_KEYBOARD_ADDR 0x5f + +#define READ_BIT I2C_MASTER_READ /*!< I2C master read */ +#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/ +#define NACK_VAL 0x1 /*!< I2C nack value */ + +void controller_init() +{ + int i2c_master_port = I2C_KEYBOARD_NUM; + i2c_config_t conf; + conf.mode = I2C_MODE_MASTER; + conf.sda_io_num = I2C_KEYBOARD_SDA_IO; + conf.sda_pullup_en = GPIO_PULLUP_ENABLE; + conf.scl_io_num = I2C_KEYBOARD_SCL_IO; + conf.scl_pullup_en = GPIO_PULLUP_ENABLE; + conf.master.clk_speed = I2C_KEYBOARD_FREQ_HZ; + i2c_param_config(i2c_master_port, &conf); + i2c_driver_install(i2c_master_port, conf.mode, + I2C_KEYBOARD_RX_BUF_DISABLE, + I2C_KEYBOARD_TX_BUF_DISABLE, 0); +} + +/** + * @brief test code to write esp-i2c-slave + * + * 1. set mode + * _________________________________________________________________ + * | start | slave_addr + wr_bit + ack | write 1 byte + ack | stop | + * --------|---------------------------|---------------------|------| + * 2. wait more than 24 ms + * 3. read data + * ______________________________________________________________________________________ + * | start | slave_addr + rd_bit + ack | read 1 byte + ack | read 1 byte + nack | stop | + * --------|---------------------------|--------------------|--------------------|------| + */ +int controller_read_input() +{ + uint8_t data; + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (I2C_KEYBOARD_ADDR << 1) | READ_BIT, ACK_CHECK_EN); + i2c_master_read_byte(cmd, &data, NACK_VAL); + i2c_master_stop(cmd); + esp_err_t ret = i2c_master_cmd_begin(I2C_KEYBOARD_NUM, cmd, 1000 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + if ((ret == 0) && (data > 0)) + { + nofrendo_log_printf("I2C read %d, return %d\n", data, ret); + + switch (data) + { + case 181: // up + return 0xFFFF ^ (1 << 4); + break; + case 182: // down + return 0xFFFF ^ (1 << 6); + break; + case 180: // left + return 0xFFFF ^ (1 << 7); + break; + case 183: // right + return 0xFFFF ^ (1 << 5); + break; + case 32: // space -> select + return 0xFFFF ^ (1 << 0); + break; + case 13: // enter -> start + return 0xFFFF ^ (1 << 3); + break; + case 108: // L -> A + return 0xFFFF ^ (1 << 13); + break; + case 107: // K -> B + return 0xFFFF ^ (1 << 14); + break; + } + } + + return 0xFFFF; +} + +#else + +void controller_gpio_init() +{ + nofrendo_log_printf("GPIO controller disabled in menuconfig; no input enabled.\n"); +} + +int controller_gpio_read_input() +{ + return 0xFFFF; +} + +#endif \ No newline at end of file diff --git a/main/controller.h b/main/controller.h new file mode 100644 index 0000000..2926c3a --- /dev/null +++ b/main/controller.h @@ -0,0 +1,7 @@ +#ifndef _CONTROLLER_H_ +#define _CONTROLLER_H_ + +int controller_read_input(); +void controller_init(); + +#endif // _CONTROLLER_H_ diff --git a/main/main.c b/main/main.c index a6f1019..daf15ad 100644 --- a/main/main.c +++ b/main/main.c @@ -1,41 +1,80 @@ -#include "freertos/FreeRTOS.h" -#include "esp_wifi.h" -#include "esp_system.h" -#include "esp_event.h" -#include "esp_event_loop.h" -#include "nvs_flash.h" -#include "driver/gpio.h" +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nofconfig.h" #include "nofrendo.h" -#include "esp_partition.h" - - - -char *osd_getromdata() { - char* romdata; - const esp_partition_t* part; - spi_flash_mmap_handle_t hrom; - esp_err_t err; - nvs_flash_init(); - part=esp_partition_find_first(0x40, 1, NULL); - if (part==0) printf("Couldn't find rom part!\n"); - err=esp_partition_mmap(part, 0, 3*1024*1024, SPI_FLASH_MMAP_DATA, (const void**)&romdata, &hrom); - if (err!=ESP_OK) printf("Couldn't map rom part!\n"); - printf("Initialized. ROM@%p\n", romdata); - return (char*)romdata; -} +#include "config.h" +void *mem_alloc(int size, bool fast_mem) +{ + if (fast_mem) + { + return heap_caps_malloc(size, MALLOC_CAP_8BIT); + } + else + { + return heap_caps_malloc_prefer(size, MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT); + } +} -esp_err_t event_handler(void *ctx, system_event_t *event) +void spiffs_init(void) { - return ESP_OK; + esp_vfs_spiffs_conf_t conf = { + .base_path = FSROOT, + .partition_label = NULL, + .max_files = 5, + .format_if_mount_failed = true}; + + // Use settings defined above to initialize and mount SPIFFS filesystem. + // Note: esp_vfs_spiffs_register is an all-in-one convenience function. + esp_err_t ret = esp_vfs_spiffs_register(&conf); + + if (ret != ESP_OK) + { + if (ret == ESP_FAIL) + { + nofrendo_log_printf("Failed to mount or format filesystem"); + } + else if (ret == ESP_ERR_NOT_FOUND) + { + nofrendo_log_printf("Failed to find SPIFFS partition"); + } + else + { + nofrendo_log_printf("Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); + } + return; + } + + size_t total = 0, used = 0; + ret = esp_spiffs_info(NULL, &total, &used); + if (ret != ESP_OK) + { + nofrendo_log_printf("Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret)); + } + else + { + nofrendo_log_printf("Partition size: total: %d, used: %d\n", total, used); + } } int app_main(void) { - printf("NoFrendo start!\n"); - nofrendo_main(0, NULL); - printf("NoFrendo died? WtF?\n"); - asm("break.n 1"); + spiffs_init(); + + nofrendo_log_printf("NoFrendo start!\n"); + nofrendo_main(0, NULL); + nofrendo_log_printf("NoFrendo died? Oh no!\n"); + asm("break.n 1"); return 0; } - diff --git a/components/nofrendo-esp32/osd.c b/main/osd.c similarity index 69% rename from components/nofrendo-esp32/osd.c rename to main/osd.c index fc0c1ca..6c50d92 100644 --- a/components/nofrendo-esp32/osd.c +++ b/main/osd.c @@ -6,36 +6,39 @@ ** ** $Id: osd.c,v 1.2 2001/04/27 14:37:11 neil Exp $ ** + */ -#include -#include -#include -#include #include #include #include + #include #include #include +#include +#include +#include +#include #include - -#include -#include -#include -#include -#include -#include +#include "log.h" +#include "nofrendo.h" +#include "noftypes.h" +#include "osd.h" +#include "version.h" + +#include "nofconfig.h" +#include "config.h" -char configfilename[]="na"; +char configfilename[] = "na"; /* This is os-specific part of main() */ int osd_main(int argc, char *argv[]) { config.filename = configfilename; - return main_loop("rom", system_autodetect); + return main_loop(ROM_FILE, system_autodetect); } /* File system interface */ @@ -47,6 +50,12 @@ void osd_fullname(char *fullname, const char *shortname) /* This gives filenames for storage of saves */ char *osd_newextension(char *string, char *ext) { + // dirty: assume extension is 3 characters + size_t l = strlen(string); + string[l - 3] = ext[1]; + string[l - 2] = ext[2]; + string[l - 1] = ext[3]; + return string; } diff --git a/main/spi_lcd.c b/main/spi_lcd.c new file mode 100644 index 0000000..4cd3d3d --- /dev/null +++ b/main/spi_lcd.c @@ -0,0 +1,409 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "spi_lcd.h" + +#include "sdkconfig.h" + +#define PIN_NUM_MISO CONFIG_HW_LCD_MISO_GPIO +#define PIN_NUM_MOSI CONFIG_HW_LCD_MOSI_GPIO +#define PIN_NUM_CLK CONFIG_HW_LCD_CLK_GPIO +#define PIN_NUM_CS CONFIG_HW_LCD_CS_GPIO +#define PIN_NUM_DC CONFIG_HW_LCD_DC_GPIO +#define PIN_NUM_RST CONFIG_HW_LCD_RST_GPIO +#define PIN_NUM_BL CONFIG_HW_LCD_BL_GPIO +#define SPI_SPD CONFIG_HW_LCD_CLOCK_SPEED + +#define SPI_NUM 0x3 + +#define LCD_TYPE_ILI9341 0 +#define LCD_TYPE_M5STACK 1 +#define LCD_TYPE_ST7789 2 +#define LCD_TYPE_ST7796 3 + +#define LCD_SEL_CMD() gpio_set_level(PIN_NUM_DC, 0) // Low to send command +#define LCD_SEL_DATA() gpio_set_level(PIN_NUM_DC, 1) // High to send data +#if (PIN_NUM_RST > -1) +#define LCD_RST_SET() gpio_set_level(PIN_NUM_RST, 1) +#define LCD_RST_CLR() gpio_set_level(PIN_NUM_RST, 0) +#else +#define LCD_RST_SET() +#define LCD_RST_CLR() +#endif + +/* + The LCD needs a bunch of command/argument values to be initialized. They are stored in this struct. +*/ +typedef struct +{ + uint8_t cmd; + uint8_t data[16]; + uint8_t databytes; //No of data in data; bit 7 = delay after set; 0xFF = end of cmds. +} lcd_init_cmd_t; + +spi_device_handle_t spi; + +DRAM_ATTR static const lcd_init_cmd_t lcd_init_cmds[] = { +#if (CONFIG_HW_LCD_TYPE == LCD_TYPE_ILI9341) + {0xCF, {0x00, 0x83, 0X30}, 3}, + {0xED, {0x64, 0x03, 0X12, 0X81}, 4}, + {0xE8, {0x85, 0x01, 0x79}, 3}, + {0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5}, + {0xF7, {0x20}, 1}, + {0xEA, {0x00, 0x00}, 2}, + {0xC0, {0x26}, 1}, // Power control, VRH[5:0] + {0xC1, {0x11}, 1}, // Power control, SAP[2:0];BT[3:0] + {0xC5, {0x35, 0x3E}, 2}, // VCM control + {0xC7, {0xBE}, 1}, // VCM control2, was B1h +#ifdef CONFIG_HW_LCD_ROTATE_180 + {0x36, {(1 << 5) | (1 << 3)}, 1}, // MV | BGR +#else + {0x36, {(1 << 7) | (1 << 6) | (1 << 5) | (1 << 3)}, 1}, // MY | MX | MV | BGR +#endif + {0x3A, {0x55}, 1}, + {0xB1, {0x00, 0x1B}, 2}, + {0xF2, {0x08}, 1}, // 3Gamma Function Disable + {0x26, {0x01}, 1}, // Gamma curve selected + {0xE0, {0x1F, 0x1A, 0x18, 0x0A, 0x0F, 0x06, 0x45, 0X87, 0x32, 0x0A, 0x07, 0x02, 0x07, 0x05, 0x00}, 15}, // Set Gamma + {0XE1, {0x00, 0x25, 0x27, 0x05, 0x10, 0x09, 0x3A, 0x78, 0x4D, 0x05, 0x18, 0x0D, 0x38, 0x3A, 0x1F}, 15}, // Set Gamma + {0x2A, {0x00, 0x00, 0x00, 0xEF}, 4}, + {0x2B, {0x00, 0x00, 0x01, 0x3f}, 4}, + {0x2C, {0}, 0}, + {0xB7, {0x07}, 1}, + {0xB6, {0x0A, 0x82, 0x27, 0x00}, 4}, // Display Function Control, 8 82 27 +#endif + +#if (CONFIG_HW_LCD_TYPE == LCD_TYPE_M5STACK) + {0xEF, {0x03, 0x80, 0x02}, 3}, + {0xCF, {0x00, 0XC1, 0X30}, 3}, + {0xED, {0x64, 0x03, 0X12, 0X81}, 4}, + {0xE8, {0x85, 0x00, 0x78}, 3}, + {0xCB, {0x39, 0x2C, 0x00, 0x34, 0x02}, 5}, + {0xF7, {0x20}, 1}, + {0xEA, {0x00, 0x00}, 2}, + {0xC0, {0x23}, 1}, + {0xC1, {0x10}, 1}, + {0xC5, {0x3e, 0x28}, 2}, + {0xC7, {0x86}, 1}, +#ifdef CONFIG_HW_LCD_ROTATE_180 + {0x36, {(1 << 7) | (1 << 6) | (1 << 3)}, 1}, // MY | MX | BGR +#else + {0x36, {(1 << 3)}, 1}, // BGR +#endif + {0x3A, {0x55}, 1}, + {0xB1, {0x00, 0x1B}, 2}, + {0xB6, {0x08, 0x82, 0x27}, 3}, + {0xF2, {0x00}, 1}, + {0x26, {0x01}, 1}, + {0xE0, {0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00}, 15}, + {0XE1, {0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F}, 15}, +#endif + +#if (CONFIG_HW_LCD_TYPE == LCD_TYPE_ST7789) +#ifdef CONFIG_HW_LCD_ROTATE_180 + {0x36, {(1 << 6) | (1 << 5)}, 1}, // MX | MV | RGB +#else + {0x36, {(1 << 7) | (1 << 5)}, 1}, // MY | MV | RGB +#endif + {0x3A, {0x55}, 1}, + {0xB2, {0x0c, 0x0c, 0x00, 0x33, 0x33}, 5}, + {0xB7, {0x35}, 1}, + {0xBB, {0x2B}, 1}, + {0xC0, {0x2C}, 1}, + {0xC2, {0x01, 0xFF}, 2}, + {0xC3, {0x11}, 1}, + {0xC4, {0x20}, 1}, + {0xC6, {0x0f}, 1}, + {0xD0, {0xA4, 0xA1}, 2}, + {0xE0, {0xD0, 0x00, 0x05, 0x0E, 0x15, 0x0D, 0x37, 0x43, 0x47, 0x09, 0x15, 0x12, 0x16, 0x19}, 14}, + {0xE1, {0xD0, 0x00, 0x05, 0x0D, 0x0C, 0x06, 0x2D, 0x44, 0x40, 0x0E, 0x1C, 0x18, 0x16, 0x19}, 14}, +#endif + +#if (CONFIG_HW_LCD_TYPE == LCD_TYPE_ST7796) +#ifdef CONFIG_HW_LCD_ROTATE_180 + {0x36, {(1 << 5) | (1 << 3)}, 1}, // MV | BGR +#else + {0x36, {(1 << 7) | (1 << 6) | (1 << 5) | (1 << 3)}, 1}, // MY | MX | MV | BGR +#endif + {0xF0, {0xC3}, 1}, + {0xF0, {0x96}, 1}, + {0x3A, {0x05}, 1}, + {0xB0, {0x80}, 1}, + {0xB6, {0x00, 0x02}, 2}, + {0xB5, {0x02, 0x03, 0x00, 0x04}, 4}, + {0xB1, {0x80, 0x10}, 2}, + {0xB4, {0x00}, 1}, + {0xB7, {0xC6}, 1}, + {0xC5, {0x24}, 1}, + {0xE4, {0x31}, 1}, + {0xE8, {0x40, 0x8A, 0x00, 0x00, 0x29, 0x19, 0xA5, 0x33}, 8}, + {0xC2, {0}, 0}, + {0xA7, {0}, 0}, + + {0xE0, {0xF0, 0x09, 0x13, 0x12, 0x12, 0x2B, 0x3C, 0x44, 0x4B, 0x1B, 0x18, 0x17, 0x1D, 0x21}, 14}, + + {0XE1, {0xF0, 0x09, 0x13, 0x0C, 0x0D, 0x27, 0x3B, 0x44, 0x4D, 0x0B, 0x17, 0x17, 0x1D, 0x21}, 14}, + + {0xF0, {0xC3}, 1}, + {0xF0, {0x69}, 1}, + {0X13, {0}, 0}, +#endif + +#if (CONFIG_HW_LCD_IPS == 1) + {0x21, {0}, 0x80}, +#endif + {0x11, {0}, 0x80}, + {0x29, {0}, 0x80}, + {0, {0}, 0xff}, +}; + +//Send a command to the LCD. Uses spi_device_transmit, which waits until the transfer is complete. +void lcd_cmd(spi_device_handle_t spi, const uint8_t cmd) +{ + esp_err_t ret; + spi_transaction_t t; + memset(&t, 0, sizeof(t)); //Zero out the transaction + t.length = 8; //Command is 8 bits + t.tx_buffer = &cmd; //The data is the cmd itself + t.user = (void *)0; //D/C needs to be set to 0 + ret = spi_device_transmit(spi, &t); //Transmit! + assert(ret == ESP_OK); //Should have had no issues. +} + +//Send data to the LCD. Uses spi_device_transmit, which waits until the transfer is complete. +void lcd_data(spi_device_handle_t spi, const uint8_t *data, int len) +{ + esp_err_t ret; + spi_transaction_t t; + if (len == 0) + return; //no need to send anything + memset(&t, 0, sizeof(t)); //Zero out the transaction + t.length = len * 8; //Len is in bytes, transaction length is in bits. + t.tx_buffer = data; //Data + t.user = (void *)1; //D/C needs to be set to 1 + ret = spi_device_transmit(spi, &t); //Transmit! + assert(ret == ESP_OK); //Should have had no issues. +} +//------ + +//Initialize the display +void lcd_gpio_init() +{ + //Initialize non-SPI GPIOs + gpio_set_direction(PIN_NUM_DC, GPIO_MODE_OUTPUT); +#if (PIN_NUM_RST > -1) + gpio_set_direction(PIN_NUM_RST, GPIO_MODE_OUTPUT); +#endif + + //Reset the display + LCD_RST_CLR(); + vTaskDelay(100 / portTICK_RATE_MS); + LCD_RST_SET(); + vTaskDelay(100 / portTICK_RATE_MS); +} + +void lcd_init_seq(spi_device_handle_t spi) +{ + int cmd = 0; + + //Send all the commands + while (lcd_init_cmds[cmd].databytes != 0xff) + { + lcd_cmd(spi, lcd_init_cmds[cmd].cmd); + lcd_data(spi, lcd_init_cmds[cmd].data, lcd_init_cmds[cmd].databytes & 0x1F); + if (lcd_init_cmds[cmd].databytes & 0x80) + { + vTaskDelay(100 / portTICK_RATE_MS); + } + cmd++; + } +} + +void lcd_spi_pre_transfer_callback(spi_transaction_t *t) +{ + int dc = (int)t->user; + gpio_set_level(PIN_NUM_DC, dc); +} + +void lcd_setBrightness(int duty) +{ +#if (PIN_NUM_BL > -1) +#if (CONFIG_HW_INV_BL == 1) + duty = 1023 - duty; +#endif +#define LEDC_HS_TIMER LEDC_TIMER_0 +#define LEDC_HS_MODE LEDC_HIGH_SPEED_MODE +#define LEDC_HS_CHANNEL LEDC_CHANNEL_0 +#define LEDC_TEST_DUTY (10) + + ledc_timer_config_t ledc_timer = { + .speed_mode = LEDC_HS_MODE, // timer mode + .duty_resolution = LEDC_TIMER_10_BIT, // resolution of PWM duty + .timer_num = LEDC_HS_TIMER, // timer index + .freq_hz = 5000, // frequency of PWM signal + .clk_cfg = LEDC_USE_APB_CLK, + }; + // Set configuration of timer0 for high speed channels + ledc_timer_config(&ledc_timer); + + ledc_channel_config_t ledc_channel = + { + .channel = LEDC_HS_CHANNEL, + .duty = duty, + .gpio_num = PIN_NUM_BL, + .speed_mode = LEDC_HS_MODE, + .timer_sel = LEDC_HS_TIMER}; + ledc_channel_config(&ledc_channel); + // ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, duty); + // ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel); +#endif +} + +#define U16x2toU32(m, l) ((((uint32_t)(l >> 8 | (l & 0xFF) << 8)) << 16) | (m >> 8 | (m & 0xFF) << 8)) + +extern uint16_t myPalette[]; + +void lcd_write_frame(const uint16_t xs, const uint16_t ys, const uint16_t width, const uint16_t height, const uint8_t *data[]) +{ + int x, y; + int i; + uint16_t x1, y1; + uint32_t xv, yv, dc; + uint32_t temp[16]; + dc = (1 << PIN_NUM_DC); + + for (y = 0; y < height; y++) + { + //start line + x1 = xs + (width - 1); + y1 = ys + y + (height - 1); + xv = U16x2toU32(xs, x1); + yv = U16x2toU32((ys + y), y1); + + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + GPIO.out_w1tc = dc; + SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 7, SPI_USR_MOSI_DBITLEN_S); + WRITE_PERI_REG((SPI_W0_REG(SPI_NUM)), 0x2A); + SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + GPIO.out_w1ts = dc; + SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 31, SPI_USR_MOSI_DBITLEN_S); + WRITE_PERI_REG((SPI_W0_REG(SPI_NUM)), xv); + SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + GPIO.out_w1tc = dc; + SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 7, SPI_USR_MOSI_DBITLEN_S); + WRITE_PERI_REG((SPI_W0_REG(SPI_NUM)), 0x2B); + SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + GPIO.out_w1ts = dc; + SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 31, SPI_USR_MOSI_DBITLEN_S); + WRITE_PERI_REG((SPI_W0_REG(SPI_NUM)), yv); + SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + GPIO.out_w1tc = dc; + SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 7, SPI_USR_MOSI_DBITLEN_S); + WRITE_PERI_REG((SPI_W0_REG(SPI_NUM)), 0x2C); + SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + + x = 0; + GPIO.out_w1ts = dc; + SET_PERI_REG_BITS(SPI_MOSI_DLEN_REG(SPI_NUM), SPI_USR_MOSI_DBITLEN, 511, SPI_USR_MOSI_DBITLEN_S); + while (x < width) + { + for (i = 0; i < 16; i++) + { + if (data == NULL) + { + temp[i] = 0; + x += 2; + continue; + } + x1 = myPalette[(unsigned char)(data[y][x])]; + x++; + y1 = myPalette[(unsigned char)(data[y][x])]; + x++; + temp[i] = U16x2toU32(x1, y1); + } + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; + for (i = 0; i < 16; i++) + { + WRITE_PERI_REG((SPI_W0_REG(SPI_NUM) + (i << 2)), temp[i]); + } + SET_PERI_REG_MASK(SPI_CMD_REG(SPI_NUM), SPI_USR); + } + } + while (READ_PERI_REG(SPI_CMD_REG(SPI_NUM)) & SPI_USR) + ; +} + +void lcd_init() +{ + esp_err_t ret; + spi_device_handle_t spi; + spi_bus_config_t buscfg = { + .miso_io_num = PIN_NUM_MISO, + .mosi_io_num = PIN_NUM_MOSI, + .sclk_io_num = PIN_NUM_CLK, + .quadwp_io_num = -1, + .quadhd_io_num = -1}; + spi_device_interface_config_t devcfg = { + .clock_speed_hz = SPI_SPD * 1000 * 1000, //Clock out at 10 MHz + .mode = 0, //SPI mode 0 + .spics_io_num = PIN_NUM_CS, //CS pin + .queue_size = 7, //We want to be able to queue 7 transactions at a time + .pre_cb = lcd_spi_pre_transfer_callback, //Specify pre-transfer callback to handle D/C line + }; + //Initialize the SPI bus + ret = spi_bus_initialize(VSPI_HOST, &buscfg, 1); + assert(ret == ESP_OK); + + //Attach the LCD to the SPI bus + ret = spi_bus_add_device(VSPI_HOST, &devcfg, &spi); + assert(ret == ESP_OK); + + //Initialize GPIO + lcd_gpio_init(); + + //Initialize the LCD + lcd_init_seq(spi); + + lcd_setBrightness(800); +} \ No newline at end of file diff --git a/components/nofrendo-esp32/spi_lcd.h b/main/spi_lcd.h similarity index 87% rename from components/nofrendo-esp32/spi_lcd.h rename to main/spi_lcd.h index 1d09625..599d26c 100644 --- a/components/nofrendo-esp32/spi_lcd.h +++ b/main/spi_lcd.h @@ -26,8 +26,8 @@ extern "C" { #endif -void ili9341_write_frame(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t *data[]); -void ili9341_init(); +void lcd_write_frame(const uint16_t x, const uint16_t y, const uint16_t width, const uint16_t height, const uint8_t *data[]); +void lcd_init(); #ifdef __cplusplus diff --git a/main/video_audio.c b/main/video_audio.c new file mode 100644 index 0000000..8e423c7 --- /dev/null +++ b/main/video_audio.c @@ -0,0 +1,329 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "bitmap.h" +#include "event.h" +#include "noftypes.h" +#include "gui.h" +#include "log.h" +#include "nes.h" +#include "nes_pal.h" +#include "nesinput.h" +#include "osd.h" + +#include "spi_lcd.h" +#include "controller.h" + +#include "nofconfig.h" +#include "sdkconfig.h" + +#define DEFAULT_SAMPLERATE 32000 +#define DEFAULT_FRAGSIZE 128 + +TimerHandle_t timer; + +//Seemingly, this will be called only once. Should call func with a freq of frequency, +int osd_installtimer(int frequency, void *func, int funcsize, void *counter, int countersize) +{ + nofrendo_log_printf("Timer install, configTICK_RATE_HZ=%d, freq=%d\n", configTICK_RATE_HZ, frequency); + timer = xTimerCreate("nes", configTICK_RATE_HZ / frequency, pdTRUE, NULL, func); + xTimerStart(timer, 0); + return 0; +} + +/* +** Audio +*/ +static void (*audio_callback)(void *buffer, int length) = NULL; +#if CONFIG_SOUND_ENA +QueueHandle_t queue; +static int16_t *audio_frame; +#endif + +static void do_audio_frame() +{ +#if CONFIG_SOUND_ENA + int left = DEFAULT_SAMPLERATE / NES_REFRESH_RATE; + while (left) + { + int n = DEFAULT_FRAGSIZE; + if (n > left) + n = left; + audio_callback(audio_frame, n); //get more data + + //16 bit mono -> 32-bit (16 bit r+l) + int16_t *mono_ptr = audio_frame + n; + int16_t *stereo_ptr = audio_frame + n + n; + int i = n; + while (i--) + { + // audio_frame[i] = audio_frame[i] + 0x8000; + int16_t a = (*(--mono_ptr) >> 3); + *(--stereo_ptr) = 0x8000 + a; + *(--stereo_ptr) = 0x8000 - a; + } + + size_t i2s_bytes_write; + i2s_write(I2S_NUM_0, (const char *)audio_frame, 4 * n, &i2s_bytes_write, portMAX_DELAY); + left -= i2s_bytes_write / 4; + } +#endif +} + +void osd_setsound(void (*playfunc)(void *buffer, int length)) +{ + //Indicates we should call playfunc() to get more data. + audio_callback = playfunc; +} + +static void osd_stopsound(void) +{ + audio_callback = NULL; +} + +static int osd_init_sound(void) +{ +#if CONFIG_SOUND_ENA + audio_frame = NOFRENDO_MALLOC(4 * DEFAULT_FRAGSIZE); + i2s_config_t cfg = { + .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN, + .sample_rate = DEFAULT_SAMPLERATE, + .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, + .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, + .communication_format = I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_I2S_MSB, + .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, + .dma_buf_count = 6, + .dma_buf_len = 512, + .use_apll = false}; + i2s_driver_install(I2S_NUM_0, &cfg, 2, &queue); + i2s_set_pin(I2S_NUM_0, NULL); + i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN); +#endif + + audio_callback = NULL; + + return 0; +} + +void osd_getsoundinfo(sndinfo_t *info) +{ + info->sample_rate = DEFAULT_SAMPLERATE; + info->bps = 16; +} + +/* +** Video +*/ + +static int init(int width, int height); +static void shutdown(void); +static int set_mode(int width, int height); +static void set_palette(rgb_t *pal); +static void clear(uint8 color); +static bitmap_t *lock_write(void); +static void free_write(int num_dirties, rect_t *dirty_rects); +static void custom_blit(bitmap_t *bmp, int num_dirties, rect_t *dirty_rects); +static char fb[1]; //dummy + +QueueHandle_t vidQueue; + +viddriver_t sdlDriver = + { + "Simple DirectMedia Layer", /* name */ + init, /* init */ + shutdown, /* shutdown */ + set_mode, /* set_mode */ + set_palette, /* set_palette */ + clear, /* clear */ + lock_write, /* lock_write */ + free_write, /* free_write */ + custom_blit, /* custom_blit */ + false /* invalidate flag */ +}; + +bitmap_t *myBitmap; + +void osd_getvideoinfo(vidinfo_t *info) +{ + info->default_width = NES_SCREEN_WIDTH; + info->default_height = NES_SCREEN_HEIGHT; + info->driver = &sdlDriver; +} + +/* flip between full screen and windowed */ +void osd_togglefullscreen(int code) +{ +} + +/* initialise video */ +static int init(int width, int height) +{ + return 0; +} + +static void shutdown(void) +{ +} + +/* set a video mode */ +static int set_mode(int width, int height) +{ + return 0; +} + +uint16 myPalette[256]; + +/* copy nes palette over to hardware */ +static void set_palette(rgb_t *pal) +{ + uint16 c; + + int i; + + for (i = 0; i < 256; i++) + { + c = (pal[i].b >> 3) + ((pal[i].g >> 2) << 5) + ((pal[i].r >> 3) << 11); + //myPalette[i]=(c>>8)|((c&0xff)<<8); + myPalette[i] = c; + } +} + +/* clear all frames to a particular color */ +static void clear(uint8 color) +{ + // SDL_FillRect(mySurface, 0, color); +} + +/* acquire the directbuffer for writing */ +static bitmap_t *lock_write(void) +{ + // SDL_LockSurface(mySurface); + myBitmap = bmp_createhw((uint8 *)fb, NES_SCREEN_WIDTH, NES_SCREEN_HEIGHT, NES_SCREEN_WIDTH * 2); + return myBitmap; +} + +/* release the resource */ +static void free_write(int num_dirties, rect_t *dirty_rects) +{ + bmp_destroy(&myBitmap); +} + +static void custom_blit(bitmap_t *bmp, int num_dirties, rect_t *dirty_rects) +{ + xQueueSend(vidQueue, &bmp, 0); + do_audio_frame(); +} + +//This runs on core 1. +static void videoTask(void *arg) +{ + int x, y; + bitmap_t *bmp = NULL; + x = (320 - NES_SCREEN_WIDTH) / 2; + y = ((240 - NES_SCREEN_HEIGHT) / 2); + while (1) + { + // xQueueReceive(vidQueue, &bmp, portMAX_DELAY);//skip one frame to drop to 30 + xQueueReceive(vidQueue, &bmp, portMAX_DELAY); + lcd_write_frame(x, y, NES_SCREEN_WIDTH, NES_SCREEN_HEIGHT, (const uint8_t **)bmp->line); + } +} + +/* +** Input +*/ + +static void osd_initinput() +{ + controller_init(); +} + +void osd_getinput(void) +{ + const int ev[16] = { + event_joypad1_select, event_state_save, event_state_load, event_joypad1_start, event_joypad1_up, event_joypad1_right, event_joypad1_down, event_joypad1_left, + 0, 0, 0, 0, event_soft_reset, event_joypad1_a, event_joypad1_b, event_hard_reset}; + static int oldb = 0xffff; + int b = controller_read_input(); + int chg = b ^ oldb; + int x; + oldb = b; + event_t evh; + // nofrendo_log_printf("Input: %x\n", b); + for (x = 0; x < 16; x++) + { + if (chg & 1) + { + evh = event_get(ev[x]); + if (evh) + evh((b & 1) ? INP_STATE_BREAK : INP_STATE_MAKE); + } + chg >>= 1; + b >>= 1; + } +} + +static void osd_freeinput(void) +{ +} + +void osd_getmouse(int *x, int *y, int *button) +{ +} + +/* +** Shutdown +*/ + +/* this is at the bottom, to eliminate warnings */ +void osd_shutdown() +{ + osd_stopsound(); + osd_freeinput(); +} + +static int logprint(const char *string) +{ + return printf("%s", string); +} + +/* +** Startup +*/ + +int osd_init() +{ + log_chain_logfunc(logprint); + + if (osd_init_sound()) + return -1; + + lcd_init(); + lcd_write_frame(0, 0, 320, 240, NULL); + vidQueue = xQueueCreate(1, sizeof(bitmap_t *)); + xTaskCreatePinnedToCore(&videoTask, "videoTask", 2048, NULL, 5, NULL, 1); + osd_initinput(); + return 0; +} diff --git a/partitions.csv b/partitions.csv index 9b85441..e9772b6 100644 --- a/partitions.csv +++ b/partitions.csv @@ -1,6 +1,6 @@ -# Name, Type, SubType, Offset, Size -# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild -nvs, data, nvs, 0x9000, 0x6000 -phy_init, data, phy, 0xf000, 0x1000 -factory, app, factory, 0x10000, 0x0E0000 -nesgame, 0x40, 0x01, 0x100000, 0x300000 +# Name, Type, SubType, Offset, Size, Flags +nvs, data, nvs, 0x9000, 0x5000, +otadata, data, ota, 0xe000, 0x2000, +app0, app, ota_0, 0x10000, 0x140000, +app1, app, ota_1, 0x150000,0x140000, +spiffs, data, spiffs, 0x290000,0x170000, diff --git a/sdkconfig b/sdkconfig deleted file mode 100644 index 70f420f..0000000 --- a/sdkconfig +++ /dev/null @@ -1,281 +0,0 @@ -# -# Automatically generated file; DO NOT EDIT. -# Espressif IoT Development Framework Configuration -# - -# -# SDK tool configuration -# -CONFIG_TOOLPREFIX="xtensa-esp32-elf-" -CONFIG_PYTHON="python" - -# -# Bootloader config -# -# CONFIG_LOG_BOOTLOADER_LEVEL_NONE is not set -# CONFIG_LOG_BOOTLOADER_LEVEL_ERROR is not set -CONFIG_LOG_BOOTLOADER_LEVEL_WARN=y -# CONFIG_LOG_BOOTLOADER_LEVEL_INFO is not set -# CONFIG_LOG_BOOTLOADER_LEVEL_DEBUG is not set -# CONFIG_LOG_BOOTLOADER_LEVEL_VERBOSE is not set -CONFIG_LOG_BOOTLOADER_LEVEL=2 - -# -# Security features -# -# CONFIG_SECURE_BOOT_ENABLED is not set -# CONFIG_FLASH_ENCRYPTION_ENABLED is not set - -# -# Serial flasher config -# -CONFIG_ESPTOOLPY_PORT="/dev/ttyUSB1" -# CONFIG_ESPTOOLPY_BAUD_115200B is not set -# CONFIG_ESPTOOLPY_BAUD_230400B is not set -CONFIG_ESPTOOLPY_BAUD_921600B=y -# CONFIG_ESPTOOLPY_BAUD_2MB is not set -# CONFIG_ESPTOOLPY_BAUD_OTHER is not set -CONFIG_ESPTOOLPY_BAUD_OTHER_VAL=115200 -CONFIG_ESPTOOLPY_BAUD=921600 -CONFIG_ESPTOOLPY_COMPRESSED=y -# CONFIG_FLASHMODE_QIO is not set -# CONFIG_FLASHMODE_QOUT is not set -CONFIG_FLASHMODE_DIO=y -# CONFIG_FLASHMODE_DOUT is not set -CONFIG_ESPTOOLPY_FLASHMODE="dio" -# CONFIG_ESPTOOLPY_FLASHFREQ_80M is not set -CONFIG_ESPTOOLPY_FLASHFREQ_40M=y -# CONFIG_ESPTOOLPY_FLASHFREQ_26M is not set -# CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set -CONFIG_ESPTOOLPY_FLASHFREQ="40m" -# CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set -# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set -CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y -# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set -# CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set -CONFIG_ESPTOOLPY_FLASHSIZE="4MB" -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y -CONFIG_ESPTOOLPY_BEFORE_RESET=y -# CONFIG_ESPTOOLPY_BEFORE_NORESET is not set -CONFIG_ESPTOOLPY_BEFORE="default_reset" -CONFIG_ESPTOOLPY_AFTER_RESET=y -# CONFIG_ESPTOOLPY_AFTER_NORESET is not set -CONFIG_ESPTOOLPY_AFTER="hard_reset" -# CONFIG_MONITOR_BAUD_9600B is not set -# CONFIG_MONITOR_BAUD_57600B is not set -CONFIG_MONITOR_BAUD_115200B=y -# CONFIG_MONITOR_BAUD_230400B is not set -# CONFIG_MONITOR_BAUD_921600B is not set -# CONFIG_MONITOR_BAUD_2MB is not set -# CONFIG_MONITOR_BAUD_OTHER is not set -CONFIG_MONITOR_BAUD_OTHER_VAL=115200 -CONFIG_MONITOR_BAUD=115200 - -# -# Nofrendo ESP32-specific configuration -# -# CONFIG_HW_WROVERKIT_V1 is not set -CONFIG_HW_WROVERKIT_V2=y -# CONFIG_HW_CUSTOM is not set -CONFIG_HW_LCD_TYPE_ILI=y -# CONFIG_HW_LCD_TYPE_ST is not set -CONFIG_HW_WROVERKIT=y -CONFIG_HW_LCD_TYPE=0 -CONFIG_HW_INV_BL=y -CONFIG_HW_LCD_MISO_GPIO=25 -CONFIG_HW_LCD_MOSI_GPIO=23 -CONFIG_HW_LCD_CLK_GPIO=19 -CONFIG_HW_LCD_CS_GPIO=22 -CONFIG_HW_LCD_DC_GPIO=21 -CONFIG_HW_LCD_RESET_GPIO=18 -CONFIG_HW_LCD_BL_GPIO=5 -# CONFIG_SOUND_ENA is not set -CONFIG_HW_PSX_ENA=y -CONFIG_HW_PSX_CLK=14 -CONFIG_HW_PSX_DAT=27 -CONFIG_HW_PSX_ATT=16 -CONFIG_HW_PSX_CMD=2 - -# -# Partition Table -# -# CONFIG_PARTITION_TABLE_SINGLE_APP is not set -# CONFIG_PARTITION_TABLE_TWO_OTA is not set -CONFIG_PARTITION_TABLE_CUSTOM=y -CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" -CONFIG_PARTITION_TABLE_CUSTOM_APP_BIN_OFFSET=0x10000 -CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" -CONFIG_APP_OFFSET=0x10000 -CONFIG_OPTIMIZATION_LEVEL_DEBUG=y -# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set - -# -# Component config -# -# CONFIG_AWS_IOT_SDK is not set -# CONFIG_BT_ENABLED is not set -CONFIG_BT_RESERVE_DRAM=0 - -# -# ESP32-specific -# -# CONFIG_ESP32_DEFAULT_CPU_FREQ_80 is not set -# CONFIG_ESP32_DEFAULT_CPU_FREQ_160 is not set -CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y -CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240 -CONFIG_MEMMAP_SMP=y -# CONFIG_MEMMAP_TRACEMEM is not set -# CONFIG_MEMMAP_TRACEMEM_TWOBANKS is not set -# CONFIG_ESP32_TRAX is not set -CONFIG_TRACEMEM_RESERVE_DRAM=0x0 -# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set -# CONFIG_ESP32_ENABLE_COREDUMP_TO_UART is not set -CONFIG_ESP32_ENABLE_COREDUMP_TO_NONE=y -# CONFIG_ESP32_ENABLE_COREDUMP is not set -# CONFIG_ESP32_APPTRACE_DEST_TRAX is not set -# CONFIG_ESP32_APPTRACE_DEST_UART is not set -CONFIG_ESP32_APPTRACE_DEST_NONE=y -# CONFIG_ESP32_APPTRACE_ENABLE is not set -# CONFIG_TWO_UNIVERSAL_MAC_ADDRESS is not set -CONFIG_FOUR_UNIVERSAL_MAC_ADDRESS=y -CONFIG_NUMBER_OF_UNIVERSAL_MAC_ADDRESS=4 -CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32 -CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2048 -CONFIG_MAIN_TASK_STACK_SIZE=12240 -CONFIG_NEWLIB_STDOUT_ADDCR=y -# CONFIG_NEWLIB_NANO_FORMAT is not set -CONFIG_CONSOLE_UART_DEFAULT=y -# CONFIG_CONSOLE_UART_CUSTOM is not set -# CONFIG_CONSOLE_UART_NONE is not set -CONFIG_CONSOLE_UART_NUM=0 -CONFIG_CONSOLE_UART_BAUDRATE=115200 -# CONFIG_ULP_COPROC_ENABLED is not set -CONFIG_ULP_COPROC_RESERVE_MEM=0 -# CONFIG_ESP32_PANIC_PRINT_HALT is not set -CONFIG_ESP32_PANIC_PRINT_REBOOT=y -# CONFIG_ESP32_PANIC_SILENT_REBOOT is not set -# CONFIG_ESP32_PANIC_GDBSTUB is not set -CONFIG_ESP32_DEBUG_OCDAWARE=y -# CONFIG_INT_WDT is not set -# CONFIG_TASK_WDT is not set -# CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set -# CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 is not set -# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set -CONFIG_ESP32_TIME_SYSCALL_USE_NONE=y -CONFIG_ESP32_RTC_CLOCK_SOURCE_INTERNAL_RC=y -# CONFIG_ESP32_RTC_CLOCK_SOURCE_EXTERNAL_CRYSTAL is not set -CONFIG_ESP32_RTC_CLK_CAL_CYCLES=1024 -CONFIG_ESP32_DEEP_SLEEP_WAKEUP_DELAY=0 -# CONFIG_ESP32_XTAL_FREQ_40 is not set -# CONFIG_ESP32_XTAL_FREQ_26 is not set -CONFIG_ESP32_XTAL_FREQ_AUTO=y -CONFIG_ESP32_XTAL_FREQ=0 -# CONFIG_WIFI_ENABLED is not set -# CONFIG_ETHERNET is not set - -# -# FAT Filesystem support -# -CONFIG_FATFS_CODEPAGE_ASCII=y -# CONFIG_FATFS_CODEPAGE_437 is not set -# CONFIG_FATFS_CODEPAGE_720 is not set -# CONFIG_FATFS_CODEPAGE_737 is not set -# CONFIG_FATFS_CODEPAGE_771 is not set -# CONFIG_FATFS_CODEPAGE_775 is not set -# CONFIG_FATFS_CODEPAGE_850 is not set -# CONFIG_FATFS_CODEPAGE_852 is not set -# CONFIG_FATFS_CODEPAGE_855 is not set -# CONFIG_FATFS_CODEPAGE_857 is not set -# CONFIG_FATFS_CODEPAGE_860 is not set -# CONFIG_FATFS_CODEPAGE_861 is not set -# CONFIG_FATFS_CODEPAGE_862 is not set -# CONFIG_FATFS_CODEPAGE_863 is not set -# CONFIG_FATFS_CODEPAGE_864 is not set -# CONFIG_FATFS_CODEPAGE_865 is not set -# CONFIG_FATFS_CODEPAGE_866 is not set -# CONFIG_FATFS_CODEPAGE_869 is not set -# CONFIG_FATFS_CODEPAGE_932 is not set -# CONFIG_FATFS_CODEPAGE_936 is not set -# CONFIG_FATFS_CODEPAGE_949 is not set -# CONFIG_FATFS_CODEPAGE_950 is not set -CONFIG_FATFS_CODEPAGE=1 -CONFIG_FATFS_MAX_LFN=255 - -# -# FreeRTOS -# -# CONFIG_FREERTOS_UNICORE is not set -CONFIG_FREERTOS_CORETIMER_0=y -# CONFIG_FREERTOS_CORETIMER_1 is not set -CONFIG_FREERTOS_HZ=1000 -CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y -# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_NONE is not set -# CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set -CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY=y -# CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set -CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=3 -CONFIG_FREERTOS_ASSERT_FAIL_ABORT=y -# CONFIG_FREERTOS_ASSERT_FAIL_PRINT_CONTINUE is not set -# CONFIG_FREERTOS_ASSERT_DISABLE is not set -CONFIG_FREERTOS_BREAK_ON_SCHEDULER_START_JTAG=y -# CONFIG_ENABLE_MEMORY_DEBUG is not set -CONFIG_FREERTOS_ISR_STACKSIZE=1536 -# CONFIG_FREERTOS_LEGACY_HOOKS is not set -CONFIG_FREERTOS_MAX_TASK_NAME_LEN=16 -# CONFIG_SUPPORT_STATIC_ALLOCATION is not set -CONFIG_TIMER_TASK_PRIORITY=1 -CONFIG_TIMER_TASK_STACK_DEPTH=2048 -CONFIG_TIMER_QUEUE_LENGTH=10 -# CONFIG_FREERTOS_DEBUG_INTERNALS is not set - -# -# Log output -# -# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set -# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set -# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set -CONFIG_LOG_DEFAULT_LEVEL_INFO=y -# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set -# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set -CONFIG_LOG_DEFAULT_LEVEL=3 -CONFIG_LOG_COLORS=y - -# -# LWIP -# -# CONFIG_L2_TO_L3_COPY is not set -CONFIG_LWIP_MAX_SOCKETS=4 -CONFIG_LWIP_THREAD_LOCAL_STORAGE_INDEX=0 -# CONFIG_LWIP_SO_REUSE is not set -# CONFIG_LWIP_SO_RCVBUF is not set -CONFIG_LWIP_DHCP_MAX_NTP_SERVERS=1 -# CONFIG_LWIP_IP_FRAG is not set -# CONFIG_LWIP_IP_REASSEMBLY is not set -CONFIG_TCP_MAXRTX=12 -CONFIG_TCP_SYNMAXRTX=6 -# CONFIG_LWIP_DHCP_DOES_ARP_CHECK is not set -CONFIG_TCPIP_TASK_STACK_SIZE=2560 -# CONFIG_PPP_SUPPORT is not set - -# -# mbedTLS -# -CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384 -# CONFIG_MBEDTLS_DEBUG is not set -CONFIG_MBEDTLS_HARDWARE_AES=y -CONFIG_MBEDTLS_HARDWARE_MPI=y -CONFIG_MBEDTLS_MPI_USE_INTERRUPT=y -CONFIG_MBEDTLS_HARDWARE_SHA=y - -# -# OpenSSL -# -# CONFIG_OPENSSL_DEBUG is not set -CONFIG_OPENSSL_ASSERT_DO_NOTHING=y -# CONFIG_OPENSSL_ASSERT_EXIT is not set - -# -# SPI Flash driver -# -# CONFIG_SPI_FLASH_ENABLE_COUNTERS is not set -CONFIG_SPI_FLASH_ROM_DRIVER_PATCH=y diff --git a/sdkconfig.defaults b/sdkconfig.defaults new file mode 100644 index 0000000..2f3da83 --- /dev/null +++ b/sdkconfig.defaults @@ -0,0 +1,11 @@ +CONFIG_ESPTOOLPY_BAUD_921600B=y +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" +CONFIG_ESP32_DEFAULT_CPU_FREQ_240=y +CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ=240 +CONFIG_ESP32_SPIRAM_SUPPORT=y +CONFIG_SPIRAM_BOOT_INIT=y +CONFIG_SPIRAM_IGNORE_NOTFOUND=y +CONFIG_ESP_TASK_WDT=n +CONFIG_FREERTOS_HZ=1000