Skip to content

Commit

Permalink
Further polishing code for cassette dump
Browse files Browse the repository at this point in the history
  • Loading branch information
ifilot committed Jul 30, 2024
1 parent d17a826 commit cca963c
Show file tree
Hide file tree
Showing 66 changed files with 222 additions and 6,371 deletions.
Binary file added cartroms/cassette-dump/CASDUMP.PRG
Binary file not shown.
Binary file added cartroms/cassette-dump/CASSETTEDUMP.PRG
Binary file not shown.
31 changes: 27 additions & 4 deletions cartroms/cassette-dump/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
all: diagcart
all: cassettedump-slot1 cassettedump-prg

diagcart: main.c fat32.c memory.c terminal.c sdcard.c sdcard.asm util.asm
cassettedump-slot1: main.c fat32.c memory.c terminal.c sdcard.c sdcard.asm util.asm
cp -v crt_preamble_cart.asm crt_preamble.asm && \
zcc \
+embedded -clib=sdcc_iy \
fat32.c main.c memory.c terminal.c terminal_ext.c \
util.c sdcard.c sdcard.asm ram.asm util.asm tape.asm \
-startup=1 \
-pragma-define:CRT_ORG_CODE=0x1000 \
-pragma-define:CRT_ORG_DATA=0x6500 \
-pragma-define:CRT_ORG_DATA=0x6100 \
-pragma-define:REGISTER_SP=0x9FFF \
-pragma-define:CRT_STACK_SIZE=256 \
-pragma-define:CRT_INCLUDE_PREAMBLE=1 \
Expand All @@ -17,5 +18,27 @@ diagcart: main.c fat32.c memory.c terminal.c sdcard.c sdcard.asm util.asm
-SO3 -bn CASSETTEDUMP.BIN \
-create-app -m \
&& mv CASSETTEDUMP.bin CASSETTEDUMP.BIN \
&& sleep 0.01 \
&& wc -c < CASSETTEDUMP.BIN \
&& truncate -s 16k CASSETTEDUMP.BIN
&& truncate -s 16k CASSETTEDUMP.BIN

cassettedump-prg: main.c fat32.c memory.c terminal.c sdcard.c sdcard.asm util.asm
cp -v crt_preamble_prg.asm crt_preamble.asm && \
zcc \
+embedded -clib=sdcc_iy \
fat32.c main.c memory.c terminal.c terminal_ext.c \
util.c sdcard.c sdcard.asm ram.asm util.asm tape.asm \
-startup=0 \
-pragma-define:CRT_ORG_CODE=0xA000 \
-pragma-define:REGISTER_SP=0xDEFF \
-pragma-define:CLIB_FOPEN_MAX=0 \
-pragma-define:CRT_ON_EXIT=0x10002 \
-pragma-define:CRT_ENABLE_EIDI=0x22 \
-pragma-define:CRT_INCLUDE_PREAMBLE=1 \
--max-allocs-per-node2000 \
--opt-code-size \
-SO3 -bn CASDUMP.BIN \
-create-app -m \
&& mv CASDUMP.bin CASDUMP.PRG \
&& sleep 0.01 \
&& wc -c < CASDUMP.PRG
1 change: 1 addition & 0 deletions cartroms/cassette-dump/compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ if [[ "$OSTYPE" == "msys" ]]; then
winpty docker run -v `pwd | sed 's/\//\/\//g'`://src/ -it z88dk/z88dk make
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
docker run -v `pwd`:/src/ -it z88dk/z88dk make
python3 ../../scripts/signprg.py CASSETTEDUMP.PRG
else
echo "Unknown operating system"
fi
2 changes: 1 addition & 1 deletion cartroms/cassette-dump/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
#ifndef _CONFIG_H
#define _CONFIG_H

#define __VERSION__ "1.0.0"
#define __VERSION__ "0.8.0"

#endif
4 changes: 3 additions & 1 deletion cartroms/cassette-dump/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#define INPUTLENGTH 20

#define ROM_BANK_DEFAULT 0
#define ROM_BANK_DEFAULT 0
#define YES 0x00
#define NO 0x01

#endif
9 changes: 7 additions & 2 deletions cartroms/cassette-dump/crt_preamble.asm
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
;
;-------------------------------------------------------------------------------

; signature, byte count, checksum
DB 0x5E,0x00,0x00,0x00,0x00
; signature, byte count, CRC16 checksum
;
; * signature needs to be 0x50 to indicate that this is a runnable PRG file
; * byte count corresponds to the number of bytes of the program
; * checksum is the CRC-16 checksum
; * the byte count and CRC16 checksum need to be set later
DB 0x50,0x00,0x00,0x00,0x00

; name of the cartridge (11 bytes)
;DB 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ DB 0x5E,0x00,0x00,0x00,0x00

