Skip to content

Commit

Permalink
Dumping cassette to SD-card
Browse files Browse the repository at this point in the history
  • Loading branch information
ifilot committed Jul 29, 2024
1 parent 51f877d commit d17a826
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 35 deletions.
63 changes: 36 additions & 27 deletions cartroms/cassette-dump/fat32.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ uint32_t read_mbr(void) {
read_sector(0x00000000);

// grab the start address of the first partition
return ram_read_uint32_t(SDCACHE0 + 446 + 8);
return ram_read_uint32_t(SDCACHE0 + 0x1C6);
}

/**
Expand Down Expand Up @@ -136,7 +136,7 @@ void read_partition(uint32_t lba0) {

// consolidate variables
_fat_begin_lba = lba0 + _reserved_sectors;
_shadow_fat_begin_lba = _fat_begin_lba + _sectors_per_fat;
_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);

Expand Down Expand Up @@ -585,31 +585,31 @@ 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();
// 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();
// 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();
// 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,15 +628,15 @@ 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, "Final position: %08lX", finalpos);
// terminal_printtermbuffer();

sprintf(termbuffer, "Allocated size: %08lX", _fptr_size_allocated);
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");
// print("Allocating more clusters");
uint32_t bytes_per_cluster = _bytes_per_sector * _sectors_per_cluster;
uint32_t required_size = finalpos - _fptr_size_allocated;
uint8_t newclusters = required_size / bytes_per_cluster;
Expand All @@ -646,8 +646,8 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {
}
allocate_clusters(newclusters);

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

// build the linked list for the file
Expand All @@ -667,8 +667,8 @@ 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();
// sprintf(termbuffer, "Sector: %02X", i);
// terminal_printtermbuffer();

// check if the write position is in this current block
if(_fptr_pos >= blockpos && _fptr_pos < nextblockpos) {
Expand All @@ -687,7 +687,13 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {

//terminal_hexdump(SDCACHE0, 2, DUMP_EXTRAM);

// sprintf(termbuffer, "Writing bytes: %04X", bytes_to_write);
// 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);
Expand All @@ -706,12 +712,15 @@ void write_to_file(uint16_t extramptr, uint16_t nrbytes) {
// 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
if(finalpos == _fptr_pos) {

print("Data is written");
// print("Data is written");

// store updated file size if the file has grown in size
if(finalpos > _fptr_filesize) {
Expand Down
76 changes: 72 additions & 4 deletions cartroms/cassette-dump/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

// definitions
void init(void);
void show_sdcard_data(void);
void copy_current_tapeblock(void);
void parse_filename(char* filename);

void main(void) {
// initialize environment
Expand All @@ -53,7 +54,7 @@ void main(void) {
// check if folder is found
if(folder_addr != 0) {
print("Accessing DUMPS folder.");

set_current_folder(folder_addr);
} else {
print_error("No folder DUMPS found in root dir.");
for(;;){}
Expand Down Expand Up @@ -82,6 +83,7 @@ void main(void) {
// copied to internal memory
print_recall("Reading next program...");
tape_read_block();

if(memory[CASSTAT] != 0) {
sprintf(termbuffer, "%cStop reading tape, exit code: %c", COL_RED, memory[CASSTAT]);
terminal_printtermbuffer();
Expand Down Expand Up @@ -111,6 +113,23 @@ void main(void) {
// grab total blocks and start copying first block
uint8_t blockcounter = 0;

// create new file
char filename[12] = "________CAS"; // has terminating char '\0'
memcpy(filename, description, 8);
parse_filename(filename);
if(create_new_file(filename) == F_SUCCESS) {
uint32_t file_addr = find_in_folder(filename, F_FIND_FILE_NAME);
if(file_addr != 0) {
set_file_pointer(folder_addr, file_addr);
} else {
print_error("Could not create file pointer");
}
} else {
print_error("Unable to create file.");
for(;;){}
}
copy_current_tapeblock();

// consume all blocks
while(memory[BLOCKCTR] > 1) {
blockcounter++;
Expand All @@ -120,8 +139,10 @@ void main(void) {
if(memory[CASSTAT] != 0) {
sprintf(termbuffer, "Stop reading tape, exit code: %c", memory[CASSTAT]);
terminal_printtermbuffer();
for(;;){}

print_error("Reading the tape failed.");
}
copy_current_tapeblock();
}
sprintf(termbuffer, "%cCopied: %s to SD-CARD", COL_GREEN, description);
terminal_printtermbuffer();
Expand All @@ -137,7 +158,8 @@ void main(void) {
}
}
print("All done reading this tape.");
print("Swap tape to continue copying.");
print("Swap tape or swap side to continue");
print("copying from tape to SD-card.");
print("");
}

Expand Down Expand Up @@ -176,4 +198,50 @@ void init(void) {

// sd card successfully mounted
print("Partition 1 mounted");
}

/**
* @brief Copy the current tape block in memory to the SD-card
*
*/
void copy_current_tapeblock(void) {
ram_set(SDCACHE1, 0x00, 0x100); // wipe first 0x100 bytes
copy_to_ram(&memory[0x6030], SDCACHE1 + 0x30, 0x20); // set metadata
copy_to_ram(&memory[BUFFER], SDCACHE1 + 0x100, 0x400); // set data
write_to_file(SDCACHE1, 0x500);
}

/**
* @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_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';
}
}
23 changes: 19 additions & 4 deletions cartroms/cassette-dump/tape.asm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SECTION code_user

PUBLIC _tape_rewind
PUBLIC _tape_read_block
PUBLIC _tape_skip_forward

; constants for cassette instructions
CAS_INIT: equ $00
Expand Down Expand Up @@ -41,17 +42,19 @@ BUFFER: equ $6100 ; position to store tape data
; Rewind the cassette
;-------------------------------------------------------------------------------
_tape_rewind:
push ix
ld a,CAS_INIT
call TAPE
ld a,CAS_REWIND
call TAPE
pop ix
ret

;-------------------------------------------------------------------------------
; Read a single block from the tape to the buffer area
;-------------------------------------------------------------------------------
_tape_read_block:
push ix ; conserve iy because it is used as frame pointer
push ix ; conserve ix because it is used as frame pointer
ld a,(CASSTAT) ; load tape status
cp 'M' ; check for M
jp z,tprdexit
Expand All @@ -60,10 +63,22 @@ _tape_read_block:
ld hl,$0400
ld ($6032),hl
ld ($6034),hl
ld a,CAS_INIT
call TAPE
;ld a,CAS_INIT
;call TAPE
ld a,CAS_READ
call TAPE
tprdexit:
pop ix ; retrieve iy
pop ix ; retrieve ix
ret

;-------------------------------------------------------------------------------
; Read a single block from the tape to the buffer area
;-------------------------------------------------------------------------------
_tape_skip_forward:
push ix ; conserve ix because it is used as frame pointer
;ld a,CAS_INIT
;call TAPE
ld a,CAS_SKIPF
call TAPE
pop ix ; retrieve ix
ret
1 change: 1 addition & 0 deletions cartroms/cassette-dump/tape.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@

void tape_rewind(void);
void tape_read_block(void);
void tape_skip_forward(void);

#endif // _TAPE_H

0 comments on commit d17a826

Please sign in to comment.