├── hardware ├── pcb.png ├── gerbers.zip ├── schematic.pdf ├── schematic.png ├── pulldown_mod.jpg └── PicoROM_Assembled.jpg ├── firmware ├── picorom.rom └── cpc_rom_emulator.uf2 ├── .gitmodules ├── romsets ├── DES.CFG ├── DEFAULT.CFG ├── GAMES2.CFG ├── GAMES3.CFG └── GAMES1.CFG ├── .vscode ├── settings.json └── c_cpp_properties.json ├── .gitignore ├── src ├── z80 │ ├── Makefile │ └── picorom.s ├── latch.pio ├── flash.h ├── fatfs │ ├── source │ │ ├── 00readme.txt │ │ ├── diskio.h │ │ ├── diskio.c │ │ ├── ffsystem.c │ │ ├── ffconf.h │ │ ├── 00history.txt │ │ └── ff.h │ └── LICENSE.txt ├── bootsel_button.h ├── flash.cpp ├── fatfs_driver.c ├── FlashInterfaceRP2040_SDK.h ├── pico_sdk_import.cmake ├── CMakeLists.txt ├── tusb_config.h ├── usb_msc_driver.c ├── memmap_custom.ld ├── usb_descriptors.c └── main.c ├── licence.md └── readme.md /hardware/pcb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/hardware/pcb.png -------------------------------------------------------------------------------- /firmware/picorom.rom: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/firmware/picorom.rom -------------------------------------------------------------------------------- /hardware/gerbers.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/hardware/gerbers.zip -------------------------------------------------------------------------------- /hardware/schematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/hardware/schematic.pdf -------------------------------------------------------------------------------- /hardware/schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/hardware/schematic.png -------------------------------------------------------------------------------- /hardware/pulldown_mod.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/hardware/pulldown_mod.jpg -------------------------------------------------------------------------------- /firmware/cpc_rom_emulator.uf2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/firmware/cpc_rom_emulator.uf2 -------------------------------------------------------------------------------- /hardware/PicoROM_Assembled.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mattcallow/CPC_PICOROM/HEAD/hardware/PicoROM_Assembled.jpg -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "src/SPIFTL"] 2 | path = src/SPIFTL 3 | url = https://github.com/earlephilhower/SPIFTL.git 4 | -------------------------------------------------------------------------------- /romsets/DES.CFG: -------------------------------------------------------------------------------- 1 | # Destop Environment System 2 | L:OS_6128.ROM 3 | 0:BASIC_1.1.ROM 4 | 1:DES1.ROM 5 | 2:DES2.ROM 6 | 11:picorom.rom 7 | 8 | -------------------------------------------------------------------------------- /romsets/DEFAULT.CFG: -------------------------------------------------------------------------------- 1 | # Default config 2 | L:OS_6128.ROM 3 | 0:BASIC_1.1.ROM 4 | 2:maxam15.ROM 5 | 3:Protext.rom 6 | 4:Utopia_v1_25b.ROM 7 | 7:unidos.rom 8 | 10:Albireo.rom 9 | 11:picorom.rom 10 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "*.s": "plaintext", 4 | "flash.h": "c", 5 | "xip.h": "c", 6 | "stdlib.h": "c", 7 | "stdio.h": "c" 8 | } 9 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | roms/* 3 | src/z80/picorom.sym 4 | src/z80/picorom.lis 5 | src/z80/picorom.map 6 | src/z80/picorom.o 7 | src/z80/picorom.rom 8 | src/z80/picorom.rom.h 9 | .vscode/* 10 | .DS_Store 11 | -------------------------------------------------------------------------------- /src/z80/Makefile: -------------------------------------------------------------------------------- 1 | Z80ASM=/home/matt/Projects/z88dk/bin/z88dk-z80asm 2 | Z80ASM=z88dk.z88dk-z80asm 3 | 4 | picorom.rom: picorom.s 5 | # z80asm -l --opicorom.rom picorom.s 6 | # z88dk.z88dk-z80asm -v -s -l -m -b -mz80 -opicorom.rom picorom.s 7 | ${Z80ASM} -v -s -l -m -b -mz80 -opicorom.rom picorom.s 8 | cp $@ ../firmware 9 | -------------------------------------------------------------------------------- /romsets/GAMES2.CFG: -------------------------------------------------------------------------------- 1 | # Games2 2 | L:OS_6128.ROM 3 | 0:BASIC_1.1.ROM 4 | 1:GAMES/BRAXX.ROM 5 | 2:GAMES/Blagger.rom 6 | 3:GAMES/Boulderdash.rom 7 | 4:GAMES/Bruce_Lee.rom 8 | 5:GAMES/bj1.rom 9 | 6:GAMES/bj2.rom 10 | 7:GAMES/bj3.rom 11 | 8:GAMES/Chopper_Squad.rom 12 | 9:GAMES/EggRSX.ROM 13 | 10:GAMES/Classic_Invaders.rom 14 | 11:picorom.rom 15 | -------------------------------------------------------------------------------- /romsets/GAMES3.CFG: -------------------------------------------------------------------------------- 1 | # Games 3 2 | L:OS_6128.ROM 3 | 0:BASIC_1.1.ROM 4 | 1:GAMES/Moon_Buggy.rom 5 | 2:GAMES/Killer_Gorilla.rom 6 | 3:GAMES/Gauntlet.rom 7 | 4:GAMES/Harrier.rom 8 | 5:GAMES/Ghouls.rom 9 | 6:GAMES/Donkey_Kong.rom 10 | 7:GAMES/Space_Mania.rom 11 | 8:GAMES/SpaceHwk.rom 12 | 9:GAMES/Ohmummy.rom 13 | 10:GAMES/Thrust.rom 14 | 11:picorom.rom 15 | -------------------------------------------------------------------------------- /romsets/GAMES1.CFG: -------------------------------------------------------------------------------- 1 | # Games1 2 | L:OS_6128.ROM 3 | 0:BASIC_1.1.ROM 4 | 1:GAMES/Ahhh.rom 5 | 2:GAMES/Air_Traffic_Control.rom 6 | 3:GAMES/Airwolf.rom 7 | 4:GAMES/Alex_Higgins_World_Snooker.rom 8 | 5:GAMES/Anarchy.rom 9 | 6:GAMES/Android_one.rom 10 | 7:GAMES/Arkanoid.rom 11 | 8:GAMES/Astro_Attack.rom 12 | 9:GAMES/Atlantis.rom 13 | 10:GAMES/AXIENS.ROM 14 | 11:picorom.rom 15 | -------------------------------------------------------------------------------- /src/latch.pio: -------------------------------------------------------------------------------- 1 | .program latch 2 | 3 | ; Autopush must be enabled, with a threshold of 8. 4 | wait 0 gpio 27 ; Wait write latch signal 5 | in pins, 8 ; get the data bits 6 | wait 1 gpio 27 7 | 8 | % c-sdk { 9 | #include "hardware/clocks.h" 10 | #include "hardware/gpio.h" 11 | 12 | static inline void latch_program_init(PIO pio, uint sm, uint offset) { 13 | pio_sm_config c = latch_program_get_default_config(offset); 14 | sm_config_set_in_pins(&c, 14); // data bus 15 | // Shift to left, autopush enabled 16 | sm_config_set_in_shift(&c, false, true, 8); 17 | sm_config_set_clkdiv(&c, 1); 18 | sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX); 19 | pio_sm_init(pio, sm, offset, &c); 20 | pio_sm_set_enabled(pio, sm, true); 21 | } 22 | %} -------------------------------------------------------------------------------- /src/flash.h: -------------------------------------------------------------------------------- 1 | #ifndef _FLASH_H_ 2 | #define _FLASH_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #ifndef FLASH_DEBUG 13 | #define FLASH_DEBUG 0 14 | #endif 15 | 16 | extern uint32_t __DRIVE_START[]; 17 | extern uint32_t __DRIVE_LEN[]; 18 | extern uint32_t __DRIVE_END[]; 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | bool flash_format(); 24 | bool flash_init(); 25 | bool flash_read(int block, uint8_t *buffer); 26 | bool flash_write(int block, const uint8_t *buffer); 27 | uint16_t get_lba_count(); 28 | uint16_t get_lba_size(); 29 | void flash_persist(); 30 | void flash_trim(int); 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | #endif 35 | -------------------------------------------------------------------------------- /src/fatfs/source/00readme.txt: -------------------------------------------------------------------------------- 1 | FatFs Module Source Files R0.15 2 | 3 | 4 | FILES 5 | 6 | 00readme.txt This file. 7 | 00history.txt Revision history. 8 | ff.c FatFs module. 9 | ffconf.h Configuration file of FatFs module. 10 | ff.h Common include file for FatFs and application module. 11 | diskio.h Common include file for FatFs and disk I/O module. 12 | diskio.c An example of glue function to attach existing disk I/O module to FatFs. 13 | ffunicode.c Optional Unicode utility functions. 14 | ffsystem.c An example of optional O/S related functions. 15 | 16 | 17 | Low level disk I/O module is not included in this archive because the FatFs 18 | module is only a generic file system layer and it does not depend on any specific 19 | storage device. You need to provide a low level disk I/O module written to 20 | control the storage device that attached to the target system. 21 | 22 | -------------------------------------------------------------------------------- /src/bootsel_button.h: -------------------------------------------------------------------------------- 1 | #ifndef _BOOTSEL_BUTTON_H 2 | #define _BOOTSEL_BUTTON_H 3 | 4 | #include "pico/stdlib.h" 5 | #include "hardware/gpio.h" 6 | #include "hardware/sync.h" 7 | #include "hardware/structs/ioqspi.h" 8 | #include "hardware/structs/sio.h" 9 | 10 | 11 | bool __no_inline_not_in_flash_func(bb_get_bootsel_button)() { 12 | const uint CS_PIN_INDEX = 1; 13 | uint32_t flags = save_and_disable_interrupts(); 14 | hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl, 15 | GPIO_OVERRIDE_LOW << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB, 16 | IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS); 17 | for (volatile int i = 0; i < 1000; ++i); 18 | bool button_state = !(sio_hw->gpio_hi_in & (1u << CS_PIN_INDEX)); 19 | hw_write_masked(&ioqspi_hw->io[CS_PIN_INDEX].ctrl, 20 | GPIO_OVERRIDE_NORMAL << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB, 21 | IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS); 22 | restore_interrupts(flags); 23 | 24 | return button_state; 25 | } 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /.vscode/c_cpp_properties.json: -------------------------------------------------------------------------------- 1 | { 2 | "configurations": [ 3 | { 4 | "name": "Linux", 5 | "includePath": [ 6 | "${workspaceFolder}/**" 7 | ], 8 | "defines": [], 9 | "compilerPath": "/usr/bin/gcc", 10 | "cStandard": "c17", 11 | "cppStandard": "gnu++17", 12 | "intelliSenseMode": "linux-gcc-x64", 13 | "compileCommands": "${workspaceFolder}/build/compile_commands.json", 14 | "configurationProvider": "ms-vscode.cmake-tools" 15 | }, 16 | { 17 | "name": "Mac", 18 | "includePath": [ 19 | "${workspaceFolder}/**" 20 | ], 21 | "defines": [], 22 | "compilerPath": "/usr/bin/gcc", 23 | "cStandard": "c17", 24 | "cppStandard": "gnu++17", 25 | "intelliSenseMode": "linux-gcc-x64", 26 | "compileCommands": "${workspaceFolder}/build/compile_commands.json", 27 | "configurationProvider": "ms-vscode.cmake-tools" 28 | } 29 | ], 30 | "version": 4 31 | } -------------------------------------------------------------------------------- /licence.md: -------------------------------------------------------------------------------- 1 | Copyright 2023 Matt Callow 2 | 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 5 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 | 7 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | 9 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /src/flash.cpp: -------------------------------------------------------------------------------- 1 | #include "flash.h" 2 | #include 3 | #include 4 | 5 | FlashInterfaceRP2040_SDK fi( (uint8_t *)__DRIVE_START, (uint8_t *)__DRIVE_END); 6 | SPIFTL ftl(&fi); 7 | 8 | 9 | // e.g. https://github.com/earlephilhower/SPIFTL 10 | 11 | bool flash_format() { 12 | #if FLASH_DEBUG 13 | printf("flash_format()\n"); 14 | #endif 15 | return ftl.format(); 16 | } 17 | 18 | bool flash_init() { 19 | static bool init_done = false; 20 | #if FLASH_DEBUG 21 | printf("flash_init() init_done=%d\n", init_done); 22 | #endif 23 | if (init_done) { 24 | return init_done; 25 | } 26 | init_done = ftl.start(); 27 | return init_done; 28 | } 29 | bool flash_read(int block, uint8_t *buffer) { 30 | #if FLASH_DEBUG 31 | printf("flash_read(%d, buffer)\n", block); 32 | #endif 33 | return ftl.read(block, buffer); 34 | } 35 | 36 | bool flash_write(int block, const uint8_t *buffer) { 37 | #if FLASH_DEBUG 38 | printf("flash_write(%d, buffer)\n", block); 39 | #endif 40 | return ftl.write(block, buffer); 41 | } 42 | 43 | void flash_persist() { 44 | #if FLASH_DEBUG 45 | printf("flash_persist()\n"); 46 | #endif 47 | ftl.persist(); 48 | } 49 | 50 | void flash_trim(int lba) { 51 | #if FLASH_DEBUG 52 | printf("flash_trim(%d)\n", lba); 53 | #endif 54 | ftl.trim(lba); 55 | } 56 | 57 | uint16_t get_lba_count() { 58 | return ftl.lbaCount(); 59 | } 60 | 61 | uint16_t get_lba_size() { 62 | return ftl.lbaBytes; 63 | } -------------------------------------------------------------------------------- /src/fatfs_driver.c: -------------------------------------------------------------------------------- 1 | #include "ff.h" 2 | #include "diskio.h" 3 | #include "flash.h" 4 | 5 | 6 | DSTATUS disk_status(BYTE drv) { 7 | return RES_OK; 8 | } 9 | 10 | DSTATUS disk_initialize(BYTE drv) { 11 | if (flash_init()) return RES_OK; 12 | return RES_ERROR; 13 | } 14 | 15 | DRESULT disk_read(BYTE drv, BYTE *buff, LBA_t sector, UINT count) { 16 | if (sector > get_lba_count()) { 17 | return RES_ERROR; 18 | } 19 | for (unsigned int i = 0; i < count; i++) { 20 | flash_read(sector + i, buff + i * get_lba_size()); 21 | } 22 | return RES_OK; 23 | } 24 | 25 | DRESULT disk_write(BYTE drv, const BYTE *buff, LBA_t sector, UINT count) { 26 | if (sector > get_lba_count()) { 27 | return RES_ERROR; 28 | } 29 | for (unsigned int i = 0; i < count; i++) { 30 | flash_write(sector + i, buff + i * get_lba_size()); 31 | } 32 | return RES_OK; 33 | } 34 | 35 | DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff) { 36 | if (ctrl == GET_SECTOR_COUNT) { 37 | *(LBA_t *)buff = get_lba_count(); 38 | return RES_OK; 39 | } 40 | if (ctrl == GET_BLOCK_SIZE) { 41 | *(DWORD *)buff = get_lba_size(); 42 | return RES_OK; 43 | } 44 | if (ctrl == CTRL_SYNC) { 45 | flash_persist(); 46 | return RES_OK; 47 | } 48 | if (ctrl == GET_SECTOR_SIZE) { 49 | *(WORD *)buff = get_lba_size(); 50 | return RES_OK; 51 | } 52 | if (ctrl == CTRL_TRIM) { 53 | LBA_t *lba = (LBA_t *)buff; 54 | for (unsigned int i = lba[0]; i < lba[1]; i++) { 55 | flash_trim(i); 56 | } 57 | return RES_OK; 58 | } 59 | return RES_PARERR; 60 | } 61 | 62 | DWORD get_fattime (void) { 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /src/fatfs/LICENSE.txt: -------------------------------------------------------------------------------- 1 | FatFs License 2 | 3 | FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files. 4 | 5 | /*----------------------------------------------------------------------------/ 6 | / FatFs - Generic FAT Filesystem Module Rx.xx / 7 | /-----------------------------------------------------------------------------/ 8 | / 9 | / Copyright (C) 20xx, ChaN, all right reserved. 10 | / 11 | / FatFs module is an open source software. Redistribution and use of FatFs in 12 | / source and binary forms, with or without modification, are permitted provided 13 | / that the following condition is met: 14 | / 15 | / 1. Redistributions of source code must retain the above copyright notice, 16 | / this condition and the following disclaimer. 17 | / 18 | / This software is provided by the copyright holder and contributors "AS IS" 19 | / and any warranties related to this software are DISCLAIMED. 20 | / The copyright owner or contributors be NOT LIABLE for any damages caused 21 | / by use of this software. 22 | /----------------------------------------------------------------------------*/ 23 | 24 | Therefore FatFs license is one of the BSD-style licenses, but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, do not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses include GNU GPL. When you redistribute the FatFs source code with changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license. 25 | -------------------------------------------------------------------------------- /src/fatfs/source/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2019 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* Status of Disk Functions */ 13 | typedef BYTE DSTATUS; 14 | 15 | /* Results of Disk Functions */ 16 | typedef enum { 17 | RES_OK = 0, /* 0: Successful */ 18 | RES_ERROR, /* 1: R/W Error */ 19 | RES_WRPRT, /* 2: Write Protected */ 20 | RES_NOTRDY, /* 3: Not Ready */ 21 | RES_PARERR /* 4: Invalid Parameter */ 22 | } DRESULT; 23 | 24 | 25 | /*---------------------------------------*/ 26 | /* Prototypes for disk control functions */ 27 | 28 | 29 | DSTATUS disk_initialize (BYTE pdrv); 30 | DSTATUS disk_status (BYTE pdrv); 31 | DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); 32 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); 33 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 34 | 35 | 36 | /* Disk Status Bits (DSTATUS) */ 37 | 38 | #define STA_NOINIT 0x01 /* Drive not initialized */ 39 | #define STA_NODISK 0x02 /* No medium in the drive */ 40 | #define STA_PROTECT 0x04 /* Write protected */ 41 | 42 | 43 | /* Command code for disk_ioctrl fucntion */ 44 | 45 | /* Generic command (Used by FatFs) */ 46 | #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ 47 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ 48 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ 49 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ 50 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ 51 | 52 | /* Generic command (Not used by FatFs) */ 53 | #define CTRL_POWER 5 /* Get/Set power status */ 54 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 55 | #define CTRL_EJECT 7 /* Eject media */ 56 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 57 | 58 | /* MMC/SDC specific ioctl command */ 59 | #define MMC_GET_TYPE 10 /* Get card type */ 60 | #define MMC_GET_CSD 11 /* Get CSD */ 61 | #define MMC_GET_CID 12 /* Get CID */ 62 | #define MMC_GET_OCR 13 /* Get OCR */ 63 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 64 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 65 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 66 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 67 | 68 | /* ATA/CF specific ioctl command */ 69 | #define ATA_GET_REV 20 /* Get F/W revision */ 70 | #define ATA_GET_MODEL 21 /* Get model name */ 71 | #define ATA_GET_SN 22 /* Get serial number */ 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /src/FlashInterfaceRP2040_SDK.h: -------------------------------------------------------------------------------- 1 | /* 2 | FlashInterfaceRP2040.h - Flash interface for RP2040 arduino-pico 3 | 4 | Copyright (c) 2024 Earle F. Philhower, III 5 | 6 | This library is free software; you can redistribute it and/or 7 | modify it under the terms of the GNU Lesser General Public 8 | License as published by the Free Software Foundation; either 9 | version 3 of the License, or (at your option) any later version. 10 | 11 | This library is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public 17 | License along with this program. If not, see https://www.gnu.org/licenses/ 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "FlashInterface.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | class FlashInterfaceRP2040_SDK : public FlashInterface { 33 | public: 34 | FlashInterfaceRP2040_SDK(const uint8_t *start, const uint8_t *end) { 35 | _flashSize = end - start; 36 | _flash = start; 37 | } 38 | 39 | virtual ~FlashInterfaceRP2040_SDK() override { 40 | } 41 | 42 | virtual int size() override { 43 | return _flashSize; 44 | } 45 | 46 | virtual int writeBufferSize() override { 47 | // Limitation of the SDK/HW, writes must be 256b or larger 48 | return 256; 49 | } 50 | 51 | virtual const uint8_t *readEB(int eb) override { 52 | return &_flash[eb * ebBytes]; 53 | } 54 | 55 | virtual bool eraseBlock(int eb) override { 56 | if (eb < _flashSize / ebBytes) { 57 | const uint8_t *addr = _flash + (eb * ebBytes); 58 | uint32_t ints = save_and_disable_interrupts(); 59 | flash_range_erase((intptr_t)addr - (intptr_t)XIP_BASE, ebBytes); 60 | restore_interrupts(ints); 61 | return true; 62 | } 63 | return false; 64 | } 65 | 66 | virtual bool program(int eb, int offset, const void *data, int size) override { 67 | if (eb < _flashSize / ebBytes) { 68 | const uint8_t *addr = _flash + (eb * ebBytes + offset); 69 | uint32_t ints = save_and_disable_interrupts(); 70 | flash_range_program((intptr_t)addr - (intptr_t)XIP_BASE, (const uint8_t *)data, size); 71 | restore_interrupts(ints); 72 | return true; 73 | } 74 | return false; 75 | } 76 | 77 | virtual bool read(int eb, int offset, void *data, int size) override { 78 | if (eb < _flashSize / ebBytes) { 79 | memcpy(data, _flash + (eb * ebBytes + offset), size); 80 | return true; 81 | } 82 | return false; 83 | } 84 | 85 | private: 86 | const int ebBytes = 4096; 87 | int _flashSize; 88 | const uint8_t *_flash; 89 | }; 90 | -------------------------------------------------------------------------------- /src/pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | # GIT_SUBMODULES_RECURSE was added in 3.17 33 | if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0") 34 | FetchContent_Declare( 35 | pico_sdk 36 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 37 | GIT_TAG master 38 | GIT_SUBMODULES_RECURSE FALSE 39 | ) 40 | else () 41 | FetchContent_Declare( 42 | pico_sdk 43 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 44 | GIT_TAG master 45 | ) 46 | endif () 47 | 48 | if (NOT pico_sdk) 49 | message("Downloading Raspberry Pi Pico SDK") 50 | FetchContent_Populate(pico_sdk) 51 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 52 | endif () 53 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 54 | else () 55 | message(FATAL_ERROR 56 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 57 | ) 58 | endif () 59 | endif () 60 | 61 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 62 | if (NOT EXISTS ${PICO_SDK_PATH}) 63 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 64 | endif () 65 | 66 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 67 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 68 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 69 | endif () 70 | 71 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 72 | 73 | include(${PICO_SDK_INIT_CMAKE_FILE}) 74 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | 3 | 4 | # initialize the SDK based on PICO_SDK_PATH 5 | # note: this must happen before project() 6 | include(pico_sdk_import.cmake) 7 | 8 | project(cpc_rom) 9 | 10 | set(PICO_BOARD pico) 11 | set(FAMILY rp2040) 12 | 13 | # initialize the Raspberry Pi Pico SDK 14 | pico_sdk_init() 15 | 16 | 17 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DPICO_USE_MALLOC_MUTEX=1") 18 | # rest of your project 19 | # set(PICO_DEFAULT_BINARY_TYPE copy_to_ram) 20 | 21 | foreach(target cpc_rom_emulator) # cpc_rom_emulator_200 cpc_rom_emulator_210 cpc_rom_emulator_220 cpc_rom_emulator_230 cpc_rom_emulator_240 cpc_rom_emulator_250 cpc_rom_emulator_260 cpc_rom_emulator_270) 22 | add_executable(${target} 23 | main.c 24 | fatfs_driver.c 25 | flash.cpp 26 | usb_msc_driver.c 27 | usb_descriptors.c 28 | ${PICO_SDK_PATH}/lib/tinyusb/src/tusb.c 29 | fatfs/source/ff.c 30 | fatfs/source/ffsystem.c 31 | fatfs/source/ffunicode.c 32 | ) 33 | pico_set_linker_script(${target} ${CMAKE_SOURCE_DIR}/memmap_custom.ld) 34 | pico_generate_pio_header(${target} ${CMAKE_CURRENT_LIST_DIR}/latch.pio) 35 | 36 | target_include_directories(${target} PUBLIC 37 | fatfs/source 38 | SPIFTL 39 | ${CMAKE_CURRENT_LIST_DIR} 40 | ) 41 | 42 | # Add pico_stdlib library which aggregates commonly used features 43 | target_link_libraries(${target} 44 | pico_stdlib 45 | pico_multicore 46 | pico_bootrom 47 | hardware_pio 48 | hardware_dma 49 | tinyusb_additions 50 | tinyusb_board 51 | tinyusb_device 52 | hardware_flash 53 | hardware_sync 54 | ) 55 | 56 | pico_enable_stdio_usb(${target} 1) 57 | pico_enable_stdio_uart(${target} 0) 58 | 59 | # create map/bin/hex/uf2 file in addition to ELF. 60 | pico_add_extra_outputs(${target}) 61 | 62 | # pico_define_boot_stage2(slower_boot2 ${PICO_DEFAULT_BOOT_STAGE2_FILE}) 63 | # target_compile_definitions(slower_boot2 PRIVATE PICO_FLASH_SPI_CLKDIV=4) 64 | # pico_set_boot_stage2(cpc_rom_emulator slower_boot2) 65 | 66 | # Make a copy of the firmware file 67 | add_custom_command(TARGET ${target} POST_BUILD 68 | COMMAND ${CMAKE_COMMAND} -E copy ${target}.uf2 ../firmware/${target}.uf2 69 | ) 70 | 71 | endforeach() 72 | 73 | #target_compile_definitions(cpc_rom_emulator_200 PRIVATE CLOCK_SPEED_KHZ=200000) 74 | #target_compile_definitions(cpc_rom_emulator_210 PRIVATE CLOCK_SPEED_KHZ=210000) 75 | #target_compile_definitions(cpc_rom_emulator_220 PRIVATE CLOCK_SPEED_KHZ=220000) 76 | #target_compile_definitions(cpc_rom_emulator_230 PRIVATE CLOCK_SPEED_KHZ=230000) 77 | #target_compile_definitions(cpc_rom_emulator_240 PRIVATE CLOCK_SPEED_KHZ=240000) 78 | #target_compile_definitions(cpc_rom_emulator_250 PRIVATE CLOCK_SPEED_KHZ=250000) 79 | #target_compile_definitions(cpc_rom_emulator_260 PRIVATE CLOCK_SPEED_KHZ=260000) 80 | #target_compile_definitions(cpc_rom_emulator_270 PRIVATE CLOCK_SPEED_KHZ=270000) 81 | 82 | 83 | add_custom_command(OUTPUT "picorom.rom" picorom.map 84 | COMMAND z88dk.z88dk-z80asm 85 | -v -s -l -m -b -mz80 86 | -o../../build/picorom.rom 87 | picorom.s 88 | WORKING_DIRECTORY "../src/z80" 89 | DEPENDS z80/picorom.s 90 | ) 91 | 92 | add_custom_command(OUTPUT "../firmware/picorom.rom" 93 | COMMAND cp picorom.rom ../firmware/picorom.rom 94 | DEPENDS picorom.rom 95 | ) 96 | 97 | add_custom_target(generate_rom ALL 98 | DEPENDS "../firmware/picorom.rom") 99 | 100 | add_compile_definitions( 101 | FTL_DEBUG=0 102 | FLASH_DEBUG=0 103 | MSC_DRIVER_DEBUG=0 104 | ) 105 | -------------------------------------------------------------------------------- /src/tusb_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | #ifndef _TUSB_CONFIG_H_ 27 | #define _TUSB_CONFIG_H_ 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | //--------------------------------------------------------------------+ 34 | // Board Specific Configuration 35 | //--------------------------------------------------------------------+ 36 | 37 | // RHPort number used for device can be defined by board.mk, default to port 0 38 | #ifndef BOARD_TUD_RHPORT 39 | #define BOARD_TUD_RHPORT 0 40 | #endif 41 | 42 | // RHPort max operational speed can defined by board.mk 43 | #ifndef BOARD_TUD_MAX_SPEED 44 | #define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED 45 | #endif 46 | 47 | //-------------------------------------------------------------------- 48 | // Common Configuration 49 | //-------------------------------------------------------------------- 50 | 51 | // defined by compiler flags for flexibility 52 | #ifndef CFG_TUSB_MCU 53 | #error CFG_TUSB_MCU must be defined 54 | #endif 55 | 56 | #ifndef CFG_TUSB_OS 57 | #define CFG_TUSB_OS OPT_OS_NONE 58 | #endif 59 | 60 | #ifndef CFG_TUSB_DEBUG 61 | #define CFG_TUSB_DEBUG 0 62 | #endif 63 | 64 | // Enable Device stack 65 | #define CFG_TUD_ENABLED 1 66 | 67 | // Default is max speed that hardware controller could support with on-chip PHY 68 | #define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED 69 | 70 | /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. 71 | * Tinyusb use follows macros to declare transferring memory so that they can be put 72 | * into those specific section. 73 | * e.g 74 | * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) 75 | * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) 76 | */ 77 | #ifndef CFG_TUSB_MEM_SECTION 78 | #define CFG_TUSB_MEM_SECTION 79 | #endif 80 | 81 | #ifndef CFG_TUSB_MEM_ALIGN 82 | #define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) 83 | #endif 84 | 85 | //-------------------------------------------------------------------- 86 | // DEVICE CONFIGURATION 87 | //-------------------------------------------------------------------- 88 | 89 | #ifndef CFG_TUD_ENDPOINT0_SIZE 90 | #define CFG_TUD_ENDPOINT0_SIZE 64 91 | #endif 92 | 93 | //------------- CLASS -------------// 94 | #define CFG_TUD_CDC 1 95 | #define CFG_TUD_MSC 1 96 | #define CFG_TUD_HID 0 97 | #define CFG_TUD_MIDI 0 98 | #define CFG_TUD_VENDOR 0 99 | 100 | // CDC FIFO size of TX and RX 101 | #define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 102 | #define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 103 | 104 | // CDC Endpoint transfer buffer size, more is faster 105 | #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 106 | 107 | // MSC Buffer size of Device Mass storage 108 | #define CFG_TUD_MSC_EP_BUFSIZE 512 109 | 110 | #ifdef __cplusplus 111 | } 112 | #endif 113 | 114 | #endif /* _TUSB_CONFIG_H_ */ 115 | -------------------------------------------------------------------------------- /src/fatfs/source/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */ 3 | /*-----------------------------------------------------------------------*/ 4 | /* If a working storage control module is available, it should be */ 5 | /* attached to the FatFs via a glue function rather than modifying it. */ 6 | /* This is an example of glue functions to attach various exsisting */ 7 | /* storage control modules to the FatFs module with a defined API. */ 8 | /*-----------------------------------------------------------------------*/ 9 | 10 | #include "ff.h" /* Obtains integer types */ 11 | #include "diskio.h" /* Declarations of disk functions */ 12 | 13 | /* Definitions of physical drive number for each drive */ 14 | #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */ 15 | #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */ 16 | #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */ 17 | 18 | 19 | /*-----------------------------------------------------------------------*/ 20 | /* Get Drive Status */ 21 | /*-----------------------------------------------------------------------*/ 22 | 23 | DSTATUS disk_status ( 24 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 25 | ) 26 | { 27 | DSTATUS stat; 28 | int result; 29 | 30 | switch (pdrv) { 31 | case DEV_RAM : 32 | result = RAM_disk_status(); 33 | 34 | // translate the reslut code here 35 | 36 | return stat; 37 | 38 | case DEV_MMC : 39 | result = MMC_disk_status(); 40 | 41 | // translate the reslut code here 42 | 43 | return stat; 44 | 45 | case DEV_USB : 46 | result = USB_disk_status(); 47 | 48 | // translate the reslut code here 49 | 50 | return stat; 51 | } 52 | return STA_NOINIT; 53 | } 54 | 55 | 56 | 57 | /*-----------------------------------------------------------------------*/ 58 | /* Inidialize a Drive */ 59 | /*-----------------------------------------------------------------------*/ 60 | 61 | DSTATUS disk_initialize ( 62 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 63 | ) 64 | { 65 | DSTATUS stat; 66 | int result; 67 | 68 | switch (pdrv) { 69 | case DEV_RAM : 70 | result = RAM_disk_initialize(); 71 | 72 | // translate the reslut code here 73 | 74 | return stat; 75 | 76 | case DEV_MMC : 77 | result = MMC_disk_initialize(); 78 | 79 | // translate the reslut code here 80 | 81 | return stat; 82 | 83 | case DEV_USB : 84 | result = USB_disk_initialize(); 85 | 86 | // translate the reslut code here 87 | 88 | return stat; 89 | } 90 | return STA_NOINIT; 91 | } 92 | 93 | 94 | 95 | /*-----------------------------------------------------------------------*/ 96 | /* Read Sector(s) */ 97 | /*-----------------------------------------------------------------------*/ 98 | 99 | DRESULT disk_read ( 100 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 101 | BYTE *buff, /* Data buffer to store read data */ 102 | LBA_t sector, /* Start sector in LBA */ 103 | UINT count /* Number of sectors to read */ 104 | ) 105 | { 106 | DRESULT res; 107 | int result; 108 | 109 | switch (pdrv) { 110 | case DEV_RAM : 111 | // translate the arguments here 112 | 113 | result = RAM_disk_read(buff, sector, count); 114 | 115 | // translate the reslut code here 116 | 117 | return res; 118 | 119 | case DEV_MMC : 120 | // translate the arguments here 121 | 122 | result = MMC_disk_read(buff, sector, count); 123 | 124 | // translate the reslut code here 125 | 126 | return res; 127 | 128 | case DEV_USB : 129 | // translate the arguments here 130 | 131 | result = USB_disk_read(buff, sector, count); 132 | 133 | // translate the reslut code here 134 | 135 | return res; 136 | } 137 | 138 | return RES_PARERR; 139 | } 140 | 141 | 142 | 143 | /*-----------------------------------------------------------------------*/ 144 | /* Write Sector(s) */ 145 | /*-----------------------------------------------------------------------*/ 146 | 147 | #if FF_FS_READONLY == 0 148 | 149 | DRESULT disk_write ( 150 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 151 | const BYTE *buff, /* Data to be written */ 152 | LBA_t sector, /* Start sector in LBA */ 153 | UINT count /* Number of sectors to write */ 154 | ) 155 | { 156 | DRESULT res; 157 | int result; 158 | 159 | switch (pdrv) { 160 | case DEV_RAM : 161 | // translate the arguments here 162 | 163 | result = RAM_disk_write(buff, sector, count); 164 | 165 | // translate the reslut code here 166 | 167 | return res; 168 | 169 | case DEV_MMC : 170 | // translate the arguments here 171 | 172 | result = MMC_disk_write(buff, sector, count); 173 | 174 | // translate the reslut code here 175 | 176 | return res; 177 | 178 | case DEV_USB : 179 | // translate the arguments here 180 | 181 | result = USB_disk_write(buff, sector, count); 182 | 183 | // translate the reslut code here 184 | 185 | return res; 186 | } 187 | 188 | return RES_PARERR; 189 | } 190 | 191 | #endif 192 | 193 | 194 | /*-----------------------------------------------------------------------*/ 195 | /* Miscellaneous Functions */ 196 | /*-----------------------------------------------------------------------*/ 197 | 198 | DRESULT disk_ioctl ( 199 | BYTE pdrv, /* Physical drive nmuber (0..) */ 200 | BYTE cmd, /* Control code */ 201 | void *buff /* Buffer to send/receive control data */ 202 | ) 203 | { 204 | DRESULT res; 205 | int result; 206 | 207 | switch (pdrv) { 208 | case DEV_RAM : 209 | 210 | // Process of the command for the RAM drive 211 | 212 | return res; 213 | 214 | case DEV_MMC : 215 | 216 | // Process of the command for the MMC/SD card 217 | 218 | return res; 219 | 220 | case DEV_USB : 221 | 222 | // Process of the command the USB drive 223 | 224 | return res; 225 | } 226 | 227 | return RES_PARERR; 228 | } 229 | 230 | -------------------------------------------------------------------------------- /src/z80/picorom.s: -------------------------------------------------------------------------------- 1 | ; Pico ROM control ROM 2 | ; 3 | ver_major EQU 3 4 | ver_minor EQU 1 5 | ver_patch EQU 1 6 | TXT_OUTPUT: EQU $BB5A 7 | KM_WAIT_KEY: EQU $BB18 8 | IO_PORT: EQU $DFFC 9 | 10 | CMD_PICOLOAD EQU $FF 11 | CMD_LED: EQU $FE 12 | CMD_ROMDIR1 EQU $FD 13 | CMD_ROMDIR2 EQU $FC 14 | CMD_ROMLIST1 EQU $FB 15 | CMD_ROMLIST2 EQU $FA 16 | CMD_ROMIN: EQU $F9 17 | CMD_ROMOUT: EQU $F8 18 | CMD_ROMSET: EQU $F7 19 | 20 | org $c000 21 | defb 1 ; background rom 22 | defb ver_major 23 | defb ver_minor 24 | defb ver_patch 25 | defw NAME_TABLE 26 | jp INIT 27 | jp BOOT 28 | jp LED 29 | jp ROMSET 30 | jp ROMDIR 31 | jp ROMLIST 32 | jp ROMOUT 33 | jp ROMIN 34 | 35 | NAME_TABLE: 36 | defm "PICO RO",'M'+128 37 | defm "PUS", 'B'+128 38 | defm "LE", 'D'+128 39 | defm "ROMSE", 'T'+128 40 | defm "PDI", 'R'+128 41 | defm "ROM", 'S'+128 42 | defm "ROMOU", 'T'+128 43 | defm "ROMI", 'N'+128 44 | defb 0 45 | INIT: 46 | push HL 47 | ld HL, START_MSG 48 | call disp_str 49 | pop HL 50 | ret 51 | 52 | START_MSG: 53 | defm " Pico ROM v",'0'+ver_major,'.','0'+ver_minor,'0'+ver_patch,0x0d,0x0a,0x0d,0x0a,0x00 54 | 55 | ; handle a single parameter command 56 | MACRO CMD_1P cmd, invalid_msg 57 | LOCAL wait, invalid 58 | cp a, 1 ; num params 59 | jp nz, invalid 60 | ld hl, RESP_BUF 61 | ld a, (hl) ; get current sequence number in A 62 | ld BC, IO_PORT 63 | out (c), c 64 | ld c, cmd 65 | out (c),c 66 | ld c,(IX) ; param 67 | out (c), c 68 | .wait 69 | cp (hl) ; wait for the sequence number to be updated 70 | jr z,wait 71 | ret 72 | .invalid 73 | ld hl, invalid_msg 74 | call disp_str 75 | ret 76 | ENDM 77 | 78 | ; 79 | MACRO CMD_0P_NOWAIT cmd 80 | ld BC, IO_PORT ; command prefix 81 | out (c), c 82 | ld c, cmd ; command byte 83 | out (c), c 84 | ret 85 | ENDM 86 | 87 | MACRO LIST_COMMAND cmd1, cmd2 88 | LOCAL wait, done ,nokey 89 | ld d, 22 ; number of lines to display 90 | ld hl, RESP_BUF 91 | ld a, (hl) ; get current sequence number in A 92 | ld BC, IO_PORT ; command prefix 93 | out (c), c 94 | ld C, cmd1 ; command byte 95 | out (c), c 96 | .wait 97 | cp (hl) ; wait for the sequence number to be updated 98 | jr z, wait 99 | inc hl ; point to status code 100 | ld a, (hl) 101 | or a 102 | jr nz, done 103 | dec d 104 | jr nz, nokey 105 | push hl 106 | ld hl, KEY_MSG 107 | call disp_str 108 | pop hl 109 | call KM_WAIT_KEY 110 | ld d,22 111 | .nokey 112 | inc hl ; skip data type # FIXME 113 | inc hl ; point to start of response 114 | call disp_str 115 | call cr_nl 116 | ; get next 117 | ld hl, RESP_BUF 118 | ld a, (hl) ; get current sequence number in A 119 | ld BC, IO_PORT ; command prefix 120 | out (c), c 121 | ld C, cmd2 ; command byte 122 | out (c), c 123 | jr wait ; wait for next command to complete 124 | .done 125 | call cr_nl 126 | ret 127 | ENDM 128 | 129 | KEY_MSG: defm 0x0d,0x0a,"*** Press any key ***",0x0d,0x0a,0x0d,0x0a,0x00 130 | 131 | 132 | IP_MSG: 133 | defm "Invalid parameters",0x0d,0x0a,0x0d,0x0a,0x00 134 | 135 | 136 | LED: CMD_1P CMD_LED, IP_MSG 137 | BOOT: CMD_0P_NOWAIT CMD_PICOLOAD 138 | ROMDIR: LIST_COMMAND CMD_ROMDIR1, CMD_ROMDIR2 139 | ROMLIST: LIST_COMMAND CMD_ROMLIST1, CMD_ROMLIST2 140 | 141 | MACRO WAIT_FOR_COMPLETION 142 | ; wait for command to finish. Last seq value in A 143 | ; display the result 144 | LOCAL wait 145 | ld hl, RESP_BUF 146 | .wait 147 | cp (hl) ; wait for the sequence number to be updated 148 | jr z, wait 149 | inc hl ; skip status code # FIXME 150 | inc hl ; skip data type # FIXME 151 | inc hl ; point to start of response 152 | call disp_str 153 | call cr_nl 154 | ENDM 155 | 156 | ROMIN: 157 | cp 2 158 | jr nz, RI_USAGE 159 | 160 | LD L,(IX+0) 161 | LD H,(IX+1) ; HL = string descriptor 162 | 163 | ld A,(HL) ; length 164 | cp 0 165 | jr z, RI_DONE ; no file given 166 | 167 | ld BC, IO_PORT ; command prefix 168 | out (c), c 169 | ld C, CMD_ROMIN 170 | out (c), c 171 | ld C, (IX+2) ; slot number 172 | out (c), c 173 | 174 | ld c, (HL) ; get length in C 175 | out (c), c ; and send it 176 | inc HL 177 | ld E,(HL) 178 | inc HL 179 | ld D,(HL) 180 | ex DE, HL ; string address now in HL 181 | ld de, RESP_BUF 182 | ld d, (de) ; save current sequence number in D 183 | ; send filename - length is in A 184 | RI_LOOP: 185 | ld c,(HL) 186 | out (c),C 187 | inc HL 188 | dec A 189 | jr nz, RI_LOOP 190 | 191 | ; wait for completion 192 | ld a,d ; get current seqeuence number 193 | WAIT_FOR_COMPLETION 194 | jr RI_DONE 195 | RI_USAGE: 196 | ld hl, RI_U_MSG 197 | call disp_str 198 | RI_DONE: 199 | ret 200 | RI_U_MSG: 201 | defm " Usage |ROMIN,,",0x0d,0x0a,0x0d,0x0a,0x00 202 | 203 | ROMOUT: CMD_1P CMD_ROMOUT, RO_U_MSG 204 | RO_U_MSG: 205 | defm " Usage |ROMOUT,",0x0d,0x0a,0x0d,0x0a,0x00 206 | 207 | 208 | ROMSET: ; load a romset from file 209 | cp 1 210 | jr nz, RS_USAGE 211 | LD L,(IX+0) 212 | LD H,(IX+1) ; HL = string descriptor 213 | 214 | ld A,(HL) ; length 215 | cp 0 216 | jr z, RS_USAGE ; no file given 217 | 218 | ld BC, IO_PORT ; command prefix 219 | out (c), c 220 | ld C, CMD_ROMSET ; command byte 221 | out (c), c 222 | 223 | ld c, (HL) ; length 224 | out (c), c 225 | inc HL 226 | ld E,(HL) 227 | inc HL 228 | ld D,(HL) 229 | ex DE, HL ; string address now in HL 230 | 231 | ld de, RESP_BUF 232 | ld d, (de) ; save current sequence number in D 233 | 234 | ; send filename - length is in A 235 | RSLOOP: 236 | ld c,(HL) 237 | out (c),C 238 | inc HL 239 | dec A 240 | jr nz, RSLOOP 241 | 242 | ; wait for completion 243 | ld a,d ; get saved seqeuence number 244 | WAIT_FOR_COMPLETION 245 | jr RS_DONE 246 | RS_USAGE: 247 | ld hl, RS_U_MSG 248 | call disp_str 249 | RS_DONE: 250 | ret 251 | RS_U_MSG: 252 | defm " Usage |ROMSET,",0x0d,0x0a,0x0d,0x0a,0x00 253 | 254 | cr_nl: 255 | push af 256 | ld A, 0x0d 257 | call TXT_OUTPUT 258 | ld A, 0x0a 259 | call TXT_OUTPUT 260 | pop af 261 | ret 262 | 263 | disp_str: ; display 0 terminated string, pointed to by HL 264 | push af 265 | disp_str1: 266 | ld A, (HL) 267 | or A 268 | jr z, disp_done 269 | call TXT_OUTPUT 270 | inc HL 271 | jr disp_str1 272 | disp_done: 273 | pop af 274 | ret 275 | END: 276 | DEFS $4000-END-$100 277 | RESP_BUF: 278 | ; Response buffer 279 | ; 0 - sequence number, increased by Pico for each response 280 | ; 1 - status code. 0=OK 281 | ; 2 - data type. 1 = 0 terminated string 282 | ; 3.. data 283 | DEFS $100, 0 284 | -------------------------------------------------------------------------------- /src/usb_msc_driver.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "flash.h" 5 | 6 | #ifndef MSC_DRIVER_DEBUG 7 | #define MSC_DRIVER_DEBUG 0 8 | #endif 9 | // whether host does safe-eject 10 | static bool ejected = false; 11 | 12 | static void fatal(int flashes) { 13 | while(1) { 14 | for (int i=0;i %d\n", bufsize, get_lba_size()); 99 | #endif 100 | // out of ramdisk 101 | if (lba >= get_lba_count()) { 102 | printf("read10 out of ramdisk: lba=%u\n", lba); 103 | return -1; 104 | } 105 | flash_read(lba, buffer); 106 | 107 | return (int32_t) bufsize; 108 | } 109 | 110 | bool tud_msc_is_writable_cb (uint8_t lun) 111 | { 112 | (void) lun; 113 | return true; 114 | } 115 | 116 | // Callback invoked when received WRITE10 command. 117 | // Process data in buffer to disk's storage and return number of written bytes 118 | int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize) 119 | { 120 | (void) lun; 121 | #if MSC_DRIVER_DEBUG 122 | printf("tud_msc_write10_cb(%d, %lu, %lu, buffer, %lu)\n", lun, lba, offset, bufsize); 123 | if (offset != 0) printf("ERROR offset is not 0\n"); 124 | if (bufsize != get_lba_size()) printf("ERROR bufsize mismatch %d <> %d\n", bufsize, get_lba_size()); 125 | #endif 126 | // out of ramdisk 127 | if (lba >= get_lba_count()) { 128 | printf("write10 out of ramdisk: lba=%u\n", lba); 129 | return -1; 130 | } 131 | 132 | flash_write(lba, buffer); 133 | return (int32_t)bufsize; 134 | } 135 | 136 | // Callback invoked when received an SCSI command not in built-in list below 137 | // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE 138 | // - READ10 and WRITE10 has their own callbacks 139 | int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize) 140 | { 141 | const int SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL = 0x1E; 142 | const int SCSI_CMD_START_STOP_UNIT = 0x1B; 143 | const int SCSI_SENSE_ILLEGAL_REQUEST = 0x05; 144 | 145 | // read10 & write10 has their own callback and MUST not be handled here 146 | void const* response = NULL; 147 | int32_t resplen = 0; 148 | // most scsi handled is input 149 | bool in_xfer = true; 150 | scsi_start_stop_unit_t const * start_stop = (scsi_start_stop_unit_t const *) scsi_cmd; 151 | switch (scsi_cmd[0]) { 152 | case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: 153 | // Host is about to read/write etc ... better not to disconnect disk 154 | if (scsi_cmd[4] & 1) { 155 | flash_init(); 156 | } 157 | resplen = 0; 158 | break; 159 | case SCSI_CMD_START_STOP_UNIT: 160 | // Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power 161 | if (!start_stop->start && start_stop->load_eject) { 162 | flash_persist(); 163 | } else if (start_stop->start && start_stop->load_eject) { 164 | flash_init(); 165 | } 166 | resplen = 0; 167 | break; 168 | default: 169 | // Set Sense = Invalid Command Operation 170 | tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); 171 | // negative means error -> tinyusb could stall and/or response with failed status 172 | resplen = -1; 173 | break; 174 | } 175 | 176 | // return resplen must not larger than bufsize 177 | if ( resplen > bufsize ) 178 | resplen = bufsize; 179 | 180 | if (response && (resplen > 0)) { 181 | if(in_xfer) { 182 | memcpy(buffer, response, (size_t)resplen); 183 | } else { 184 | ; // SCSI output 185 | } 186 | } 187 | 188 | return (int32_t)resplen; 189 | } 190 | -------------------------------------------------------------------------------- /src/fatfs/source/ffsystem.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------*/ 2 | /* A Sample Code of User Provided OS Dependent Functions for FatFs */ 3 | /*------------------------------------------------------------------------*/ 4 | 5 | #include "ff.h" 6 | 7 | 8 | #if FF_USE_LFN == 3 /* Use dynamic memory allocation */ 9 | 10 | /*------------------------------------------------------------------------*/ 11 | /* Allocate/Free a Memory Block */ 12 | /*------------------------------------------------------------------------*/ 13 | 14 | #include /* with POSIX API */ 15 | 16 | 17 | void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ 18 | UINT msize /* Number of bytes to allocate */ 19 | ) 20 | { 21 | return malloc((size_t)msize); /* Allocate a new memory block */ 22 | } 23 | 24 | 25 | void ff_memfree ( 26 | void* mblock /* Pointer to the memory block to free (no effect if null) */ 27 | ) 28 | { 29 | free(mblock); /* Free the memory block */ 30 | } 31 | 32 | #endif 33 | 34 | 35 | 36 | 37 | #if FF_FS_REENTRANT /* Mutal exclusion */ 38 | /*------------------------------------------------------------------------*/ 39 | /* Definitions of Mutex */ 40 | /*------------------------------------------------------------------------*/ 41 | 42 | #define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */ 43 | 44 | 45 | #if OS_TYPE == 0 /* Win32 */ 46 | #include 47 | static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ 48 | 49 | #elif OS_TYPE == 1 /* uITRON */ 50 | #include "itron.h" 51 | #include "kernel.h" 52 | static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ 53 | 54 | #elif OS_TYPE == 2 /* uc/OS-II */ 55 | #include "includes.h" 56 | static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */ 57 | 58 | #elif OS_TYPE == 3 /* FreeRTOS */ 59 | #include "FreeRTOS.h" 60 | #include "semphr.h" 61 | static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ 62 | 63 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 64 | #include "cmsis_os.h" 65 | static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ 66 | 67 | #endif 68 | 69 | 70 | 71 | /*------------------------------------------------------------------------*/ 72 | /* Create a Mutex */ 73 | /*------------------------------------------------------------------------*/ 74 | /* This function is called in f_mount function to create a new mutex 75 | / or semaphore for the volume. When a 0 is returned, the f_mount function 76 | / fails with FR_INT_ERR. 77 | */ 78 | 79 | int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */ 80 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 81 | ) 82 | { 83 | #if OS_TYPE == 0 /* Win32 */ 84 | Mutex[vol] = CreateMutex(NULL, FALSE, NULL); 85 | return (int)(Mutex[vol] != INVALID_HANDLE_VALUE); 86 | 87 | #elif OS_TYPE == 1 /* uITRON */ 88 | T_CMTX cmtx = {TA_TPRI,1}; 89 | 90 | Mutex[vol] = acre_mtx(&cmtx); 91 | return (int)(Mutex[vol] > 0); 92 | 93 | #elif OS_TYPE == 2 /* uC/OS-II */ 94 | OS_ERR err; 95 | 96 | Mutex[vol] = OSMutexCreate(0, &err); 97 | return (int)(err == OS_NO_ERR); 98 | 99 | #elif OS_TYPE == 3 /* FreeRTOS */ 100 | Mutex[vol] = xSemaphoreCreateMutex(); 101 | return (int)(Mutex[vol] != NULL); 102 | 103 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 104 | osMutexDef(cmsis_os_mutex); 105 | 106 | Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex)); 107 | return (int)(Mutex[vol] != NULL); 108 | 109 | #endif 110 | } 111 | 112 | 113 | /*------------------------------------------------------------------------*/ 114 | /* Delete a Mutex */ 115 | /*------------------------------------------------------------------------*/ 116 | /* This function is called in f_mount function to delete a mutex or 117 | / semaphore of the volume created with ff_mutex_create function. 118 | */ 119 | 120 | void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */ 121 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 122 | ) 123 | { 124 | #if OS_TYPE == 0 /* Win32 */ 125 | CloseHandle(Mutex[vol]); 126 | 127 | #elif OS_TYPE == 1 /* uITRON */ 128 | del_mtx(Mutex[vol]); 129 | 130 | #elif OS_TYPE == 2 /* uC/OS-II */ 131 | OS_ERR err; 132 | 133 | OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err); 134 | 135 | #elif OS_TYPE == 3 /* FreeRTOS */ 136 | vSemaphoreDelete(Mutex[vol]); 137 | 138 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 139 | osMutexDelete(Mutex[vol]); 140 | 141 | #endif 142 | } 143 | 144 | 145 | /*------------------------------------------------------------------------*/ 146 | /* Request a Grant to Access the Volume */ 147 | /*------------------------------------------------------------------------*/ 148 | /* This function is called on enter file functions to lock the volume. 149 | / When a 0 is returned, the file function fails with FR_TIMEOUT. 150 | */ 151 | 152 | int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */ 153 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 154 | ) 155 | { 156 | #if OS_TYPE == 0 /* Win32 */ 157 | return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0); 158 | 159 | #elif OS_TYPE == 1 /* uITRON */ 160 | return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK); 161 | 162 | #elif OS_TYPE == 2 /* uC/OS-II */ 163 | OS_ERR err; 164 | 165 | OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err)); 166 | return (int)(err == OS_NO_ERR); 167 | 168 | #elif OS_TYPE == 3 /* FreeRTOS */ 169 | return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE); 170 | 171 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 172 | return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK); 173 | 174 | #endif 175 | } 176 | 177 | 178 | 179 | /*------------------------------------------------------------------------*/ 180 | /* Release a Grant to Access the Volume */ 181 | /*------------------------------------------------------------------------*/ 182 | /* This function is called on leave file functions to unlock the volume. 183 | */ 184 | 185 | void ff_mutex_give ( 186 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 187 | ) 188 | { 189 | #if OS_TYPE == 0 /* Win32 */ 190 | ReleaseMutex(Mutex[vol]); 191 | 192 | #elif OS_TYPE == 1 /* uITRON */ 193 | unl_mtx(Mutex[vol]); 194 | 195 | #elif OS_TYPE == 2 /* uC/OS-II */ 196 | OSMutexPost(Mutex[vol]); 197 | 198 | #elif OS_TYPE == 3 /* FreeRTOS */ 199 | xSemaphoreGive(Mutex[vol]); 200 | 201 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 202 | osMutexRelease(Mutex[vol]); 203 | 204 | #endif 205 | } 206 | 207 | #endif /* FF_FS_REENTRANT */ 208 | 209 | -------------------------------------------------------------------------------- /src/memmap_custom.ld: -------------------------------------------------------------------------------- 1 | /* Based on GCC ARM embedded samples. 2 | Defines the following symbols for use by code: 3 | __exidx_start 4 | __exidx_end 5 | __etext 6 | __data_start__ 7 | __preinit_array_start 8 | __preinit_array_end 9 | __init_array_start 10 | __init_array_end 11 | __fini_array_start 12 | __fini_array_end 13 | __data_end__ 14 | __bss_start__ 15 | __bss_end__ 16 | __end__ 17 | end 18 | __HeapLimit 19 | __StackLimit 20 | __StackTop 21 | __stack (== StackTop) 22 | */ 23 | 24 | __DRIVE_LEN = 1536k; 25 | __FLASH_START = 0x10000000; 26 | __FLASH_LEN = 2048k - __DRIVE_LEN; 27 | __DRIVE_START = __FLASH_START + __FLASH_LEN; 28 | __DRIVE_END = __DRIVE_START + __DRIVE_LEN; 29 | 30 | 31 | MEMORY 32 | { 33 | /* FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k */ 34 | FLASH(rx) : ORIGIN = __FLASH_START, LENGTH = __FLASH_LEN 35 | DRIVE(r): ORIGIN = __DRIVE_START, LENGTH = __DRIVE_LEN 36 | RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k 37 | SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k 38 | SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k 39 | } 40 | 41 | ENTRY(_entry_point) 42 | 43 | SECTIONS 44 | { 45 | /* Second stage bootloader is prepended to the image. It must be 256 bytes big 46 | and checksummed. It is usually built by the boot_stage2 target 47 | in the Raspberry Pi Pico SDK 48 | */ 49 | 50 | .flash_begin : { 51 | __flash_binary_start = .; 52 | } > FLASH 53 | 54 | .boot2 : { 55 | __boot2_start__ = .; 56 | KEEP (*(.boot2)) 57 | __boot2_end__ = .; 58 | } > FLASH 59 | 60 | ASSERT(__boot2_end__ - __boot2_start__ == 256, 61 | "ERROR: Pico second stage bootloader must be 256 bytes in size") 62 | 63 | /* The second stage will always enter the image at the start of .text. 64 | The debugger will use the ELF entry point, which is the _entry_point 65 | symbol if present, otherwise defaults to start of .text. 66 | This can be used to transfer control back to the bootrom on debugger 67 | launches only, to perform proper flash setup. 68 | */ 69 | 70 | .text : { 71 | __logical_binary_start = .; 72 | KEEP (*(.vectors)) 73 | KEEP (*(.binary_info_header)) 74 | __binary_info_header_end = .; 75 | KEEP (*(.reset)) 76 | /* TODO revisit this now memset/memcpy/float in ROM */ 77 | /* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from 78 | * FLASH ... we will include any thing excluded here in .data below by default */ 79 | *(.init) 80 | *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*) 81 | *(.fini) 82 | /* Pull all c'tors into .text */ 83 | *crtbegin.o(.ctors) 84 | *crtbegin?.o(.ctors) 85 | *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) 86 | *(SORT(.ctors.*)) 87 | *(.ctors) 88 | /* Followed by destructors */ 89 | *crtbegin.o(.dtors) 90 | *crtbegin?.o(.dtors) 91 | *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) 92 | *(SORT(.dtors.*)) 93 | *(.dtors) 94 | 95 | *(.eh_frame*) 96 | . = ALIGN(4); 97 | } > FLASH 98 | 99 | .rodata : { 100 | *(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*) 101 | . = ALIGN(4); 102 | *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*))) 103 | . = ALIGN(4); 104 | } > FLASH 105 | 106 | .ARM.extab : 107 | { 108 | *(.ARM.extab* .gnu.linkonce.armextab.*) 109 | } > FLASH 110 | 111 | __exidx_start = .; 112 | .ARM.exidx : 113 | { 114 | *(.ARM.exidx* .gnu.linkonce.armexidx.*) 115 | } > FLASH 116 | __exidx_end = .; 117 | 118 | /* Machine inspectable binary information */ 119 | . = ALIGN(4); 120 | __binary_info_start = .; 121 | .binary_info : 122 | { 123 | KEEP(*(.binary_info.keep.*)) 124 | *(.binary_info.*) 125 | } > FLASH 126 | __binary_info_end = .; 127 | . = ALIGN(4); 128 | 129 | .ram_vector_table (NOLOAD): { 130 | *(.ram_vector_table) 131 | } > RAM 132 | 133 | .data : { 134 | __data_start__ = .; 135 | *(vtable) 136 | 137 | *(.time_critical*) 138 | 139 | /* remaining .text and .rodata; i.e. stuff we exclude above because we want it in RAM */ 140 | *(.text*) 141 | . = ALIGN(4); 142 | *(.rodata*) 143 | . = ALIGN(4); 144 | 145 | *(.data*) 146 | 147 | . = ALIGN(4); 148 | *(.after_data.*) 149 | . = ALIGN(4); 150 | /* preinit data */ 151 | PROVIDE_HIDDEN (__mutex_array_start = .); 152 | KEEP(*(SORT(.mutex_array.*))) 153 | KEEP(*(.mutex_array)) 154 | PROVIDE_HIDDEN (__mutex_array_end = .); 155 | 156 | . = ALIGN(4); 157 | /* preinit data */ 158 | PROVIDE_HIDDEN (__preinit_array_start = .); 159 | KEEP(*(SORT(.preinit_array.*))) 160 | KEEP(*(.preinit_array)) 161 | PROVIDE_HIDDEN (__preinit_array_end = .); 162 | 163 | . = ALIGN(4); 164 | /* init data */ 165 | PROVIDE_HIDDEN (__init_array_start = .); 166 | KEEP(*(SORT(.init_array.*))) 167 | KEEP(*(.init_array)) 168 | PROVIDE_HIDDEN (__init_array_end = .); 169 | 170 | . = ALIGN(4); 171 | /* finit data */ 172 | PROVIDE_HIDDEN (__fini_array_start = .); 173 | *(SORT(.fini_array.*)) 174 | *(.fini_array) 175 | PROVIDE_HIDDEN (__fini_array_end = .); 176 | 177 | *(.jcr) 178 | . = ALIGN(4); 179 | /* All data end */ 180 | __data_end__ = .; 181 | } > RAM AT> FLASH 182 | /* __etext is (for backwards compatibility) the name of the .data init source pointer (...) */ 183 | __etext = LOADADDR(.data); 184 | 185 | .uninitialized_data (NOLOAD): { 186 | . = ALIGN(4); 187 | *(.uninitialized_data*) 188 | } > RAM 189 | 190 | /* Start and end symbols must be word-aligned */ 191 | .scratch_x : { 192 | __scratch_x_start__ = .; 193 | *(.scratch_x.*) 194 | . = ALIGN(4); 195 | __scratch_x_end__ = .; 196 | } > SCRATCH_X AT > FLASH 197 | __scratch_x_source__ = LOADADDR(.scratch_x); 198 | 199 | .scratch_y : { 200 | __scratch_y_start__ = .; 201 | *(.scratch_y.*) 202 | . = ALIGN(4); 203 | __scratch_y_end__ = .; 204 | } > SCRATCH_Y AT > FLASH 205 | __scratch_y_source__ = LOADADDR(.scratch_y); 206 | 207 | .bss : { 208 | . = ALIGN(4); 209 | __bss_start__ = .; 210 | *(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*))) 211 | *(COMMON) 212 | . = ALIGN(4); 213 | __bss_end__ = .; 214 | } > RAM 215 | 216 | .heap (NOLOAD): 217 | { 218 | __end__ = .; 219 | end = __end__; 220 | KEEP(*(.heap*)) 221 | __HeapLimit = .; 222 | } > RAM 223 | 224 | /* .stack*_dummy section doesn't contains any symbols. It is only 225 | * used for linker to calculate size of stack sections, and assign 226 | * values to stack symbols later 227 | * 228 | * stack1 section may be empty/missing if platform_launch_core1 is not used */ 229 | 230 | /* by default we put core 0 stack at the end of scratch Y, so that if core 1 231 | * stack is not used then all of SCRATCH_X is free. 232 | */ 233 | .stack1_dummy (NOLOAD): 234 | { 235 | *(.stack1*) 236 | } > SCRATCH_X 237 | .stack_dummy (NOLOAD): 238 | { 239 | KEEP(*(.stack*)) 240 | } > SCRATCH_Y 241 | 242 | .flash_end : { 243 | PROVIDE(__flash_binary_end = .); 244 | } > FLASH 245 | 246 | /* stack limit is poorly named, but historically is maximum heap ptr */ 247 | __StackLimit = ORIGIN(RAM) + LENGTH(RAM); 248 | __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X); 249 | __StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y); 250 | __StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy); 251 | __StackBottom = __StackTop - SIZEOF(.stack_dummy); 252 | PROVIDE(__stack = __StackTop); 253 | 254 | /* Check if data + heap + stack exceeds RAM limit */ 255 | ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed") 256 | 257 | ASSERT( __binary_info_header_end - __logical_binary_start <= 256, "Binary info must be in first 256 bytes of the binary") 258 | /* todo assert on extra code */ 259 | } 260 | 261 | -------------------------------------------------------------------------------- /src/usb_descriptors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | #include "tusb.h" 27 | 28 | /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. 29 | * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. 30 | * 31 | * Auto ProductID layout's Bitmap: 32 | * [MSB] HID | MSC | CDC [LSB] 33 | */ 34 | #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) 35 | #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ 36 | _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) 37 | 38 | #define USB_VID 0xCafe 39 | #define USB_BCD 0x0200 40 | 41 | //--------------------------------------------------------------------+ 42 | // Device Descriptors 43 | //--------------------------------------------------------------------+ 44 | tusb_desc_device_t const desc_device = 45 | { 46 | .bLength = sizeof(tusb_desc_device_t), 47 | .bDescriptorType = TUSB_DESC_DEVICE, 48 | .bcdUSB = USB_BCD, 49 | 50 | // Use Interface Association Descriptor (IAD) for CDC 51 | // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) 52 | .bDeviceClass = TUSB_CLASS_MISC, 53 | .bDeviceSubClass = MISC_SUBCLASS_COMMON, 54 | .bDeviceProtocol = MISC_PROTOCOL_IAD, 55 | 56 | .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, 57 | 58 | .idVendor = USB_VID, 59 | .idProduct = USB_PID, 60 | .bcdDevice = 0x0100, 61 | 62 | .iManufacturer = 0x01, 63 | .iProduct = 0x02, 64 | .iSerialNumber = 0x03, 65 | 66 | .bNumConfigurations = 0x01 67 | }; 68 | 69 | // Invoked when received GET DEVICE DESCRIPTOR 70 | // Application return pointer to descriptor 71 | uint8_t const * tud_descriptor_device_cb(void) 72 | { 73 | return (uint8_t const *) &desc_device; 74 | } 75 | 76 | //--------------------------------------------------------------------+ 77 | // Configuration Descriptor 78 | //--------------------------------------------------------------------+ 79 | 80 | enum 81 | { 82 | ITF_NUM_CDC = 0, 83 | ITF_NUM_CDC_DATA, 84 | ITF_NUM_MSC, 85 | ITF_NUM_TOTAL 86 | }; 87 | 88 | #if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX 89 | // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number 90 | // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In, 5 Bulk etc ... 91 | #define EPNUM_CDC_NOTIF 0x81 92 | #define EPNUM_CDC_OUT 0x02 93 | #define EPNUM_CDC_IN 0x82 94 | 95 | #define EPNUM_MSC_OUT 0x05 96 | #define EPNUM_MSC_IN 0x85 97 | 98 | #elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X 99 | // SAMG & SAME70 don't support a same endpoint number with different direction IN and OUT 100 | // e.g EP1 OUT & EP1 IN cannot exist together 101 | #define EPNUM_CDC_NOTIF 0x81 102 | #define EPNUM_CDC_OUT 0x02 103 | #define EPNUM_CDC_IN 0x83 104 | 105 | #define EPNUM_MSC_OUT 0x04 106 | #define EPNUM_MSC_IN 0x85 107 | 108 | #elif CFG_TUSB_MCU == OPT_MCU_CXD56 109 | // CXD56 doesn't support a same endpoint number with different direction IN and OUT 110 | // e.g EP1 OUT & EP1 IN cannot exist together 111 | // CXD56 USB driver has fixed endpoint type (bulk/interrupt/iso) and direction (IN/OUT) by its number 112 | // 0 control (IN/OUT), 1 Bulk (IN), 2 Bulk (OUT), 3 In (IN), 4 Bulk (IN), 5 Bulk (OUT), 6 In (IN) 113 | #define EPNUM_CDC_NOTIF 0x83 114 | #define EPNUM_CDC_OUT 0x02 115 | #define EPNUM_CDC_IN 0x81 116 | 117 | #define EPNUM_MSC_OUT 0x05 118 | #define EPNUM_MSC_IN 0x84 119 | 120 | #elif CFG_TUSB_MCU == OPT_MCU_FT90X || CFG_TUSB_MCU == OPT_MCU_FT93X 121 | // FT9XX doesn't support a same endpoint number with different direction IN and OUT 122 | // e.g EP1 OUT & EP1 IN cannot exist together 123 | #define EPNUM_CDC_NOTIF 0x81 124 | #define EPNUM_CDC_OUT 0x02 125 | #define EPNUM_CDC_IN 0x83 126 | 127 | #define EPNUM_MSC_OUT 0x04 128 | #define EPNUM_MSC_IN 0x85 129 | 130 | #else 131 | #define EPNUM_CDC_NOTIF 0x81 132 | #define EPNUM_CDC_OUT 0x02 133 | #define EPNUM_CDC_IN 0x82 134 | 135 | #define EPNUM_MSC_OUT 0x03 136 | #define EPNUM_MSC_IN 0x83 137 | 138 | #endif 139 | 140 | #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_MSC_DESC_LEN) 141 | 142 | // full speed configuration 143 | uint8_t const desc_fs_configuration[] = 144 | { 145 | // Config number, interface count, string index, total length, attribute, power in mA 146 | TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), 147 | 148 | // Interface number, string index, EP notification address and size, EP data address (out, in) and size. 149 | TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64), 150 | 151 | // Interface number, string index, EP Out & EP In address, EP size 152 | TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64), 153 | }; 154 | 155 | #if TUD_OPT_HIGH_SPEED 156 | // Per USB specs: high speed capable device must report device_qualifier and other_speed_configuration 157 | 158 | // high speed configuration 159 | uint8_t const desc_hs_configuration[] = 160 | { 161 | // Config number, interface count, string index, total length, attribute, power in mA 162 | TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), 163 | 164 | // Interface number, string index, EP notification address and size, EP data address (out, in) and size. 165 | TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 512), 166 | 167 | // Interface number, string index, EP Out & EP In address, EP size 168 | TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, EPNUM_MSC_OUT, EPNUM_MSC_IN, 512), 169 | }; 170 | 171 | // other speed configuration 172 | uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN]; 173 | 174 | // device qualifier is mostly similar to device descriptor since we don't change configuration based on speed 175 | tusb_desc_device_qualifier_t const desc_device_qualifier = 176 | { 177 | .bLength = sizeof(tusb_desc_device_qualifier_t), 178 | .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER, 179 | .bcdUSB = USB_BCD, 180 | 181 | .bDeviceClass = TUSB_CLASS_MISC, 182 | .bDeviceSubClass = MISC_SUBCLASS_COMMON, 183 | .bDeviceProtocol = MISC_PROTOCOL_IAD, 184 | 185 | .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, 186 | .bNumConfigurations = 0x01, 187 | .bReserved = 0x00 188 | }; 189 | 190 | // Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request 191 | // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete. 192 | // device_qualifier descriptor describes information about a high-speed capable device that would 193 | // change if the device were operating at the other speed. If not highspeed capable stall this request. 194 | uint8_t const* tud_descriptor_device_qualifier_cb(void) 195 | { 196 | return (uint8_t const*) &desc_device_qualifier; 197 | } 198 | 199 | // Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request 200 | // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete 201 | // Configuration descriptor in the other speed e.g if high speed then this is for full speed and vice versa 202 | uint8_t const* tud_descriptor_other_speed_configuration_cb(uint8_t index) 203 | { 204 | (void) index; // for multiple configurations 205 | 206 | // if link speed is high return fullspeed config, and vice versa 207 | // Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG 208 | memcpy(desc_other_speed_config, 209 | (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration : desc_hs_configuration, 210 | CONFIG_TOTAL_LEN); 211 | 212 | desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; 213 | 214 | return desc_other_speed_config; 215 | } 216 | 217 | #endif // highspeed 218 | 219 | 220 | // Invoked when received GET CONFIGURATION DESCRIPTOR 221 | // Application return pointer to descriptor 222 | // Descriptor contents must exist long enough for transfer to complete 223 | uint8_t const * tud_descriptor_configuration_cb(uint8_t index) 224 | { 225 | (void) index; // for multiple configurations 226 | 227 | #if TUD_OPT_HIGH_SPEED 228 | // Although we are highspeed, host may be fullspeed. 229 | return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration; 230 | #else 231 | return desc_fs_configuration; 232 | #endif 233 | } 234 | 235 | //--------------------------------------------------------------------+ 236 | // String Descriptors 237 | //--------------------------------------------------------------------+ 238 | 239 | // array of pointer to string descriptors 240 | char const* string_desc_arr [] = 241 | { 242 | (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) 243 | "TinyUSB", // 1: Manufacturer 244 | "TinyUSB Device", // 2: Product 245 | "123456789012", // 3: Serials, should use chip ID 246 | "TinyUSB CDC", // 4: CDC Interface 247 | "TinyUSB MSC", // 5: MSC Interface 248 | }; 249 | 250 | static uint16_t _desc_str[32]; 251 | 252 | // Invoked when received GET STRING DESCRIPTOR request 253 | // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete 254 | uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) 255 | { 256 | (void) langid; 257 | 258 | uint8_t chr_count; 259 | 260 | if ( index == 0) 261 | { 262 | memcpy(&_desc_str[1], string_desc_arr[0], 2); 263 | chr_count = 1; 264 | }else 265 | { 266 | // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. 267 | // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors 268 | 269 | if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; 270 | 271 | const char* str = string_desc_arr[index]; 272 | 273 | // Cap at max char 274 | chr_count = (uint8_t) strlen(str); 275 | if ( chr_count > 31 ) chr_count = 31; 276 | 277 | // Convert ASCII string into UTF-16 278 | for(uint8_t i=0; i= 1 with LFN enabled, string functions convert the character 73 | / encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE 74 | / to be read/written via those functions. 75 | / 76 | / 0: ANSI/OEM in current CP 77 | / 1: Unicode in UTF-16LE 78 | / 2: Unicode in UTF-16BE 79 | / 3: Unicode in UTF-8 80 | */ 81 | 82 | 83 | /*---------------------------------------------------------------------------/ 84 | / Locale and Namespace Configurations 85 | /---------------------------------------------------------------------------*/ 86 | 87 | #define FF_CODE_PAGE 437 88 | /* This option specifies the OEM code page to be used on the target system. 89 | / Incorrect code page setting can cause a file open failure. 90 | / 91 | / 437 - U.S. 92 | / 720 - Arabic 93 | / 737 - Greek 94 | / 771 - KBL 95 | / 775 - Baltic 96 | / 850 - Latin 1 97 | / 852 - Latin 2 98 | / 855 - Cyrillic 99 | / 857 - Turkish 100 | / 860 - Portuguese 101 | / 861 - Icelandic 102 | / 862 - Hebrew 103 | / 863 - Canadian French 104 | / 864 - Arabic 105 | / 865 - Nordic 106 | / 866 - Russian 107 | / 869 - Greek 2 108 | / 932 - Japanese (DBCS) 109 | / 936 - Simplified Chinese (DBCS) 110 | / 949 - Korean (DBCS) 111 | / 950 - Traditional Chinese (DBCS) 112 | / 0 - Include all code pages above and configured by f_setcp() 113 | */ 114 | 115 | 116 | #define FF_USE_LFN 1 117 | #define FF_MAX_LFN 255 118 | /* The FF_USE_LFN switches the support for LFN (long file name). 119 | / 120 | / 0: Disable LFN. FF_MAX_LFN has no effect. 121 | / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. 122 | / 2: Enable LFN with dynamic working buffer on the STACK. 123 | / 3: Enable LFN with dynamic working buffer on the HEAP. 124 | / 125 | / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function 126 | / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and 127 | / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. 128 | / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can 129 | / be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN 130 | / specification. 131 | / When use stack for the working buffer, take care on stack overflow. When use heap 132 | / memory for the working buffer, memory management functions, ff_memalloc() and 133 | / ff_memfree() exemplified in ffsystem.c, need to be added to the project. */ 134 | 135 | 136 | #define FF_LFN_UNICODE 0 137 | /* This option switches the character encoding on the API when LFN is enabled. 138 | / 139 | / 0: ANSI/OEM in current CP (TCHAR = char) 140 | / 1: Unicode in UTF-16 (TCHAR = WCHAR) 141 | / 2: Unicode in UTF-8 (TCHAR = char) 142 | / 3: Unicode in UTF-32 (TCHAR = DWORD) 143 | / 144 | / Also behavior of string I/O functions will be affected by this option. 145 | / When LFN is not enabled, this option has no effect. */ 146 | 147 | 148 | #define FF_LFN_BUF 255 149 | #define FF_SFN_BUF 12 150 | /* This set of options defines size of file name members in the FILINFO structure 151 | / which is used to read out directory items. These values should be suffcient for 152 | / the file names to read. The maximum possible length of the read file name depends 153 | / on character encoding. When LFN is not enabled, these options have no effect. */ 154 | 155 | 156 | #define FF_FS_RPATH 2 157 | /* This option configures support for relative path. 158 | / 159 | / 0: Disable relative path and remove related functions. 160 | / 1: Enable relative path. f_chdir() and f_chdrive() are available. 161 | / 2: f_getcwd() function is available in addition to 1. 162 | */ 163 | 164 | 165 | /*---------------------------------------------------------------------------/ 166 | / Drive/Volume Configurations 167 | /---------------------------------------------------------------------------*/ 168 | 169 | #define FF_VOLUMES 4 170 | /* Number of volumes (logical drives) to be used. (1-10) */ 171 | 172 | 173 | #define FF_STR_VOLUME_ID 0 174 | #define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" 175 | /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. 176 | / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive 177 | / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each 178 | / logical drives. Number of items must not be less than FF_VOLUMES. Valid 179 | / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are 180 | / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is 181 | / not defined, a user defined volume string table is needed as: 182 | / 183 | / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... 184 | */ 185 | 186 | 187 | #define FF_MULTI_PARTITION 0 188 | /* This option switches support for multiple volumes on the physical drive. 189 | / By default (0), each logical drive number is bound to the same physical drive 190 | / number and only an FAT volume found on the physical drive will be mounted. 191 | / When this function is enabled (1), each logical drive number can be bound to 192 | / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() 193 | / function will be available. */ 194 | 195 | 196 | #define FF_MIN_SS 512 197 | #define FF_MAX_SS 4096 198 | /* This set of options configures the range of sector size to be supported. (512, 199 | / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and 200 | / harddisk, but a larger value may be required for on-board flash memory and some 201 | / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured 202 | / for variable sector size mode and disk_ioctl() function needs to implement 203 | / GET_SECTOR_SIZE command. */ 204 | 205 | 206 | #define FF_LBA64 0 207 | /* This option switches support for 64-bit LBA. (0:Disable or 1:Enable) 208 | / To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */ 209 | 210 | 211 | #define FF_MIN_GPT 0x10000000 212 | /* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and 213 | / f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ 214 | 215 | 216 | #define FF_USE_TRIM 0 217 | /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) 218 | / To enable Trim function, also CTRL_TRIM command should be implemented to the 219 | / disk_ioctl() function. */ 220 | 221 | 222 | 223 | /*---------------------------------------------------------------------------/ 224 | / System Configurations 225 | /---------------------------------------------------------------------------*/ 226 | 227 | #define FF_FS_TINY 0 228 | /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) 229 | / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. 230 | / Instead of private sector buffer eliminated from the file object, common sector 231 | / buffer in the filesystem object (FATFS) is used for the file data transfer. */ 232 | 233 | 234 | #define FF_FS_EXFAT 0 235 | /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) 236 | / To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1) 237 | / Note that enabling exFAT discards ANSI C (C89) compatibility. */ 238 | 239 | 240 | #define FF_FS_NORTC 1 241 | #define FF_NORTC_MON 1 242 | #define FF_NORTC_MDAY 1 243 | #define FF_NORTC_YEAR 2022 244 | /* The option FF_FS_NORTC switches timestamp feature. If the system does not have 245 | / an RTC or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable the 246 | / timestamp feature. Every object modified by FatFs will have a fixed timestamp 247 | / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. 248 | / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be 249 | / added to the project to read current time form real-time clock. FF_NORTC_MON, 250 | / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. 251 | / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ 252 | 253 | 254 | #define FF_FS_NOFSINFO 0 255 | /* If you need to know correct free space on the FAT32 volume, set bit 0 of this 256 | / option, and f_getfree() function at the first time after volume mount will force 257 | / a full FAT scan. Bit 1 controls the use of last allocated cluster number. 258 | / 259 | / bit0=0: Use free cluster count in the FSINFO if available. 260 | / bit0=1: Do not trust free cluster count in the FSINFO. 261 | / bit1=0: Use last allocated cluster number in the FSINFO if available. 262 | / bit1=1: Do not trust last allocated cluster number in the FSINFO. 263 | */ 264 | 265 | 266 | #define FF_FS_LOCK 0 267 | /* The option FF_FS_LOCK switches file lock function to control duplicated file open 268 | / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY 269 | / is 1. 270 | / 271 | / 0: Disable file lock function. To avoid volume corruption, application program 272 | / should avoid illegal open, remove and rename to the open objects. 273 | / >0: Enable file lock function. The value defines how many files/sub-directories 274 | / can be opened simultaneously under file lock control. Note that the file 275 | / lock control is independent of re-entrancy. */ 276 | 277 | 278 | #define FF_FS_REENTRANT 0 279 | #define FF_FS_TIMEOUT 1000 280 | /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs 281 | / module itself. Note that regardless of this option, file access to different 282 | / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() 283 | / and f_fdisk() function, are always not re-entrant. Only file/directory access 284 | / to the same volume is under control of this featuer. 285 | / 286 | / 0: Disable re-entrancy. FF_FS_TIMEOUT have no effect. 287 | / 1: Enable re-entrancy. Also user provided synchronization handlers, 288 | / ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give() 289 | / function, must be added to the project. Samples are available in ffsystem.c. 290 | / 291 | / The FF_FS_TIMEOUT defines timeout period in unit of O/S time tick. 292 | */ 293 | 294 | 295 | 296 | /*--- End of configuration options ---*/ 297 | -------------------------------------------------------------------------------- /src/fatfs/source/00history.txt: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | Revision history of FatFs module 3 | ---------------------------------------------------------------------------- 4 | 5 | R0.00 (February 26, 2006) 6 | 7 | Prototype. 8 | 9 | 10 | 11 | R0.01 (April 29, 2006) 12 | 13 | The first release. 14 | 15 | 16 | 17 | R0.02 (June 01, 2006) 18 | 19 | Added FAT12 support. 20 | Removed unbuffered mode. 21 | Fixed a problem on small (<32M) partition. 22 | 23 | 24 | 25 | R0.02a (June 10, 2006) 26 | 27 | Added a configuration option (_FS_MINIMUM). 28 | 29 | 30 | 31 | R0.03 (September 22, 2006) 32 | 33 | Added f_rename(). 34 | Changed option _FS_MINIMUM to _FS_MINIMIZE. 35 | 36 | 37 | 38 | R0.03a (December 11, 2006) 39 | 40 | Improved cluster scan algorithm to write files fast. 41 | Fixed f_mkdir() creates incorrect directory on FAT32. 42 | 43 | 44 | 45 | R0.04 (February 04, 2007) 46 | 47 | Added f_mkfs(). 48 | Supported multiple drive system. 49 | Changed some interfaces for multiple drive system. 50 | Changed f_mountdrv() to f_mount(). 51 | 52 | 53 | 54 | R0.04a (April 01, 2007) 55 | 56 | Supported multiple partitions on a physical drive. 57 | Added a capability of extending file size to f_lseek(). 58 | Added minimization level 3. 59 | Fixed an endian sensitive code in f_mkfs(). 60 | 61 | 62 | 63 | R0.04b (May 05, 2007) 64 | 65 | Added a configuration option _USE_NTFLAG. 66 | Added FSINFO support. 67 | Fixed DBCS name can result FR_INVALID_NAME. 68 | Fixed short seek (<= csize) collapses the file object. 69 | 70 | 71 | 72 | R0.05 (August 25, 2007) 73 | 74 | Changed arguments of f_read(), f_write() and f_mkfs(). 75 | Fixed f_mkfs() on FAT32 creates incorrect FSINFO. 76 | Fixed f_mkdir() on FAT32 creates incorrect directory. 77 | 78 | 79 | 80 | R0.05a (February 03, 2008) 81 | 82 | Added f_truncate() and f_utime(). 83 | Fixed off by one error at FAT sub-type determination. 84 | Fixed btr in f_read() can be mistruncated. 85 | Fixed cached sector is not flushed when create and close without write. 86 | 87 | 88 | 89 | R0.06 (April 01, 2008) 90 | 91 | Added fputc(), fputs(), fprintf() and fgets(). 92 | Improved performance of f_lseek() on moving to the same or following cluster. 93 | 94 | 95 | 96 | R0.07 (April 01, 2009) 97 | 98 | Merged Tiny-FatFs as a configuration option. (_FS_TINY) 99 | Added long file name feature. (_USE_LFN) 100 | Added multiple code page feature. (_CODE_PAGE) 101 | Added re-entrancy for multitask operation. (_FS_REENTRANT) 102 | Added auto cluster size selection to f_mkfs(). 103 | Added rewind option to f_readdir(). 104 | Changed result code of critical errors. 105 | Renamed string functions to avoid name collision. 106 | 107 | 108 | 109 | R0.07a (April 14, 2009) 110 | 111 | Septemberarated out OS dependent code on reentrant cfg. 112 | Added multiple sector size feature. 113 | 114 | 115 | 116 | R0.07c (June 21, 2009) 117 | 118 | Fixed f_unlink() can return FR_OK on error. 119 | Fixed wrong cache control in f_lseek(). 120 | Added relative path feature. 121 | Added f_chdir() and f_chdrive(). 122 | Added proper case conversion to extended character. 123 | 124 | 125 | 126 | R0.07e (November 03, 2009) 127 | 128 | Septemberarated out configuration options from ff.h to ffconf.h. 129 | Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH. 130 | Fixed name matching error on the 13 character boundary. 131 | Added a configuration option, _LFN_UNICODE. 132 | Changed f_readdir() to return the SFN with always upper case on non-LFN cfg. 133 | 134 | 135 | 136 | R0.08 (May 15, 2010) 137 | 138 | Added a memory configuration option. (_USE_LFN = 3) 139 | Added file lock feature. (_FS_SHARE) 140 | Added fast seek feature. (_USE_FASTSEEK) 141 | Changed some types on the API, XCHAR->TCHAR. 142 | Changed .fname in the FILINFO structure on Unicode cfg. 143 | String functions support UTF-8 encoding files on Unicode cfg. 144 | 145 | 146 | 147 | R0.08a (August 16, 2010) 148 | 149 | Added f_getcwd(). (_FS_RPATH = 2) 150 | Added sector erase feature. (_USE_ERASE) 151 | Moved file lock semaphore table from fs object to the bss. 152 | Fixed f_mkfs() creates wrong FAT32 volume. 153 | 154 | 155 | 156 | R0.08b (January 15, 2011) 157 | 158 | Fast seek feature is also applied to f_read() and f_write(). 159 | f_lseek() reports required table size on creating CLMP. 160 | Extended format syntax of f_printf(). 161 | Ignores duplicated directory separators in given path name. 162 | 163 | 164 | 165 | R0.09 (September 06, 2011) 166 | 167 | f_mkfs() supports multiple partition to complete the multiple partition feature. 168 | Added f_fdisk(). 169 | 170 | 171 | 172 | R0.09a (August 27, 2012) 173 | 174 | Changed f_open() and f_opendir() reject null object pointer to avoid crash. 175 | Changed option name _FS_SHARE to _FS_LOCK. 176 | Fixed assertion failure due to OS/2 EA on FAT12/16 volume. 177 | 178 | 179 | 180 | R0.09b (January 24, 2013) 181 | 182 | Added f_setlabel() and f_getlabel(). 183 | 184 | 185 | 186 | R0.10 (October 02, 2013) 187 | 188 | Added selection of character encoding on the file. (_STRF_ENCODE) 189 | Added f_closedir(). 190 | Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO) 191 | Added forced mount feature with changes of f_mount(). 192 | Improved behavior of volume auto detection. 193 | Improved write throughput of f_puts() and f_printf(). 194 | Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write(). 195 | Fixed f_write() can be truncated when the file size is close to 4GB. 196 | Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error. 197 | 198 | 199 | 200 | R0.10a (January 15, 2014) 201 | 202 | Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID) 203 | Added a configuration option of minimum sector size. (_MIN_SS) 204 | 2nd argument of f_rename() can have a drive number and it will be ignored. 205 | Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10) 206 | Fixed f_close() invalidates the file object without volume lock. 207 | Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10) 208 | Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07) 209 | 210 | 211 | 212 | R0.10b (May 19, 2014) 213 | 214 | Fixed a hard error in the disk I/O layer can collapse the directory entry. 215 | Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07) 216 | 217 | 218 | 219 | R0.10c (November 09, 2014) 220 | 221 | Added a configuration option for the platforms without RTC. (_FS_NORTC) 222 | Changed option name _USE_ERASE to _USE_TRIM. 223 | Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b) 224 | Fixed a potential problem of FAT access that can appear on disk error. 225 | Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08) 226 | 227 | 228 | 229 | R0.11 (February 09, 2015) 230 | 231 | Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND) 232 | Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c) 233 | Fixed _FS_NORTC option does not work properly. (appeared at R0.10c) 234 | 235 | 236 | 237 | R0.11a (September 05, 2015) 238 | 239 | Fixed wrong media change can lead a deadlock at thread-safe configuration. 240 | Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE) 241 | Removed some code pages actually not exist on the standard systems. (_CODE_PAGE) 242 | Fixed errors in the case conversion teble of code page 437 and 850 (ff.c). 243 | Fixed errors in the case conversion teble of Unicode (cc*.c). 244 | 245 | 246 | 247 | R0.12 (April 12, 2016) 248 | 249 | Added support for exFAT file system. (_FS_EXFAT) 250 | Added f_expand(). (_USE_EXPAND) 251 | Changed some members in FINFO structure and behavior of f_readdir(). 252 | Added an option _USE_CHMOD. 253 | Removed an option _WORD_ACCESS. 254 | Fixed errors in the case conversion table of Unicode (cc*.c). 255 | 256 | 257 | 258 | R0.12a (July 10, 2016) 259 | 260 | Added support for creating exFAT volume with some changes of f_mkfs(). 261 | Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed. 262 | f_forward() is available regardless of _FS_TINY. 263 | Fixed f_mkfs() creates wrong volume. (appeared at R0.12) 264 | Fixed wrong memory read in create_name(). (appeared at R0.12) 265 | Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD. 266 | 267 | 268 | 269 | R0.12b (September 04, 2016) 270 | 271 | Made f_rename() be able to rename objects with the same name but case. 272 | Fixed an error in the case conversion teble of code page 866. (ff.c) 273 | Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12) 274 | Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12) 275 | Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12) 276 | Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12) 277 | Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12) 278 | Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12) 279 | 280 | 281 | 282 | R0.12c (March 04, 2017) 283 | 284 | Improved write throughput at the fragmented file on the exFAT volume. 285 | Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN. 286 | Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12) 287 | Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c) 288 | 289 | 290 | 291 | R0.13 (May 21, 2017) 292 | 293 | Changed heading character of configuration keywords "_" to "FF_". 294 | Removed ASCII-only configuration, FF_CODE_PAGE = 1. Use FF_CODE_PAGE = 437 instead. 295 | Added f_setcp(), run-time code page configuration. (FF_CODE_PAGE = 0) 296 | Improved cluster allocation time on stretch a deep buried cluster chain. 297 | Improved processing time of f_mkdir() with large cluster size by using FF_USE_LFN = 3. 298 | Improved NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous. 299 | Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12) 300 | Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c) 301 | Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c) 302 | 303 | 304 | 305 | R0.13a (October 14, 2017) 306 | 307 | Added support for UTF-8 encoding on the API. (FF_LFN_UNICODE = 2) 308 | Added options for file name output buffer. (FF_LFN_BUF, FF_SFN_BUF). 309 | Added dynamic memory allocation option for working buffer of f_mkfs() and f_fdisk(). 310 | Fixed f_fdisk() and f_mkfs() create the partition table with wrong CHS parameters. (appeared at R0.09) 311 | Fixed f_unlink() can cause lost clusters at fragmented file on the exFAT volume. (appeared at R0.12c) 312 | Fixed f_setlabel() rejects some valid characters for exFAT volume. (appeared at R0.12) 313 | 314 | 315 | 316 | R0.13b (April 07, 2018) 317 | 318 | Added support for UTF-32 encoding on the API. (FF_LFN_UNICODE = 3) 319 | Added support for Unix style volume ID. (FF_STR_VOLUME_ID = 2) 320 | Fixed accesing any object on the exFAT root directory beyond the cluster boundary can fail. (appeared at R0.12c) 321 | Fixed f_setlabel() does not reject some invalid characters. (appeared at R0.09b) 322 | 323 | 324 | 325 | R0.13c (October 14, 2018) 326 | Supported stdint.h for C99 and later. (integer.h was included in ff.h) 327 | Fixed reading a directory gets infinite loop when the last directory entry is not empty. (appeared at R0.12) 328 | Fixed creating a sub-directory in the fragmented sub-directory on the exFAT volume collapses FAT chain of the parent directory. (appeared at R0.12) 329 | Fixed f_getcwd() cause output buffer overrun when the buffer has a valid drive number. (appeared at R0.13b) 330 | 331 | 332 | 333 | R0.14 (October 14, 2019) 334 | Added support for 64-bit LBA and GUID partition table (FF_LBA64 = 1) 335 | Changed some API functions, f_mkfs() and f_fdisk(). 336 | Fixed f_open() function cannot find the file with file name in length of FF_MAX_LFN characters. 337 | Fixed f_readdir() function cannot retrieve long file names in length of FF_MAX_LFN - 1 characters. 338 | Fixed f_readdir() function returns file names with wrong case conversion. (appeared at R0.12) 339 | Fixed f_mkfs() function can fail to create exFAT volume in the second partition. (appeared at R0.12) 340 | 341 | 342 | R0.14a (December 5, 2020) 343 | Limited number of recursive calls in f_findnext(). 344 | Fixed old floppy disks formatted with MS-DOS 2.x and 3.x cannot be mounted. 345 | Fixed some compiler warnings. 346 | 347 | 348 | 349 | R0.14b (April 17, 2021) 350 | Made FatFs uses standard library for copy, compare and search instead of built-in string functions. 351 | Added support for long long integer and floating point to f_printf(). (FF_STRF_LLI and FF_STRF_FP) 352 | Made path name parser ignore the terminating separator to allow "dir/". 353 | Improved the compatibility in Unix style path name feature. 354 | Fixed the file gets dead-locked when f_open() failed with some conditions. (appeared at R0.12a) 355 | Fixed f_mkfs() can create wrong exFAT volume due to a timing dependent error. (appeared at R0.12) 356 | Fixed code page 855 cannot be set by f_setcp(). 357 | Fixed some compiler warnings. 358 | 359 | 360 | 361 | R0.15 (November 6, 2022) 362 | Changed user provided synchronization functions in order to completely eliminate the platform dependency from FatFs code. 363 | FF_SYNC_t is removed from the configuration options. 364 | Fixed a potential error in f_mount when FF_FS_REENTRANT. 365 | Fixed file lock control FF_FS_LOCK is not mutal excluded when FF_FS_REENTRANT && FF_VOLUMES > 1 is true. 366 | Fixed f_mkfs() creates broken exFAT volume when the size of volume is >= 2^32 sectors. 367 | Fixed string functions cannot write the unicode characters not in BMP when FF_LFN_UNICODE == 2 (UTF-8). 368 | Fixed a compatibility issue in identification of GPT header. 369 | 370 | -------------------------------------------------------------------------------- /src/fatfs/source/ff.h: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------------/ 2 | / FatFs - Generic FAT Filesystem module R0.15 / 3 | /-----------------------------------------------------------------------------/ 4 | / 5 | / Copyright (C) 2022, ChaN, all right reserved. 6 | / 7 | / FatFs module is an open source software. Redistribution and use of FatFs in 8 | / source and binary forms, with or without modification, are permitted provided 9 | / that the following condition is met: 10 | 11 | / 1. Redistributions of source code must retain the above copyright notice, 12 | / this condition and the following disclaimer. 13 | / 14 | / This software is provided by the copyright holder and contributors "AS IS" 15 | / and any warranties related to this software are DISCLAIMED. 16 | / The copyright owner or contributors be NOT LIABLE for any damages caused 17 | / by use of this software. 18 | / 19 | /----------------------------------------------------------------------------*/ 20 | 21 | 22 | #ifndef FF_DEFINED 23 | #define FF_DEFINED 80286 /* Revision ID */ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #include "ffconf.h" /* FatFs configuration options */ 30 | 31 | #if FF_DEFINED != FFCONF_DEF 32 | #error Wrong configuration file (ffconf.h). 33 | #endif 34 | 35 | 36 | /* Integer types used for FatFs API */ 37 | 38 | #if defined(_WIN32) /* Windows VC++ (for development only) */ 39 | #define FF_INTDEF 2 40 | #include 41 | typedef unsigned __int64 QWORD; 42 | #include 43 | #define isnan(v) _isnan(v) 44 | #define isinf(v) (!_finite(v)) 45 | 46 | #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */ 47 | #define FF_INTDEF 2 48 | #include 49 | typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ 50 | typedef unsigned char BYTE; /* char must be 8-bit */ 51 | typedef uint16_t WORD; /* 16-bit unsigned integer */ 52 | typedef uint32_t DWORD; /* 32-bit unsigned integer */ 53 | typedef uint64_t QWORD; /* 64-bit unsigned integer */ 54 | typedef WORD WCHAR; /* UTF-16 character type */ 55 | 56 | #else /* Earlier than C99 */ 57 | #define FF_INTDEF 1 58 | typedef unsigned int UINT; /* int must be 16-bit or 32-bit */ 59 | typedef unsigned char BYTE; /* char must be 8-bit */ 60 | typedef unsigned short WORD; /* 16-bit unsigned integer */ 61 | typedef unsigned long DWORD; /* 32-bit unsigned integer */ 62 | typedef WORD WCHAR; /* UTF-16 character type */ 63 | #endif 64 | 65 | 66 | /* Type of file size and LBA variables */ 67 | 68 | #if FF_FS_EXFAT 69 | #if FF_INTDEF != 2 70 | #error exFAT feature wants C99 or later 71 | #endif 72 | typedef QWORD FSIZE_t; 73 | #if FF_LBA64 74 | typedef QWORD LBA_t; 75 | #else 76 | typedef DWORD LBA_t; 77 | #endif 78 | #else 79 | #if FF_LBA64 80 | #error exFAT needs to be enabled when enable 64-bit LBA 81 | #endif 82 | typedef DWORD FSIZE_t; 83 | typedef DWORD LBA_t; 84 | #endif 85 | 86 | 87 | 88 | /* Type of path name strings on FatFs API (TCHAR) */ 89 | 90 | #if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */ 91 | typedef WCHAR TCHAR; 92 | #define _T(x) L ## x 93 | #define _TEXT(x) L ## x 94 | #elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */ 95 | typedef char TCHAR; 96 | #define _T(x) u8 ## x 97 | #define _TEXT(x) u8 ## x 98 | #elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */ 99 | typedef DWORD TCHAR; 100 | #define _T(x) U ## x 101 | #define _TEXT(x) U ## x 102 | #elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3) 103 | #error Wrong FF_LFN_UNICODE setting 104 | #else /* ANSI/OEM code in SBCS/DBCS */ 105 | typedef char TCHAR; 106 | #define _T(x) x 107 | #define _TEXT(x) x 108 | #endif 109 | 110 | 111 | 112 | /* Definitions of volume management */ 113 | 114 | #if FF_MULTI_PARTITION /* Multiple partition configuration */ 115 | typedef struct { 116 | BYTE pd; /* Physical drive number */ 117 | BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ 118 | } PARTITION; 119 | extern PARTITION VolToPart[]; /* Volume - Partition mapping table */ 120 | #endif 121 | 122 | #if FF_STR_VOLUME_ID 123 | #ifndef FF_VOLUME_STRS 124 | extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */ 125 | #endif 126 | #endif 127 | 128 | 129 | 130 | /* Filesystem object structure (FATFS) */ 131 | 132 | typedef struct { 133 | BYTE fs_type; /* Filesystem type (0:not mounted) */ 134 | BYTE pdrv; /* Volume hosting physical drive */ 135 | BYTE ldrv; /* Logical drive number (used only when FF_FS_REENTRANT) */ 136 | BYTE n_fats; /* Number of FATs (1 or 2) */ 137 | BYTE wflag; /* win[] status (b0:dirty) */ 138 | BYTE fsi_flag; /* FSINFO status (b7:disabled, b0:dirty) */ 139 | WORD id; /* Volume mount ID */ 140 | WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ 141 | WORD csize; /* Cluster size [sectors] */ 142 | #if FF_MAX_SS != FF_MIN_SS 143 | WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ 144 | #endif 145 | #if FF_USE_LFN 146 | WCHAR* lfnbuf; /* LFN working buffer */ 147 | #endif 148 | #if FF_FS_EXFAT 149 | BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ 150 | #endif 151 | #if !FF_FS_READONLY 152 | DWORD last_clst; /* Last allocated cluster */ 153 | DWORD free_clst; /* Number of free clusters */ 154 | #endif 155 | #if FF_FS_RPATH 156 | DWORD cdir; /* Current directory start cluster (0:root) */ 157 | #if FF_FS_EXFAT 158 | DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ 159 | DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ 160 | DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ 161 | #endif 162 | #endif 163 | DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ 164 | DWORD fsize; /* Number of sectors per FAT */ 165 | LBA_t volbase; /* Volume base sector */ 166 | LBA_t fatbase; /* FAT base sector */ 167 | LBA_t dirbase; /* Root directory base sector (FAT12/16) or cluster (FAT32/exFAT) */ 168 | LBA_t database; /* Data base sector */ 169 | #if FF_FS_EXFAT 170 | LBA_t bitbase; /* Allocation bitmap base sector */ 171 | #endif 172 | LBA_t winsect; /* Current sector appearing in the win[] */ 173 | BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ 174 | } FATFS; 175 | 176 | 177 | 178 | /* Object ID and allocation information (FFOBJID) */ 179 | 180 | typedef struct { 181 | FATFS* fs; /* Pointer to the hosting volume of this object */ 182 | WORD id; /* Hosting volume's mount ID */ 183 | BYTE attr; /* Object attribute */ 184 | BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */ 185 | DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */ 186 | FSIZE_t objsize; /* Object size (valid when sclust != 0) */ 187 | #if FF_FS_EXFAT 188 | DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */ 189 | DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */ 190 | DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */ 191 | DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ 192 | DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */ 193 | #endif 194 | #if FF_FS_LOCK 195 | UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ 196 | #endif 197 | } FFOBJID; 198 | 199 | 200 | 201 | /* File object structure (FIL) */ 202 | 203 | typedef struct { 204 | FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */ 205 | BYTE flag; /* File status flags */ 206 | BYTE err; /* Abort flag (error code) */ 207 | FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ 208 | DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ 209 | LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */ 210 | #if !FF_FS_READONLY 211 | LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ 212 | BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */ 213 | #endif 214 | #if FF_USE_FASTSEEK 215 | DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */ 216 | #endif 217 | #if !FF_FS_TINY 218 | BYTE buf[FF_MAX_SS]; /* File private data read/write window */ 219 | #endif 220 | } FIL; 221 | 222 | 223 | 224 | /* Directory object structure (DIR) */ 225 | 226 | typedef struct { 227 | FFOBJID obj; /* Object identifier */ 228 | DWORD dptr; /* Current read/write offset */ 229 | DWORD clust; /* Current cluster */ 230 | LBA_t sect; /* Current sector (0:Read operation has terminated) */ 231 | BYTE* dir; /* Pointer to the directory item in the win[] */ 232 | BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ 233 | #if FF_USE_LFN 234 | DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */ 235 | #endif 236 | #if FF_USE_FIND 237 | const TCHAR* pat; /* Pointer to the name matching pattern */ 238 | #endif 239 | } DIR; 240 | 241 | 242 | 243 | /* File information structure (FILINFO) */ 244 | 245 | typedef struct { 246 | FSIZE_t fsize; /* File size */ 247 | WORD fdate; /* Modified date */ 248 | WORD ftime; /* Modified time */ 249 | BYTE fattrib; /* File attribute */ 250 | #if FF_USE_LFN 251 | TCHAR altname[FF_SFN_BUF + 1];/* Alternative file name */ 252 | TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */ 253 | #else 254 | TCHAR fname[12 + 1]; /* File name */ 255 | #endif 256 | } FILINFO; 257 | 258 | 259 | 260 | /* Format parameter structure (MKFS_PARM) */ 261 | 262 | typedef struct { 263 | BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */ 264 | BYTE n_fat; /* Number of FATs */ 265 | UINT align; /* Data area alignment (sector) */ 266 | UINT n_root; /* Number of root directory entries */ 267 | DWORD au_size; /* Cluster size (byte) */ 268 | } MKFS_PARM; 269 | 270 | 271 | 272 | /* File function return code (FRESULT) */ 273 | 274 | typedef enum { 275 | FR_OK = 0, /* (0) Succeeded */ 276 | FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ 277 | FR_INT_ERR, /* (2) Assertion failed */ 278 | FR_NOT_READY, /* (3) The physical drive cannot work */ 279 | FR_NO_FILE, /* (4) Could not find the file */ 280 | FR_NO_PATH, /* (5) Could not find the path */ 281 | FR_INVALID_NAME, /* (6) The path name format is invalid */ 282 | FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ 283 | FR_EXIST, /* (8) Access denied due to prohibited access */ 284 | FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ 285 | FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ 286 | FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ 287 | FR_NOT_ENABLED, /* (12) The volume has no work area */ 288 | FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ 289 | FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ 290 | FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ 291 | FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ 292 | FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ 293 | FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ 294 | FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ 295 | } FRESULT; 296 | 297 | 298 | 299 | 300 | /*--------------------------------------------------------------*/ 301 | /* FatFs Module Application Interface */ 302 | /*--------------------------------------------------------------*/ 303 | 304 | FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ 305 | FRESULT f_close (FIL* fp); /* Close an open file object */ 306 | FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */ 307 | FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ 308 | FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ 309 | FRESULT f_truncate (FIL* fp); /* Truncate the file */ 310 | FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ 311 | FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */ 312 | FRESULT f_closedir (DIR* dp); /* Close an open directory */ 313 | FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */ 314 | FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ 315 | FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */ 316 | FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ 317 | FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ 318 | FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ 319 | FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ 320 | FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */ 321 | FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */ 322 | FRESULT f_chdir (const TCHAR* path); /* Change current directory */ 323 | FRESULT f_chdrive (const TCHAR* path); /* Change current drive */ 324 | FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ 325 | FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ 326 | FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ 327 | FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ 328 | FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ 329 | FRESULT f_expand (FIL* fp, FSIZE_t fsz, BYTE opt); /* Allocate a contiguous block to the file */ 330 | FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ 331 | FRESULT f_mkfs (const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len); /* Create a FAT volume */ 332 | FRESULT f_fdisk (BYTE pdrv, const LBA_t ptbl[], void* work); /* Divide a physical drive into some partitions */ 333 | FRESULT f_setcp (WORD cp); /* Set current code page */ 334 | int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ 335 | int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ 336 | int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ 337 | TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ 338 | 339 | /* Some API fucntions are implemented as macro */ 340 | 341 | #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize)) 342 | #define f_error(fp) ((fp)->err) 343 | #define f_tell(fp) ((fp)->fptr) 344 | #define f_size(fp) ((fp)->obj.objsize) 345 | #define f_rewind(fp) f_lseek((fp), 0) 346 | #define f_rewinddir(dp) f_readdir((dp), 0) 347 | #define f_rmdir(path) f_unlink(path) 348 | #define f_unmount(path) f_mount(0, path, 0) 349 | 350 | 351 | 352 | 353 | /*--------------------------------------------------------------*/ 354 | /* Additional Functions */ 355 | /*--------------------------------------------------------------*/ 356 | 357 | /* RTC function (provided by user) */ 358 | #if !FF_FS_READONLY && !FF_FS_NORTC 359 | DWORD get_fattime (void); /* Get current time */ 360 | #endif 361 | 362 | 363 | /* LFN support functions (defined in ffunicode.c) */ 364 | 365 | #if FF_USE_LFN >= 1 366 | WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */ 367 | WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */ 368 | DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */ 369 | #endif 370 | 371 | 372 | /* O/S dependent functions (samples available in ffsystem.c) */ 373 | 374 | #if FF_USE_LFN == 3 /* Dynamic memory allocation */ 375 | void* ff_memalloc (UINT msize); /* Allocate memory block */ 376 | void ff_memfree (void* mblock); /* Free memory block */ 377 | #endif 378 | #if FF_FS_REENTRANT /* Sync functions */ 379 | int ff_mutex_create (int vol); /* Create a sync object */ 380 | void ff_mutex_delete (int vol); /* Delete a sync object */ 381 | int ff_mutex_take (int vol); /* Lock sync object */ 382 | void ff_mutex_give (int vol); /* Unlock sync object */ 383 | #endif 384 | 385 | 386 | 387 | 388 | /*--------------------------------------------------------------*/ 389 | /* Flags and Offset Address */ 390 | /*--------------------------------------------------------------*/ 391 | 392 | /* File access mode and open method flags (3rd argument of f_open) */ 393 | #define FA_READ 0x01 394 | #define FA_WRITE 0x02 395 | #define FA_OPEN_EXISTING 0x00 396 | #define FA_CREATE_NEW 0x04 397 | #define FA_CREATE_ALWAYS 0x08 398 | #define FA_OPEN_ALWAYS 0x10 399 | #define FA_OPEN_APPEND 0x30 400 | 401 | /* Fast seek controls (2nd argument of f_lseek) */ 402 | #define CREATE_LINKMAP ((FSIZE_t)0 - 1) 403 | 404 | /* Format options (2nd argument of f_mkfs) */ 405 | #define FM_FAT 0x01 406 | #define FM_FAT32 0x02 407 | #define FM_EXFAT 0x04 408 | #define FM_ANY 0x07 409 | #define FM_SFD 0x08 410 | 411 | /* Filesystem type (FATFS.fs_type) */ 412 | #define FS_FAT12 1 413 | #define FS_FAT16 2 414 | #define FS_FAT32 3 415 | #define FS_EXFAT 4 416 | 417 | /* File attribute bits for directory entry (FILINFO.fattrib) */ 418 | #define AM_RDO 0x01 /* Read only */ 419 | #define AM_HID 0x02 /* Hidden */ 420 | #define AM_SYS 0x04 /* System */ 421 | #define AM_DIR 0x10 /* Directory */ 422 | #define AM_ARC 0x20 /* Archive */ 423 | 424 | 425 | #ifdef __cplusplus 426 | } 427 | #endif 428 | 429 | #endif /* FF_DEFINED */ 430 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # CPC ROM Emulator using Pi Pico 2 | 3 | ## Features 4 | 5 | * Easy to build - only Pi Pico and a handful of passive components requried 6 | * Emulates the lower ROM and up to 12 upper ROMs (number of ROMs is limited by available Pico RAM) 7 | * Acts as USB flash drive when plugged into a PC - easy to copy ROMs 8 | * Handles plain ROMs, or ROMs with 128byte headers 9 | * Companion ROM to control the board from the CPC 10 | * Optional USB module - partially compatible with Albireo (only supports USB drive) https://www.cpcwiki.eu/index.php/Albireo 11 | 12 | 13 | ## Quickstart 14 | 15 | ### To load the firmware image: 16 | * Press and hold the bootsel button on the pico and plug the pico into a PC. It should appear as a USB drive alled RPI-RP2. 17 | * Copy the firmware image firmware/cpc_rom_emulator.uf2 onto the Pico USB drive 18 | 19 | You have now programed your Pico. It should reboot and appear a USB drive called PICOROM with about 1.5MB capacity. The LED should slowly blink about once per second. 20 | 21 | ### Format the PICOROM 22 | If this is the first time you have have installed the software, or you are upgrading from an earlier verion, you need to format the drive. 23 | * Press and hold the bootsel button until the LED stays on (about 10 seconds). 24 | * Release the button, the LED should turn off and the flash drive will be formatted. 25 | 26 | ### Obtain ROM images 27 | You will need some CPC ROM images. You will also need the picorom.rom file, which is in the firmware directory. 28 | There are many ROM iamges available here https://www.cpcwiki.eu/index.php/ROM_List 29 | If the PICOROM detects that the ROM file has a AMSDOS header, it will be removed before loading. 30 | * Copy your ROM images onto the PICOROM USB drive 31 | 32 | As a minimm you will need OS_6128.ROM and BASIC_1.1.ROM. You should also add picorom.rom 33 | 34 | At this point, you can test your PICOROM by unplugging the USB cable and plugging it into your CPC. If all is well, the CPC shold boot into Basic 1.1 35 | 36 | ### Create config files 37 | 38 | You can create config files from the PC to define what ROMS to load. These files consist of one or more lines like this: 39 | 40 | ``` 41 | : 42 | ``` 43 | 44 | Where `````` = L for lower ROM or 0-13 for upper ROM bank 45 | and `````` = the filename of the ROM to load 46 | 47 | For example: 48 | ``` 49 | # Default config 50 | L:OS_6128.ROM 51 | 0:BASIC_1.1.ROM 52 | 1:picorom.rom 53 | 2:maxam15.ROM 54 | 3:Protext.rom 55 | 4:Utopia_v1_25b.ROM 56 | 5:Manic_Miner.rom 57 | 6:Chuckie.rom 58 | ``` 59 | 60 | At startup, if the Pico finds a file called DEFAULT.CFG it will load that. Otherwise if will try to load OS_6128.ROM and BASIC_1.1.ROM 61 | 62 | ### Status LED 63 | 64 | The LED shows the status of the board: 65 | * solid on - emulating ROMs for the CPC 66 | * 1/2 second on/off - emulating USB drive for PC 67 | * off - bootloader mode (or no power!) 68 | * Repeating rapid flashes indicate an error: 69 | * 4 flashes = Failed to load OS ROM 70 | * 5 flashes = Failed to load Basic ROM 71 | 72 | ### ROM Commands 73 | 74 | If you have loaded the picorom.rom, the you get some new commands on the CPC which let you control the board: 75 | 76 | * |PUSB - start emulating a USB drive. CPC will stop working. 77 | * |LED,n - Control the PICO LED n=1 for on, n=0 for off 78 | * |ROMSET,"``````" - load a new config from the Pico. 79 | * |PDIR - list all available ROMS on the Pico 80 | * |ROMS - List currently inserted ROMs 81 | * |ROMOUT,n - remove a ROM from slot n 82 | * |ROMIN,n,"``````" - loads rom into slot n 83 | 84 | ## TODO 85 | 86 | * Support listing subdirectories from CPC 87 | 88 | ## More details 89 | 90 | ![Completed Board](/hardware/PicoROM_Assembled.jpg) 91 | 92 | 93 | |Signal |GPIO |CPC Pin|Pico Pin | 94 | |-----------|-------------|-------|------------------| 95 | |A0-A13 |GPIO0-GPIO13 |18-5 |1-2,4-7,9-12,14-17| 96 | |D0-D7 |GPIO14-GPIO21|26-19 |19-22,24-27 | 97 | |~ROMEN |GPIO22 |42 |29 | 98 | |ROMDIS |- |43 |- | 99 | |A15 |GPIO26 |3 |31| 100 | |WRITE_LATCH|GPIO27 |- |32| 101 | |CPC RESET |GPIO28 |40* |34| 102 | |Pico RESET |- |- |30| 103 | 104 | 105 | \* GPIO28 connected to CPC RESET via a diode 106 | ``` 107 | CPC RESET __|\_|___ GPIO28 108 | |/ | 109 | 110 | ``` 111 | CPC Reset also has a push button to 0V 112 | 113 | 114 | WRITE_LATCH signal is created using a Diode-OR gate: 115 | ``` 116 | A13 __|\|___ 117 | |/| | 118 | | 119 | ~WR __|\|__|____ WRITE_LATCH (GPIO27) 120 | |/| | 121 | | 2k2 122 | ~IORQ __|\|__|__/\/\/\/\___ 123 | |/| | 124 | | 125 | GND 126 | 127 | ``` 128 | All diodes IN4148 or similar. Pulldown via 2k2 resistor to ground. 129 | 130 | Note: Rev1 board do not have the resistor pulldown, but it can be added as shown below 131 | ![Pulldown mod](/hardware/pulldown_mod.jpg) 132 | 133 | ROMDIS is connected to 5V 134 | 135 | Pico is powered from 5V via a IN4148 diode to VSYS 136 | 137 | 138 | Pico Reset is a push button to 0V 139 | 140 | ## Notes 141 | 142 | At startup, ROMs are loaded into RAM arrays, the the second core emulates all ROM 143 | The first core handles the ROM latch at 0xDFxx with the help of a PIO state machine. The same IO port is also used to send commands to the PICO. This is done by writing a series of bytes to the port, startign with a 0xfc (which I don't think is a valid ROM number). Format is as follows: 144 | * 0xfc - cmd prefix 145 | * cmd byte 146 | * 0 to 4 parameter bytes 147 | 148 | Data is sent from the PICO to the CPC via a 0xff byte area in the ROM at 0xC100. Format is as follows: 149 | * sequence number - incremented when the PICO has completed the command 150 | * status code. 0=OK 151 | * data type. 1 = null terminated string 152 | * data ( 0 or more bytes) 153 | 154 | There is a CPC ROM which provides a control over the ROM emulator. 155 | 156 | ## Flash drive 157 | 158 | The flash drive is emulated as a USB MSC device. The SPIFTL library is used to provide wear leveling for the flash. 159 | 160 | ## PCB 161 | **WARNING** There is an error on the schematic and PCB silkscreen. D2 is reversed. So, if you are going to build this, make sure that you insert D2 with the cathode (stripe) at the bottom. 162 | ---- 163 | There is a schematic and PCB layout which includes an optional USB interface. 164 | ![Schematic](hardware/schematic.png) 165 | ![PCB](hardware/pcb.png) 166 | 167 | My PCBs were made by PCBWay. The [gerbers](hardware/gerbers.zip) I used are also avalilable. 168 | 169 | # Credits 170 | FatFs - http://elm-chan.org/fsw/ff/ 171 | 172 | ``` 173 | /*----------------------------------------------------------------------------/ 174 | / FatFs - Generic FAT Filesystem Module Rx.xx / 175 | /-----------------------------------------------------------------------------/ 176 | / 177 | / Copyright (C) 20xx, ChaN, all right reserved. 178 | / 179 | / FatFs module is an open source software. Redistribution and use of FatFs in 180 | / source and binary forms, with or without modification, are permitted provided 181 | / that the following condition is met: 182 | / 183 | / 1. Redistributions of source code must retain the above copyright notice, 184 | / this condition and the following disclaimer. 185 | / 186 | / This software is provided by the copyright holder and contributors "AS IS" 187 | / and any warranties related to this software are DISCLAIMED. 188 | / The copyright owner or contributors be NOT LIABLE for any damages caused 189 | / by use of this software. 190 | /----------------------------------------------------------------------------*/ 191 | ``` 192 | 193 | USB flash drive code - https://github.com/oyama/pico-usb-flash-drive 194 | 195 | ``` 196 | Copyright 2024, Hiroyuki OYAMA. All rights reserved. 197 | 198 | Redistribution and use in source and binary forms, with or without modification, 199 | are permitted provided that the following conditions are met: 200 | 201 | - Redistributions of source code must retain the above copyright notice, this 202 | list of conditions and the following disclaimer. 203 | - Redistributions in binary form must reproduce the above copyright notice, this 204 | list of conditions and the following disclaimer in the documentation and/or 205 | other materials provided with the distribution. 206 | - Neither the name of the copyright holder nor the names of its contributors may 207 | be used to endorse or promote products derived from this software without 208 | specific prior written permission. 209 | 210 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “ AS IS” AND 211 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 212 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 213 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 214 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 215 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 216 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 217 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 218 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 219 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 220 | 221 | ``` 222 | 223 | SPIFTL code - https://github.com/earlephilhower/SPIFTL 224 | 225 | ``` 226 | # SPIFTL - Embedded, Static Wear-Leveling Library 227 | ## Copyright 2024, Earle F. Philhower III 228 | 229 | GNU LESSER GENERAL PUBLIC LICENSE 230 | Version 3, 29 June 2007 231 | 232 | Copyright (C) 2007 Free Software Foundation, Inc. 233 | Everyone is permitted to copy and distribute verbatim copies 234 | of this license document, but changing it is not allowed. 235 | 236 | 237 | This version of the GNU Lesser General Public License incorporates 238 | the terms and conditions of version 3 of the GNU General Public 239 | License, supplemented by the additional permissions listed below. 240 | 241 | 0. Additional Definitions. 242 | 243 | As used herein, "this License" refers to version 3 of the GNU Lesser 244 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 245 | General Public License. 246 | 247 | "The Library" refers to a covered work governed by this License, 248 | other than an Application or a Combined Work as defined below. 249 | 250 | An "Application" is any work that makes use of an interface provided 251 | by the Library, but which is not otherwise based on the Library. 252 | Defining a subclass of a class defined by the Library is deemed a mode 253 | of using an interface provided by the Library. 254 | 255 | A "Combined Work" is a work produced by combining or linking an 256 | Application with the Library. The particular version of the Library 257 | with which the Combined Work was made is also called the "Linked 258 | Version". 259 | 260 | The "Minimal Corresponding Source" for a Combined Work means the 261 | Corresponding Source for the Combined Work, excluding any source code 262 | for portions of the Combined Work that, considered in isolation, are 263 | based on the Application, and not on the Linked Version. 264 | 265 | The "Corresponding Application Code" for a Combined Work means the 266 | object code and/or source code for the Application, including any data 267 | and utility programs needed for reproducing the Combined Work from the 268 | Application, but excluding the System Libraries of the Combined Work. 269 | 270 | 1. Exception to Section 3 of the GNU GPL. 271 | 272 | You may convey a covered work under sections 3 and 4 of this License 273 | without being bound by section 3 of the GNU GPL. 274 | 275 | 2. Conveying Modified Versions. 276 | 277 | If you modify a copy of the Library, and, in your modifications, a 278 | facility refers to a function or data to be supplied by an Application 279 | that uses the facility (other than as an argument passed when the 280 | facility is invoked), then you may convey a copy of the modified 281 | version: 282 | 283 | a) under this License, provided that you make a good faith effort to 284 | ensure that, in the event an Application does not supply the 285 | function or data, the facility still operates, and performs 286 | whatever part of its purpose remains meaningful, or 287 | 288 | b) under the GNU GPL, with none of the additional permissions of 289 | this License applicable to that copy. 290 | 291 | 3. Object Code Incorporating Material from Library Header Files. 292 | 293 | The object code form of an Application may incorporate material from 294 | a header file that is part of the Library. You may convey such object 295 | code under terms of your choice, provided that, if the incorporated 296 | material is not limited to numerical parameters, data structure 297 | layouts and accessors, or small macros, inline functions and templates 298 | (ten or fewer lines in length), you do both of the following: 299 | 300 | a) Give prominent notice with each copy of the object code that the 301 | Library is used in it and that the Library and its use are 302 | covered by this License. 303 | 304 | b) Accompany the object code with a copy of the GNU GPL and this license 305 | document. 306 | 307 | 4. Combined Works. 308 | 309 | You may convey a Combined Work under terms of your choice that, 310 | taken together, effectively do not restrict modification of the 311 | portions of the Library contained in the Combined Work and reverse 312 | engineering for debugging such modifications, if you also do each of 313 | the following: 314 | 315 | a) Give prominent notice with each copy of the Combined Work that 316 | the Library is used in it and that the Library and its use are 317 | covered by this License. 318 | 319 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 320 | document. 321 | 322 | c) For a Combined Work that displays copyright notices during 323 | execution, include the copyright notice for the Library among 324 | these notices, as well as a reference directing the user to the 325 | copies of the GNU GPL and this license document. 326 | 327 | d) Do one of the following: 328 | 329 | 0) Convey the Minimal Corresponding Source under the terms of this 330 | License, and the Corresponding Application Code in a form 331 | suitable for, and under terms that permit, the user to 332 | recombine or relink the Application with a modified version of 333 | the Linked Version to produce a modified Combined Work, in the 334 | manner specified by section 6 of the GNU GPL for conveying 335 | Corresponding Source. 336 | 337 | 1) Use a suitable shared library mechanism for linking with the 338 | Library. A suitable mechanism is one that (a) uses at run time 339 | a copy of the Library already present on the user's computer 340 | system, and (b) will operate properly with a modified version 341 | of the Library that is interface-compatible with the Linked 342 | Version. 343 | 344 | e) Provide Installation Information, but only if you would otherwise 345 | be required to provide such information under section 6 of the 346 | GNU GPL, and only to the extent that such information is 347 | necessary to install and execute a modified version of the 348 | Combined Work produced by recombining or relinking the 349 | Application with a modified version of the Linked Version. (If 350 | you use option 4d0, the Installation Information must accompany 351 | the Minimal Corresponding Source and Corresponding Application 352 | Code. If you use option 4d1, you must provide the Installation 353 | Information in the manner specified by section 6 of the GNU GPL 354 | for conveying Corresponding Source.) 355 | 356 | 5. Combined Libraries. 357 | 358 | You may place library facilities that are a work based on the 359 | Library side by side in a single library together with other library 360 | facilities that are not Applications and are not covered by this 361 | License, and convey such a combined library under terms of your 362 | choice, if you do both of the following: 363 | 364 | a) Accompany the combined library with a copy of the same work based 365 | on the Library, uncombined with any other library facilities, 366 | conveyed under the terms of this License. 367 | 368 | b) Give prominent notice with the combined library that part of it 369 | is a work based on the Library, and explaining where to find the 370 | accompanying uncombined form of the same work. 371 | 372 | 6. Revised Versions of the GNU Lesser General Public License. 373 | 374 | The Free Software Foundation may publish revised and/or new versions 375 | of the GNU Lesser General Public License from time to time. Such new 376 | versions will be similar in spirit to the present version, but may 377 | differ in detail to address new problems or concerns. 378 | 379 | Each version is given a distinguishing version number. If the 380 | Library as you received it specifies that a certain numbered version 381 | of the GNU Lesser General Public License "or any later version" 382 | applies to it, you have the option of following the terms and 383 | conditions either of that published version or of any later version 384 | published by the Free Software Foundation. If the Library as you 385 | received it does not specify a version number of the GNU Lesser 386 | General Public License, you may choose any version of the GNU Lesser 387 | General Public License ever published by the Free Software Foundation. 388 | 389 | If the Library as you received it specifies that a proxy can decide 390 | whether future versions of the GNU Lesser General Public License shall 391 | apply, that proxy's public statement of acceptance of any version is 392 | permanent authorization for you to choose that version for the 393 | Library. 394 | ``` -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | // CPC ROM emulator 2 | // Matt Callow March 2023 3 | // Updated Jan 2025 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "pico/stdlib.h" 11 | #include "pico/multicore.h" 12 | #include "pico/bootrom.h" 13 | #include "hardware/watchdog.h" 14 | #include "hardware/vreg.h" 15 | #include "hardware/pio.h" 16 | #include "hardware/structs/xip_ctrl.h" 17 | #include "hardware/regs/xip.h" 18 | #include "hardware/flash.h" 19 | #include "hardware/dma.h" 20 | #include "latch.pio.h" 21 | #include "bootsel_button.h" 22 | #include "flash.h" 23 | 24 | #undef DEBUG_TO_SERIAL 25 | #undef DEBUG_TO_FILE 26 | #define VER_MAJOR 3 27 | #define VER_MINOR 1 28 | #define VER_PATCH 1 29 | #ifndef CLOCK_SPEED_KHZ 30 | // overclock speed - pick the lowest freq that works reliably 31 | //#define CLOCK_SPEED_KHZ 200000 32 | //#define CLOCK_SPEED_KHZ 225000 33 | #define CLOCK_SPEED_KHZ 250000 34 | //#define CLOCK_SPEED_KHZ 260000 35 | #endif 36 | 37 | // not enough RAM for 16 38 | #define NUM_ROM_BANKS 12 39 | #define ROM_SIZE 16384 40 | // RAM copies of the ROMs 41 | #undef USE_XIP_CACHE_AS_RAM 42 | #ifdef USE_XIP_CACHE_AS_RAM 43 | static uint8_t *LOWER_ROM = (uint8_t *)0x15000000; 44 | #else 45 | static uint8_t LOWER_ROM[ROM_SIZE]; 46 | #endif 47 | static uint8_t UPPER_ROMS[NUM_ROM_BANKS][ROM_SIZE]; 48 | #define NO_ROM 0xff 49 | static volatile uint8_t rom_bank = 0; // index into upper ROMS, 0xff = no ROM 50 | static volatile uint16_t upper_roms = 0; // bitmask to indicate which ROM banks are active 51 | 52 | static FATFS filesystem; 53 | 54 | // values from the linker 55 | extern uint32_t __FLASH_START[]; 56 | extern uint32_t __FLASH_LEN[]; 57 | extern uint32_t __DRIVE_START[]; 58 | extern uint32_t __DRIVE_LEN[]; 59 | 60 | 61 | const uint32_t ADDRESS_BUS_MASK = 0x3fff; 62 | const uint32_t DATA_BUS_MASK = 0xff << 14; 63 | const uint32_t ROMEN_GPIO = 22; 64 | const uint32_t ROMEN_MASK = 1 << ROMEN_GPIO; 65 | const uint32_t A15_GPIO = 26; 66 | const uint32_t A15_MASK = 1 << A15_GPIO; 67 | const uint32_t WRITE_LATCH_GPIO = 27; 68 | const uint32_t WRITE_LATCH_MASK = 1 << WRITE_LATCH_GPIO; 69 | const uint32_t RESET_GPIO = 28; 70 | const uint32_t RESET_MASK = 1 << RESET_GPIO; 71 | const uint32_t FULL_MASK = ADDRESS_BUS_MASK|DATA_BUS_MASK|ROMEN_MASK|A15_MASK|WRITE_LATCH_MASK|RESET_MASK; 72 | 73 | #define CPC_ASSERT_RESET() do { debug("CPC_ASSERT_RESET"); gpio_set_dir(RESET_GPIO, GPIO_OUT); } while(0) 74 | #define CPC_RELEASE_RESET() do { debug("CPC_RELEASE_RESET"); gpio_set_dir(RESET_GPIO, GPIO_IN); } while(0) 75 | 76 | void fatal(int flashes) { 77 | while(1) { 78 | for (int i=0;i 125000) { // Long-push BOOTSEL button 121 | // turn on the LED 122 | gpio_put(PICO_DEFAULT_LED_PIN, true); 123 | // wait for button release 124 | while(bb_get_bootsel_button()); 125 | // turn off LED 126 | gpio_put(PICO_DEFAULT_LED_PIN, false); 127 | flash_format(); 128 | flash_init(); 129 | format(); 130 | // and reset 131 | watchdog_enable(1, 1); 132 | while(1); 133 | } 134 | } 135 | 136 | // flash the LED 137 | void led_task() { 138 | static int64_t timer = 0; 139 | static bool led = false; 140 | 141 | if (to_ms_since_boot(get_absolute_time()) - timer > 500) { 142 | gpio_put(PICO_DEFAULT_LED_PIN, led); 143 | timer = to_ms_since_boot(get_absolute_time()) + 500; 144 | led = !led; 145 | } 146 | } 147 | 148 | void usb_mode() { 149 | board_init(); 150 | tud_init(BOARD_TUD_RHPORT); 151 | stdio_init_all(); 152 | f_unmount(""); 153 | while(1) // the mainloop 154 | { 155 | button_task(); 156 | tud_task(); // device task 157 | led_task(); 158 | } 159 | } 160 | 161 | void debug(const char *msg) { 162 | #ifdef DEBUG_TO_FILE 163 | FIL fp; 164 | UINT l; 165 | f_open(&fp, "DEBUG.TXT", FA_WRITE|FA_OPEN_APPEND); 166 | f_printf(&fp, "%06d: %s\n", to_ms_since_boot(get_absolute_time()), msg); 167 | f_close(&fp); 168 | #endif 169 | } 170 | 171 | void fdebug(const char *fmt, ...) { 172 | #ifdef DEBUG_TO_FILE 173 | FIL fp; 174 | UINT l; 175 | char buf[256]; 176 | va_list args; 177 | va_start (args, fmt); 178 | vsprintf (buf, fmt, args); 179 | va_end (args); 180 | f_open(&fp, "DEBUG.TXT", FA_WRITE|FA_OPEN_APPEND); 181 | f_printf(&fp, "%06d: %s\n", to_ms_since_boot(get_absolute_time()), buf); 182 | f_close(&fp); 183 | #endif 184 | } 185 | 186 | 187 | #pragma pack(1) 188 | typedef struct { 189 | uint8_t user_number; 190 | char filename[8]; 191 | char ext[3]; 192 | char zeros[4]; 193 | uint8_t block_number; 194 | uint8_t last_block; 195 | uint8_t file_type; 196 | uint16_t data_length; 197 | uint16_t data_location; 198 | uint8_t first_block; 199 | uint16_t logical_length; 200 | uint16_t entry_address; 201 | char unused[36]; 202 | uint8_t real_length_h; 203 | uint16_t real_length; 204 | uint16_t checksum; 205 | char unused2[59]; 206 | } amsdos_header_t; 207 | #pragma pack() 208 | 209 | bool load_rom(const TCHAR* path, void* dest) { 210 | FIL fp; 211 | FRESULT fr; 212 | UINT bytes_read; 213 | FILINFO fno; 214 | fdebug("Loading %s", path); 215 | if (f_stat(path, &fno) != FR_OK) return false; 216 | if (f_open(&fp, path, FA_READ) != FR_OK) return false; 217 | fr = f_read(&fp, dest, 128, &bytes_read); 218 | if (fr != FR_OK) { 219 | f_close(&fp); 220 | fdebug("Failed to read header from %s", path); 221 | return false; 222 | } 223 | amsdos_header_t *hdr = (amsdos_header_t *)dest; 224 | uint16_t chksum = 0; 225 | for (int i=0;i<67;i++) { 226 | chksum += *(uint8_t *)(dest+i); 227 | } 228 | UINT btr; 229 | fdebug("Calculated chksum 0X%02X header chksum 0X%02X", chksum, hdr->checksum); 230 | if (chksum == hdr->checksum) { 231 | btr = hdr->logical_length; 232 | } else { 233 | f_rewind(&fp); 234 | btr = ROM_SIZE; 235 | } 236 | fr = f_read(&fp, dest, btr, &bytes_read); 237 | fdebug("btr=%d bytes_read=%d fr=%d", btr, bytes_read, fr); 238 | f_close(&fp); 239 | if (fr != FR_OK) return false; 240 | return true; 241 | } 242 | 243 | bool load_lower_rom(const TCHAR* path) { 244 | return load_rom(path, (void *)LOWER_ROM); 245 | } 246 | 247 | void remove_upper_rom(int bank) { 248 | upper_roms &= ~(1<= NUM_ROM_BANKS)) return false; 253 | bool ret = load_rom(path, (void *)UPPER_ROMS[bank]); 254 | if (ret) { 255 | upper_roms |= (1<= NUM_ROM_BANKS) rom_bank = NO_ROM; 357 | else if ((upper_roms & (1< 0) { 477 | num_params--; 478 | params[num_params] = latch; 479 | } 480 | if (num_params == 0) { 481 | switch (cmd) { 482 | case CMD_ROMIN: 483 | // get filename 484 | memset(buf, 0, sizeof(buf)); 485 | num_params = pio_sm_get_blocking(pio, sm) & 0xff; // get string length 486 | for (int i=0;i= NUM_ROM_BANKS) || (params[0] < 0)) { 491 | UPPER_ROMS[rom_bank][RESP_BUF+1] = 0; // status=OK 492 | UPPER_ROMS[rom_bank][RESP_BUF+2] = 1; // string 493 | strcpy((char *)&UPPER_ROMS[rom_bank][RESP_BUF+3], "Invalid bank number"); 494 | UPPER_ROMS[rom_bank][RESP_BUF]++; 495 | } else if (!load_upper_rom(buf, params[0])) { 496 | UPPER_ROMS[rom_bank][RESP_BUF+1] = 0; // status=OK 497 | UPPER_ROMS[rom_bank][RESP_BUF+2] = 1; // string 498 | strcpy((char *)&UPPER_ROMS[rom_bank][RESP_BUF+3], "Failed to load ROM"); 499 | UPPER_ROMS[rom_bank][RESP_BUF]++; 500 | } else { 501 | CPC_ASSERT_RESET(); 502 | sleep_ms(10); 503 | CPC_RELEASE_RESET(); 504 | } 505 | break; 506 | case CMD_ROMOUT: 507 | CPC_ASSERT_RESET(); 508 | sprintf((char *)&UPPER_ROMS[rom_bank][RESP_BUF+3], "ROMOUT,%d", params[0]); 509 | if (params[0] >= NUM_ROM_BANKS) params[0] = NUM_ROM_BANKS-1; 510 | if (params[0] < 0) params[0] = 0; 511 | remove_upper_rom(params[0]); 512 | UPPER_ROMS[rom_bank][RESP_BUF]++; 513 | CPC_RELEASE_RESET(); 514 | break; 515 | case CMD_LED: 516 | //printf("LED,%d latch=%d num_params=%d\n", params[0], latch, num_params); 517 | gpio_put(PICO_DEFAULT_LED_PIN, params[0]!=0); 518 | UPPER_ROMS[rom_bank][RESP_BUF+1] = 0; // status=OK 519 | UPPER_ROMS[rom_bank][RESP_BUF]++; 520 | break; 521 | 522 | } 523 | cmd = 0; 524 | } 525 | } 526 | } 527 | } 528 | 529 | void cpc_mode() { 530 | CPC_ASSERT_RESET(); 531 | upper_roms = 0; 532 | if (f_mount(&filesystem, "", 1)) { 533 | format(); 534 | } 535 | debug("Drive mounted"); 536 | if (!load_config("DEFAULT.CFG")) { 537 | debug("default config failed"); 538 | if (!load_lower_rom("OS_6128.ROM")) fatal(4); 539 | debug("OS loaded"); 540 | if (!load_upper_rom("BASIC_1.1.ROM", 0)) fatal(5); 541 | debug("basic loaded"); 542 | load_upper_rom("picorom.rom", 1); 543 | } 544 | set_sys_clock_khz(CLOCK_SPEED_KHZ, true); 545 | multicore_launch_core1(emulate); 546 | uint offset = pio_add_program(pio, &latch_program); 547 | latch_program_init(pio, sm, offset); 548 | gpio_put(PICO_DEFAULT_LED_PIN, 1); 549 | CPC_RELEASE_RESET(); 550 | handle_latch(); 551 | debug("ERROR - should never reach here"); 552 | } 553 | 554 | 555 | int main() { 556 | #ifdef USE_XIP_CACHE_AS_RAM 557 | // disable XIP cache - this frees up the cache RAM for variable storage 558 | hw_clear_bits(&xip_ctrl_hw->ctrl, XIP_CTRL_EN_BITS); 559 | #endif 560 | #ifdef DEBUG_TO_SERIAL 561 | board_init(); 562 | tud_init(BOARD_TUD_RHPORT); 563 | stdio_init_all(); 564 | /* 565 | for (int i=0;i<50;i++) { 566 | printf("%d ", i); 567 | sleep_ms(500); 568 | } 569 | */ 570 | //while (!tud_cdc_connected()) { sleep_ms(100); }; 571 | printf("tud_cdc_connected() %s\n", __TIMESTAMP__); 572 | printf("__FLASH_START 0x%08x __FLASH_LEN 0x%08x\n", __FLASH_START, __FLASH_LEN); 573 | printf("__DRIVE_START 0x%08x __DRIVE_LEN 0x%08x\n", __DRIVE_START, __DRIVE_LEN); 574 | #endif 575 | gpio_init_mask(FULL_MASK); 576 | gpio_set_dir_in_masked(FULL_MASK); 577 | gpio_pull_down(WRITE_LATCH_GPIO); // pull down for diode OR 578 | gpio_pull_up(ROMEN_GPIO); // pull ROMEN high. If this goes low within xxms of startup, then assume we are connected to CPC 579 | gpio_put(RESET_GPIO, 0); 580 | gpio_init(PICO_DEFAULT_LED_PIN); 581 | gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); 582 | gpio_put(PICO_DEFAULT_LED_PIN, 0); 583 | flash_init(); 584 | 585 | CPC_RELEASE_RESET(); 586 | 587 | while (to_ms_since_boot(get_absolute_time()) < 500) { 588 | // if ROMEN goes low, start emulation 589 | if (gpio_get(ROMEN_GPIO) == 0) { 590 | cpc_mode(); 591 | } 592 | } 593 | // If we get here assume that we are not plugged in to a CPC - emuulate a USB drive 594 | usb_mode(); 595 | } 596 | 597 | --------------------------------------------------------------------------------