// SPDX-License-Identifier: GPL-3.0-or-later #include #include typedef unsigned char Byte; typedef struct TapeBlock { Byte type, size, data[255]; } TapeBlock; static TapeBlock block; static Byte mode, position; static int readStatus; enum { CLOSED, READING, WRITING }; // The ROM's generic leader and block-write vectors take their parameters in // BASIC's scratch bytes. Save these and the caller's register/mask state. static asm void writeBlock(TapeBlock *p) { asm { ldx 2,s pshs cc,dp,y,u orcc #$50 clra tfr a,dp ldd <$7c pshs d ldd <$7e pshs d ldd <$80 pshs d lda <$85 ldb >$ff20 ; restore DAC and serial outputs after ROM writes pshs d ldd ,x++ std <$7c stx <$7e jsr [$a00c] ; gapped tape, leader at every block jsr [$a008] ; ROM generates type, count, data, checksum lda >$ff21 anda #$f7 sta >$ff21 ; stop motor between blocks puls d stb >$ff20 sta <$85 puls d std <$80 puls d std <$7e puls d std <$7c puls cc,dp,y,u } } // ROM BLKIN waits indefinitely for a transition on a damaged tape. Decode a // complete block with bounded edge waits instead. High pulse widths are // calibrated against the alternating leader in each gapped block. The fast // CAS reader supplies pulse widths in BASIC's normal period scratch byte. static asm Byte readBlock(TapeBlock *p, const Byte *wanted) { asm { ldx 2,s pshs cc,dp,y,u orcc #$50 clra tfr a,dp tfr x,y leas -12,s tfr s,u ; U frame: saved ROM period, threshold, min, max, bit counter, ; edge timeout multiplier, sync search budget, block address, wanted name. sty 8,u ldd 22,u ; second argument above 12 locals, 6 saved registers and PC std 10,u lda <$83 sta ,u clr <$83 lda >$ff21 ora #8 sta >$ff21 lbsr tapeRX_width ; discard the initial partial pulse lda #255 sta 2,u clr 3,u ldb #24 tapeRX_learn lbsr tapeRX_width cmpa 2,u bhs tapeRX_max sta 2,u tapeRX_max cmpa 3,u bls tapeRX_next sta 3,u tapeRX_next decb bne tapeRX_learn lda 3,u suba 2,u lsra adda 2,u sta 1,u tapeRX_scan ldy 8,u ldd #$ffff std 6,u clrb tapeRX_sync lbsr tapeRX_bit rorb ; bits arrive least significant first cmpb #$3c beq tapeRX_packet ldx 6,u leax -1,x stx 6,u bne tapeRX_sync lbra tapeRX_error tapeRX_packet lbsr tapeRX_byte stb ,y+ lbsr tapeRX_byte stb ,y+ stb 2,u ; remaining payload bytes addb -2,y stb 3,u ; additive checksum including type and count tst 2,u beq tapeRX_check tapeRX_data lbsr tapeRX_byte stb ,y+ addb 3,u stb 3,u dec 2,u bne tapeRX_data tapeRX_check lbsr tapeRX_byte cmpb 3,u bne tapeRX_error ; During named lookup keep the motor on through unrelated files. ; Nongapped program blocks can have only a single short sync leader. ldx 10,u beq tapeRX_ok ldy 8,u tst ,y+ bne tapeRX_scan lda ,y+ cmpa #15 bne tapeRX_error ldb #8 tapeRX_name lda ,y+ cmpa ,x+ bne tapeRX_scan decb bne tapeRX_name tapeRX_ok clrb bra tapeRX_finish tapeRX_error ldb #1 tapeRX_finish tfr u,s ; also unwinds any nested edge/byte calls lda ,u sta <$83 lda >$ff21 anda #$f7 sta >$ff21 leas 12,s puls cc,dp,y,u,pc tapeRX_byte lda #8 sta 4,u tapeRX_bits bsr tapeRX_bit rorb dec 4,u bne tapeRX_bits rts tapeRX_bit bsr tapeRX_width cmpa 1,u ; short high phase represents a one rts tapeRX_width lda #3 ; allow the initial motor silence sta 5,u ldx #0 tapeRX_rise lda >$ff20 lsra bcs tapeRX_high leax -1,x bne tapeRX_rise dec 5,u bne tapeRX_rise lbra tapeRX_error tapeRX_high ldx #0 lda #3 sta 5,u tapeRX_fall leax 1,x bne tapeRX_sample dec 5,u lbeq tapeRX_error tapeRX_sample lda >$ff20 lsra bcs tapeRX_fall lda <$83 bne tapeRX_measured ; accelerated reader supplied the period cmpx #255 blo tapeRX_short lda #255 rts tapeRX_short pshs x lda 1,s leas 2,s tapeRX_measured rts } } static int fileName(const char *name, Byte *dest) { // Avoid interfering with a BASIC file or an already active motor. if (mode || *(Byte *)0x78 || (*(Byte *)0xff21 & 8)) return VCC_TAPE_BUSY; if (!name || !*name) return VCC_TAPE_INVALID; memset(dest, ' ', 8); for (Byte i = 0; *name; ++i) { Byte ch = *name++; if (i == 8 || ch < 32 || ch > 126) return VCC_TAPE_INVALID; dest[i] = ch; } return VCC_TAPE_OK; } int vcc_tape_open_write(const char *name) { int status = fileName(name, block.data); if (status) return status; block.type = 0; block.size = 15; block.data[8] = 1; // BASIC data file block.data[9] = block.data[10] = 255; // ASCII, gapped memset(block.data + 11, 0, 4); writeBlock(&block); mode = WRITING; block.type = 1; block.size = 0; return VCC_TAPE_OK; } static void putByte(Byte ch) { block.data[block.size++] = ch; if (block.size == 255) { writeBlock(&block); block.size = 0; } } int vcc_tape_write_line(const char *line) { if (mode != WRITING || !line) return VCC_TAPE_INVALID; for (const char *s = line; *s; ++s) if ((Byte)*s < 32 || (Byte)*s > 126) return VCC_TAPE_INVALID; while (*line) putByte(*line++); putByte(13); return VCC_TAPE_OK; } int vcc_tape_open_read(const char *name) { Byte wanted[8]; int status = fileName(name, wanted); if (status) return status; if (readBlock(&block, wanted)) return VCC_TAPE_IO; if (block.data[8] != 1 || !block.data[9] || !block.data[10]) return VCC_TAPE_TYPE; mode = READING; block.size = position = 0; readStatus = VCC_TAPE_OK; return VCC_TAPE_OK; } int vcc_tape_read_line(char *line, unsigned capacity) { unsigned used = 0; Byte haveText = 0, overflow = 0; if (!line || !capacity) return VCC_TAPE_INVALID; line[0] = 0; if (mode != READING) return VCC_TAPE_INVALID; if (readStatus) return readStatus; for (;;) { while (position == block.size) { if (readBlock(&block, 0)) { readStatus = VCC_TAPE_IO; } else if (block.type == 255 && !block.size) { readStatus = VCC_TAPE_EOF; // BASIC can suppress its last CR with PRINT #-1,"TEXT";. if (haveText) { if (overflow) { line[0] = 0; return VCC_TAPE_LONG; } line[used] = 0; return VCC_TAPE_OK; } } else if (block.type != 1) { readStatus = VCC_TAPE_IO; } if (readStatus) { line[0] = 0; return readStatus; } position = 0; } Byte ch = block.data[position++]; if (ch == 13) { if (overflow) { line[0] = 0; return VCC_TAPE_LONG; } line[used] = 0; return VCC_TAPE_OK; } if (ch < 32 || ch > 126) { line[0] = 0; return readStatus = VCC_TAPE_IO; } haveText = 1; if (used < capacity - 1) line[used++] = ch; else overflow = 1; } } int vcc_tape_close(void) { if (!mode) return VCC_TAPE_INVALID; if (mode == WRITING) { if (block.size) writeBlock(&block); block.type = 255; block.size = 0; writeBlock(&block); } mode = CLOSED; return VCC_TAPE_OK; }