書接上文
《單片機開發(fā)—ESP32-S3模塊上手》
《單片機開發(fā)—ESP32S3移植lvgl+觸摸屏》
參考內(nèi)容
依舊是參考韋東山老師的作品來移植的
《ESP32|爺青回!ESP32(單片機) NES模擬器_NES游戲機掌機教程(開源+詳細講解實現(xiàn)代碼!)》
韋老師已經(jīng)將代碼開源,喜歡的朋友當(dāng)然是可以去支持一波。
另外還有g(shù)ithub上的一份原始代碼,喜歡從頭來的,也可以去學(xué)習(xí)一下,核心部分是一樣的,適配硬件的部分需要自己來修改。
github上的espressif/esp32-nesemu
移植效果

esp32s3模擬nes
小時候玩的第一個游戲就是超級瑪麗,算是callback了。
移植過程
我使用的是ESP-IDF4.4的開發(fā)環(huán)境,和韋老師的不太一樣,并且硬件也是ESP32S3,所以我的方法就是將代碼移植過來,重新構(gòu)建了一個工程。
源碼
將menu和nofrendo代碼復(fù)制過來,并且將適配層代碼提出來并列目錄。工程采用了原始的helloworld項目,只是重新修改了主函數(shù)的c文件。
漫長的編譯過程
修改Cmake
首先需要添加對目錄的檢索,將c文件都進行編譯,并且添加頭文件檢索路徑,以便包含的時候,更加簡單。
FILE(GLOB_RECURSE app_sources ./*.* ./menu/*.* ./esp32s3/*.* ./nofrendo/*.* ./nofrendo/cpu/*.* ./nofrendo/libsnss/*.* ./nofrendo/mappers/*.* ./nofrendo/nes/*.* ./nofrendo/sndhrdw/*.*)
idf_component_register(
SRCS ${app_sources}
INCLUDE_DIRS "."
INCLUDE_DIRS "./menu/"
INCLUDE_DIRS "./esp32s3/"
INCLUDE_DIRS "./nofrendo/"
INCLUDE_DIRS "./nofrendo/cpu/"
INCLUDE_DIRS "./nofrendo/libsnss/"
INCLUDE_DIRS "./nofrendo/mappers/"
INCLUDE_DIRS "./nofrendo/nes/"
INCLUDE_DIRS "./nofrendo/sndhrdw/"
EMBED_FILES "./100ask_logo.jpg"
)
這兩行就達到了自動搜索對應(yīng)路徑的c文件,并且檢索對應(yīng)路徑的頭文件。
另外如果編譯的時候,需要修改一些FLAGS或者增加一些宏定義進行配置編譯,參考下面句子修改
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=char-subscripts -Wno-error=attributes -DNOFRENDO_DEBUG -DCONFIG_HW_CONTROLLER_GPIO")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=char-subscripts -Wno-error=attributes -DNOFRENDO_DEBUG -DCONFIG_HW_CONTROLLER_GPIO")
我將對應(yīng)韋老師的代碼中定義的宏定義以及FLAGS放在這里了,然后才能開始第一步的編譯。否則就怕有其他莫名其妙的問題。
宏定義與函數(shù)沖突
編譯的時候遇到
expected declaration specifiers or '...' before '
原因就是模擬器中重新定義了malloc和free
但是和其他文件一起編譯的時候,收到stdlib.h中的同名函數(shù)影響,就會報錯。
嘗試過用原有的malloc,但是會出現(xiàn)內(nèi)存異常。
所以直接將模擬器部分的代碼,重新替換了新的宏定義,
里面可能有一些問題,通過這個重新封裝的函數(shù),在釋放空指針等操作的時候,給出提示,或者直接跳過。
不起作用的一句話
error: this 'if' clause does not guard... [-Werror=misleading-indentation]
報錯的
if (!pMem)
return XX;
修改后
if (!pMem)
{
return XX;
}
反正我是一直看不上那些不愛加括號的代碼。一塊的功能,就是要用括號括起來,這樣看起來工整多了。
移植小竅門
涉及到硬件的部分,首先把中間層的代碼中,每個文件對外的接口提供出來,保證函數(shù)存在,該有返回值的,有返回值,其余代碼注釋掉。
保證編譯通過,然后燒寫,根據(jù)報錯的內(nèi)容,一步一步打開代碼再修改,這樣能夠熟悉所有的流程,并且學(xué)習(xí)出代碼的功能。
隨后慢慢增加代碼。
移植過程
該注釋的注釋掉,很快就能編譯通過。然后就開始調(diào)試。
SD卡模塊
源碼首先是注冊SD卡
因為是要將nes的rom放在sd卡中。
參考esp32s3的example代碼。替換掉源碼中的部分代碼
esp_err_t init_sd_card(void)
{
esp_err_t ret;
// Options for mounting the filesystem.
// If format_if_mount_failed is set to true, SD card will be partitioned and
// formatted in case when mounting fails.
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
#ifdef CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED
.format_if_mount_failed = true,
#else
.format_if_mount_failed = false,
#endif // EXAMPLE_FORMAT_IF_MOUNT_FAILED
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
sdmmc_card_t *card;
const char mount_point[] = "/sdcard";
ESP_LOGI(TAG, "Initializing SD card");
// Use settings defined above to initialize SD card and mount FAT filesystem.
// Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
// Please check its source code and implement error recovery when developing
// production applications.
ESP_LOGI(TAG, "Using SPI peripheral");
sdmmc_host_t host = SDSPI_HOST_DEFAULT();
host.slot=SD_HOST;
spi_bus_config_t bus_cfg = {
.mosi_io_num = SD_MOSI,
.miso_io_num = SD_MISO,
.sclk_io_num = SD_CLK,
.quadwp_io_num = -1,
.quadhd_io_num = -1,
.max_transfer_sz = 4000,
};
ret = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize bus.");
return;
}
// This initializes the slot without card detect (CD) and write protect (WP) signals.
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
slot_config.gpio_cs = SD_CS;
slot_config.host_id = host.slot;
ESP_LOGI(TAG, "Mounting filesystem");
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
if (ret != ESP_OK)
{
if (ret == ESP_FAIL)
{
ESP_LOGE(TAG, "Failed to mount filesystem. "
"If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
}
else
{
ESP_LOGE(TAG, "Failed to initialize the card (%s). "
"Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret));
}
return ret;
}
else
{
// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);
return ESP_OK;
}
}
這里注意SD的SPI通道選擇,因為LCD通常用高速通道,所以這個SD卡我們用在了SPI3上。
一共就兩個SPI,只能這樣計劃了。
輸入模塊
源碼第二步就是輸入設(shè)備初始化
這里的輸入設(shè)備支持到了三種,包括GPIO,I2C和手柄。
這里根據(jù)不同宏定義進行了編譯包含,后者我們都沒有,所以只能用GPIO。
上拉和下拉的選擇
這里我用的是一個GPIO按鍵模塊,前面在w801上用過的。
輸入方式下。內(nèi)部上拉保證了如果沒有輸入,就是高電平,下拉相反,沒有輸入就是低電平。
由于我這里公共端是高電平,所以需要使能下拉,保證了:
無輸入:0,有輸入:1
static void _init_gpio(gpio_num_t gpio_num)
{
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_POSEDGE;
io_conf.pin_bit_mask = (1ULL<<gpio_num);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 0;
io_conf.pull_down_en = 1;
gpio_config(&io_conf);
}
然后定義了部分GPIO來使用
只是為了驗證部分功能,所以只注冊了部分按鍵
顯示模塊
第三步就是顯示菜單,然后結(jié)合前面的內(nèi)容選擇rom
這里的初始化與esp32s3基本一致,所以修改好對應(yīng)的引腳和SPI通道,就可以使用了
然后需要修改一下這個函數(shù)
//Load Rom list from flash partition to char array(lines), init some variables for printing rom list
void initRomList()
{
DIR *pDir = NULL;
struct dirent * pEnt = NULL;
pDir = opendir("/sdcard/nes");
char fileName[FILENAME_LENGTH][FILENAME_LENGTH+1];
int dir_count = 0;
entryCount = 0;
if (NULL == pDir)
{
perror("opendir");
}
else
{
while (1)
{
pEnt = readdir(pDir);
if(pEnt != NULL)
{
ESP_LOGI(TAG,"rom name[%s]", pEnt->d_name);
strcpy(fileName[dir_count], pEnt->d_name);
dir_count++;
entryCount++;
}
else
{
break;
}
}
closedir(pDir);
}
if(entryCount > 0)
{
menuEntries = (MenuEntry *)malloc(entryCount * sizeof(MenuEntry));
for (int i = 0; i < entryCount; i++)
{
//menuEntries[i].entryNumber = i;
//menuEntries[i].icon = 'E';
menuEntries[i].icon = '$';
//strcpy(menuEntries[i].name, fileName[i]);
memset(menuEntries[i].fileName,0,FILENAME_LENGTH+1);//sunjin
strcpy(menuEntries[i].fileName, fileName[i]);
for (int j = strlen(menuEntries[i].fileName); j > 0; j--)
{
if (menuEntries[i].fileName[j] < ' ')
{
menuEntries[i].fileName[j] = '\0';
}
}
}
ESP_LOGI(TAG,"Read %d rom entries", entryCount);
}
else
{
ESP_LOGW(TAG,"no roms!");
}
}
里面我修改了一下獲取的文件數(shù)量變量初始值以及初始化了一下數(shù)組,否則會出現(xiàn)內(nèi)存異常以及顯示亂碼的問題。
到達這一步的時候,就可以顯示開機動畫以及rom選擇菜單了。
讀取ROM
接下來就是正式啟動模擬器了
這里的需要修改的,就是將rom文件讀取到內(nèi)存中,源碼為這個函數(shù)
這里涉及到了一個分區(qū)表的概念,具體可以參考
分區(qū)表
簡單來說就是將數(shù)據(jù)從SD卡讀取到FALSH中,然后就可以當(dāng)成一個靜態(tài)數(shù)組來使用,訪問這里就像訪問內(nèi)存一樣,解決了單片機內(nèi)存小的問題。
這里我就不一樣了,我有8M的內(nèi)存,所以這里我直接修改放在內(nèi)存中。
char *romdata;
// Open the file
ESP_LOGI(TAG, "Reading rom from %s", selectedRomFilename);
FILE *rom = fopen(selectedRomFilename, "r");
long fileSize = -1;
if (!rom)
{
ESP_LOGE(TAG, "Could not read %s", selectedRomFilename);
exit(1);
}
// First figure out how large the file is
fseek(rom, 0L, SEEK_END);
fileSize = ftell(rom);
rewind(rom);
romdata=malloc(fileSize+READ_BUFFER_SIZE);
if (!romdata)
{
ESP_LOGE(TAG, "Could not malloc ");
exit(1);
}
// Copy the file contents into EEPROM memory
char buffer[READ_BUFFER_SIZE];
int offset = 0;
while (fread(buffer, 1, READ_BUFFER_SIZE, rom) > 0)
{
memcpy(romdata+offset,buffer,READ_BUFFER_SIZE);
offset += READ_BUFFER_SIZE;
}
fclose(rom);
ESP_LOGI(TAG, "Loaded %d bytes into ROM memory", offset);
return (char *)romdata;
就是豪橫。
繪制游戲
spi_lcd.c中對外就提供了兩個接口,
其實就是用來初始化顯示屏和繪制圖像的,韋老師的代碼中用額的gpio模擬的方式進行驅(qū)動屏幕,與前面顯示菜單用了兩套軟件。
這里我整合為一套,就用了顯示菜單的方式。所以初始化中,我只保留了一些變量初始化,然后申請了2條緩存,用來更新畫面
繪制圖像的函數(shù),就比較難了。我看了好久才找到顯示的數(shù)據(jù)。
void draw_write_frame(const uint16_t xs, const uint16_t ys, const uint16_t width, const uint16_t height, const uint8_t *data[], bool xStr, bool yStr)
{
int x, y;
int xx, yy;
int i;
uint16_t x1, y1, evenPixel, oddPixel, backgroundColor;
int drsy = 0;
uint32_t xv, yv, dc;
uint32_t temp[16];
if(data==NULL)
{
return;
}
if (getShowMenu() != lastShowMenu)
{
memset(rowCrc, 0, sizeof rowCrc);
}
lastShowMenu = getShowMenu();
int lastY = -1;
int lastYshown = 0;
// Black background
backgroundColor = 0;
for (y = 0; y < height; y++)
{
yy = yStr ? scaleY[y] : y;
if (lastY == yy)
{
if (!lastYshown && !getShowMenu())
continue;
}
else
{
lastY = yy;
uint16_t crc = calcCrc(data[yy]);
if (crc == rowCrc[yy] && !getShowMenu())
{
lastYshown = false;
continue;
}
else
{
lastYshown = true;
rowCrc[yy] = crc;
}
}
//start line
x1 = xs + (width - 1);
y1 = ys + y + (height - 1);
xv = U16x2toU32(xs, x1);
yv = U16x2toU32((ys + y), y1);
drsy = 0;
x = 0;
while (x < width)
{
// Render 32 pixels, grouped as pairs of 16-bit pixels stored in 32-bit values
for (i = 0; i < 16; i++)
{
xx = xStr ? scaleX[x] : x;
if (xx >= 32 && !xStr)
xx -= 32;
evenPixel = myPalette[(unsigned char)(data[yy][xx])];
x++;
xx = xStr ? scaleX[x] : x;
if (xx >= 32 && !xStr)
xx -= 32;
oddPixel = myPalette[(unsigned char)(data[yy][xx])];
x++;
if (!xStr && (x <= 32 || x >= 288))
evenPixel = oddPixel = backgroundColor;
if (!yStr && y >= 224)
evenPixel = oddPixel = backgroundColor;
if (getShowMenu())
{
evenPixel = oddPixel = renderInGameMenu(x, y, evenPixel, oddPixel, xStr, yStr);
}
fastlines[BbufIdx][drsy++]=evenPixel;
fastlines[BbufIdx][drsy++]=oddPixel;
}
}
AbufIdx = BbufIdx;
BbufIdx = 1 - BbufIdx;
nes_100ask_send_line_finish(mylcd_spi);
nes_100ask_send_one_line(mylcd_spi, yy, (uint16_t*)(fastlines[AbufIdx]));
}
if (nes_100ask_get_shutdown())
setBrightness(nes_100ask_get_bright());
//#if LCD_BCKL >= 0
// if (nes_100ask_get_bright() == -1)
// LCD_BKG_OFF();
//#endif
}
這里有兩個問題。
- 數(shù)據(jù)獲取
一開是以為傳入的data就是數(shù)據(jù),其實后來發(fā)現(xiàn),這里需要計算出每個像素,
fastlines[BbufIdx][drsy++]=evenPixel;
fastlines[BbufIdx][drsy++]=oddPixel;
再將循環(huán)buf一次一次交替行繪制。
- 調(diào)色板
模擬器計算出每個點的顏色,結(jié)果繪制來發(fā)現(xiàn),顏色不對,像極了我之前在w801上移植的時候,于是我返回去找了一下,原來是這個原因,在寫入SPI總線 時候,大小端的問題,所以為了從根本上解決問題。
我直接修改了調(diào)色板!
uint16 myPalette[256];
unsigned short Convert(unsigned short s)
{
char right, left;
right = s& 0XFF;//低八位
left = s >> 8;//高八位 右移8位
s = right * 256 + left;
return s;
}
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] = Convert(c);
}
}
因為不要在畫圖的時候再進行轉(zhuǎn)化,會影響顯示速度。
其他功能
剩余的問題包括了聲音,手柄2的擴展,這些東西后面需要補充一下,才能像一個能用的游戲機。
所以還有續(xù)集。
結(jié)束語
以前人有兩個坎,73和84,現(xiàn)在人也有兩坎,35和65,薅羊毛也不能光可著這一代人薅吧。文章來源:http://www.zghlxwxcb.cn/news/detail-415261.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-415261.html
到了這里,關(guān)于單片機開發(fā)---ESP32S3移植NES模擬器(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!