; name of the cartridge (11 bytes)
;DB 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
DB 'S' ,'D' ,'W' ,'R' ,'I' ,'T' ,'E', 'T', 'E', 'S', 'T'
DB 'C' ,'A' ,'S' ,'S' ,'E' ,'T' ,'T', 'E', 'D', 'M', 'P'

jp __Start
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
; * the byte count and CRC16 checksum need to be set later
DB 0x50,0x00,0x00,0x00,0x00

; name of the program (8+3 bytes)
DB 'H','E','L','L','O','W','O','R','P','R','G'
; name of the cartridge (11 bytes)
;DB 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
DB 'C' ,'A' ,'S' ,'S' ,'E' ,'T' ,'T', 'E', 'D', 'M', 'P'

; first address to call is thus $A210
jp __Start
139 changes: 54 additions & 85 deletions cartroms/cassette-dump/fat32.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,6 @@ void read_partition(uint32_t lba0) {
// partition signature, should be 0x55AA
uint16_t signature = ram_read_uint16_t(SDCACHE0 + 0x1FE);

// print data
// sprintf(termbuffer, "LBA partition 1:%c%08lX", COL_GREEN, lba0);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Bytes per sector:%c%i", COL_GREEN, _bytes_per_sector);
// terminal_printtermbuffer();

sprintf(termbuffer, "Sectors per cluster:%c%i", COL_GREEN, _sectors_per_cluster);
terminal_printtermbuffer();

// sprintf(termbuffer, "Reserved sectors:%c%i", COL_GREEN, _reserved_sectors);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Number of FATS:%c%i", COL_GREEN, _number_of_fats);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Sectors per FAT:%c%lu", COL_GREEN, _sectors_per_fat);
// terminal_printtermbuffer();

// calculate the total capacity on the partition
// each FAT holds a number of sectors
// each sector can refer to 128 clusters (128 x 32 = 512 bytes)
Expand All @@ -128,29 +109,12 @@ void read_partition(uint32_t lba0) {
sprintf(termbuffer, "Partition size:%c%lu MiB", COL_GREEN, (_sectors_per_fat * _sectors_per_cluster * _bytes_per_sector) >> 13 );
terminal_printtermbuffer();

// sprintf(termbuffer, "Root first cluster:%c%08lX", COL_GREEN, _root_dir_first_cluster);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Signature:%c%04X", COL_GREEN, signature);
// terminal_printtermbuffer();

// consolidate variables
_fat_begin_lba = lba0 + _reserved_sectors;
_shadow_fat_begin_lba = _fat_begin_lba + _sectors_per_fat + 1;
_sector_begin_lba = _fat_begin_lba + (_number_of_fats * _sectors_per_fat);
_lba_addr_root_dir = calculate_sector_address(_root_dir_first_cluster, 0);

// show prominent sector locations
sprintf(termbuffer, "FAT sector:%c%08lX", COL_GREEN, _fat_begin_lba);
terminal_printtermbuffer();
sprintf(termbuffer, "Shadow FAT sector:%c%08lX", COL_GREEN, _shadow_fat_begin_lba);
terminal_printtermbuffer();
// sprintf(termbuffer, "First cluster sector:%c%08lX", COL_GREEN, calculate_sector_address(0, 0));
// terminal_printtermbuffer();

// sprintf(termbuffer, "Number of FATS:%c%i", COL_GREEN, _number_of_fats);
// terminal_printtermbuffer();

// read first sector of first partition to establish volume name
read_sector(_lba_addr_root_dir);

Expand Down Expand Up @@ -585,31 +549,15 @@ void set_file_pointer(uint32_t folder_addr, uint32_t file_addr) {
// determine allocated file size
build_linked_list(file_addr);

// sprintf(termbuffer, "Linked list[0]: %08lX", _linkedlist[0]);
// terminal_printtermbuffer();
// sprintf(termbuffer, "Linked list[1]: %08lX", _linkedlist[1]);
// terminal_printtermbuffer();

uint8_t ctr = 0;
_fptr_size_allocated = 0;
while(_linkedlist[ctr] != 0xFFFFFFFF && ctr < F_LL_SIZE) {
_fptr_size_allocated += _sectors_per_cluster * _bytes_per_sector;
ctr++;
}
// sprintf(termbuffer, "Allocated size: %08lX KiB", _fptr_size_allocated);
// terminal_printtermbuffer();

// reset pointer positions
_fptr_pos = 0;

// sprintf(termbuffer, "File size: %08lX", _fptr_filesize);
// terminal_printtermbuffer();
// sprintf(termbuffer, "Allocated size: %08lX KiB", _fptr_size_allocated >> 10);
// terminal_printtermbuffer();
// sprintf(termbuffer, "Folder cluster: %08lX", _fptr_folder_addr);
// terminal_printtermbuffer();
// sprintf(termbuffer, "File cluster: %08lX", _fptr_cluster);
// terminal_printtermbuffer();
}

/**
Expand All @@ -628,12 +576,6 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {
// calculate final position after writing
uint32_t finalpos = _fptr_pos + nrbytes;

// sprintf(termbuffer, "Final position: %08lX", finalpos);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Allocated size: %08lX", _fptr_size_allocated);
// terminal_printtermbuffer();

// check if allocatable size needs to be expanded and do so if necessary
if(finalpos > _fptr_size_allocated) {
// print("Allocating more clusters");
Expand All @@ -645,9 +587,6 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {
newclusters++;
}
allocate_clusters(newclusters);

// print_recall("Press key to continue");
// wait_for_key();
}

// build the linked list for the file
Expand All @@ -667,9 +606,6 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {
// set position of next block
nextblockpos = blockpos + _bytes_per_sector;

// sprintf(termbuffer, "Sector: %02X", i);
// terminal_printtermbuffer();

// check if the write position is in this current block
if(_fptr_pos >= blockpos && _fptr_pos < nextblockpos) {
// determine how many bytes need to be written in this block
Expand All @@ -685,36 +621,15 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {
// read data from sector in SDCACHE0
read_sector(sector_addr);

//terminal_hexdump(SDCACHE0, 2, DUMP_EXTRAM);

// sprintf(termbuffer, "Writing to sector address: %08X", sector_addr);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Writing to sector: %02X", i);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Writing to cluster: %08X", _linkedlist[ctr]);
// terminal_printtermbuffer();

// sprintf(termbuffer, "Transfer from: %04X", extramptr);
// terminal_printtermbuffer();
// sprintf(termbuffer, "Transfer to: %04X", SDCACHE0 + (_fptr_pos - blockpos));
// terminal_printtermbuffer();

// copy new data into sector
ram_transfer(extramptr, SDCACHE0 + (_fptr_pos - blockpos), bytes_to_write);

//terminal_hexdump(SDCACHE0, 2, DUMP_EXTRAM);

// write sector from SDCACHE0
write_sector(sector_addr);

// increment pointers and write positions
extramptr += bytes_to_write;
_fptr_pos += bytes_to_write;

// print_recall("Press key to continue");
// wait_for_key();
}

// check if all data has been written, if so, stop function
Expand Down Expand Up @@ -766,4 +681,58 @@ void update_pointer_next_cluster(uint32_t src, uint32_t des) {
ram_write_uint32_t((src & 0b01111111) * 4, des);
write_sector(addr);
write_sector(addr + _sectors_per_fat);
}

/**
* @brief Convert any invalid characters. FAT32 8.3 filenames only support
* uppercase characters and certain special characters. This function
* transforms any lowercase to uppercase characters and transforms any
* invalid special characters to 'X'.
*
* @param filename filename to convert (only convert first 8 characters)
*/
void parse_fat32_filename(char* filename) {
for(uint8_t j=0; j<8; j++) {
if(filename[j] >= 'a' && filename[j] <= 'z') {
filename[j] -= 0x20;
}
if(filename[j] >= 'A' && filename[j] <= 'Z') {
continue;
}
if(filename[j] >= '0' && filename[j] <= '9') {
continue;
}
if(filename[j] >= '#' && filename[j] <= ')') {
continue;
}
if(filename[j] >= '^' && filename[j] <= '`') {
continue;
}
if(filename[j] == '!' || filename[j] == '-' || filename[j] == '@') {
continue;
}
if(filename[j] == '{' || filename[j] == '}' || filename[j] == '~' || filename[j] == ' ') {
continue;
}
filename[j] = 'X';
}
}

/**
* @brief Rename a FAT32 filename by grabbing the last digit, checking
* if it is a digit. If so, increment it by one, if not, replace
* it by a zero.
*
* @param filename filename to convert
*/
void rename_fat32_filename(char* filename) {
if(!(filename[7] >= '0' && filename[7] <= '9')) {
filename[7] = '0';
} else {
if(filename[7] == '9') {
filename[7] = '0';
} else {
filename[7] += 1;
}
}
}
19 changes: 19 additions & 0 deletions cartroms/cassette-dump/fat32.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,23 @@ void allocate_clusters(uint8_t nr_of_clusters);
*/
void update_pointer_next_cluster(uint32_t src, uint32_t des);

/**
* @brief Convert any invalid characters. FAT32 8.3 filenames only support
* uppercase characters and certain special characters. This function
* transforms any lowercase to uppercase characters and transforms any
* invalid special characters to 'X'.
*
* @param filename filename to convert (only convert first 8 characters)
*/
void parse_fat32_filename(char* filename);

/**
* @brief Rename a FAT32 filename by grabbing the last digit, checking
* if it is a digit. If so, increment it by one, if not, replace
* it by a zero.
*
* @param filename filename to convert
*/
void rename_fat32_filename(char* filename);

#endif // _FAT32_H
Loading

0 comments on commit cca963c

Please sign in to comment.