;=========================================================================== ; SPEEDSCRIPT 3.1 for the Commodore 64 -- by Charles Brannon ; (originally published as a type-in by COMPUTE!'s Gazette) ; ; Commented disassembly of speedscript.prg (load address $0801, 6156 bytes) ; ; This file re-assembles to a byte-identical copy of the original PRG: ; ca65 speedscript.asm -o speedscript.o ; ld65 -t none -o speedscript.prg speedscript.o ; (with "-t none" ld65 writes the code exactly as placed by .org) ; ; Labels are descriptive names chosen while reverse-engineering (many follow ; the names used in Brannon's published source where they are known). ; Labels of the form Lxxxx are minor branch targets inside a routine. ;=========================================================================== ; ; HOW IT STARTS ; ------------- ; A one-line BASIC program "10 SYS 2061" sits at $0801. SYS 2061 ($080D) ; enters the machine code, which initialises everything and drops into the ; main editing loop (MAIN). The program never returns to BASIC unless you ; press RUN/STOP+RESTORE and answer Y to "EXIT SpeedScript". ; ; MEMORY MAP ; ---------- ; $0400-$07E7 Screen. Row 0 is the "command/message line"; rows 1-24 ; show 24 lines of the document. ; $0801-$200C This program (BASIC stub, code, tables, message strings) ; $2008-$200C TEXSTART/TEXEND/TEXBUF low bytes live at the tail of the ; file; they are (re)initialised by INIT at start-up. ; $200D-$2471 Variables, input buffers, the printer line buffer, the ; header/footer buffers, hunt/replace strings, etc. ; (uninitialised RAM - not part of the file) ; $2500-$CEFF TEXT AREA: the document being edited (TEXSTART..TEXEND). ; $D000-$FEFF CUT/PASTE BUFFER (TEXBUF..BUFEND). This is the RAM hiding ; underneath the I/O chips and the KERNAL ROM, so the ; program flips the 6510 port ($01) to $34 (all RAM) while ; copying into or out of it, with the raster IRQ disabled. ; BASIC ROM is switched out permanently ($01=$36) so $A000-$BFFF is plain ; RAM for text; it is banked back in briefly ($01=$37) whenever the ; program wants BASIC's "print a 16-bit number" routine (LINPRT $BDCD). ; ; HOW TEXT IS STORED ; ------------------ ; The document is stored as SCREEN CODES, not PETSCII, so the screen ; refresh can copy bytes straight into screen memory. Conversions: ; ASTOIN PETSCII -> screen code INTOAS screen code -> PETSCII ; Special values inside the text: ; $1F "return mark" (end of paragraph; shows as a left-arrow) ; bit 7 set reverse-video character = embedded FORMAT COMMAND for ; the printer (entered with CTRL-pound) ; The cursor is drawn by flipping bit 7 of the character under it. ; Lines are not stored: REFRESH word-wraps the text into 40 column rows ; every time the screen is redrawn. ; ; KEYBOARD COMMANDS (see CTRLTBL / CTRLVEC) ; ----------------------------------------- ; CRSR right/left move one character CRSR up/down sentence back/fwd ; F1 / F2 word forward/back F3 / F4 sentence fwd/back ; F5 / F6 paragraph fwd/back F7 / F8 Load / Save ; HOME top of screen (twice = top of text) ; CTRL-Z end of text CLR (SHIFT-HOME) erase all text ; DEL delete char to left INST insert a space ; CTRL-F delete char under cursor (SHIFT-CTRL-F: delete the ; run of spaces from the cursor forward) ; CTRL-D Delete back: S)entence W)ord P)aragraph (to buffer) ; CTRL-E Erase forward: S/W/P, repeatable, RETURN exits ; (SHIFT-CTRL-E appends to the buffer instead of clearing) ; CTRL-R Retrieve (paste) the buffer at the cursor ; CTRL-K Kill (clear) the buffer ; CTRL-I toggle insert mode (command line turns light blue) ; CTRL-X transpose two characters CTRL-A change case ; CTRL-B / CTRL-L cycle background / text colour ; CTRL-H Hunt again (SHIFT-CTRL-H: set the hunt string) ; CTRL-J Replace found text (SHIFT-CTRL-J: set replacement) ; CTRL-G Global search & replace ; CTRL-P Print to printer (SHIFT-CTRL-P: Screen/Disk/Printer) ; CTRL-V Verify a saved file CTRL-4 disk directory ; CTRL-pound insert a format command (reverse character) ; CTRL-up-arrow send a DOS command / read the error channel ; CTRL-= show free memory ; RUN/STOP insert 5 spaces SHIFT-RUN/STOP 255 spaces ; SHIFT-RETURN new paragraph (2 return marks + 5-space indent) ; left-arrow key back up and blank the character ; RUN/STOP+RESTORE exit to BASIC (after a Y/N prompt) ; (Several CTRL-digit colour codes share a PETSCII value with the CTRL ; letters above, e.g. CTRL-2 = CTRL-E, CTRL-3 = CTRL-pound.) ; ; PRINTER FORMAT COMMANDS (reverse characters in the text, see FMTTAB) ; -------------------------------------------------------------------- ; l n left margin r n right margin t n top margin ; b n bottom margin p n page length s n line spacing ; x n page width (for centring) @ n first page number ; ? n start printing at page n n force next page ; h.. header line f.. footer line i.. ignore (comment) line ; m next line has no left margin w wait between pages ; a true-ASCII translation j add linefeed after CR ; g d:name / g t:name chain (link) to the next file and keep printing ; c centre line e flush right u underline on/off ; # print page number k=n define printer key k to send code n, ; then a reverse k in the text sends code n. ;=========================================================================== .setcpu "6502" ; --------------------------------------------------------------------------- ; External / RAM equates (zero page, program variables above the code, ; I/O registers and KERNAL entry points) ; --------------------------------------------------------------------------- R6510 = $01 ; 6510 I/O port: memory configuration ($36 = BASIC out, $34 = all RAM, $37 = normal) UNDERCURS = $02 ; the real character under the (blinking) cursor WINDCOLR = $0C ; background colour of the command line (row 0); read by IRQ_HANDLER COLRPTR = $14 ; pointer into colour RAM used by REFRESH FROML = $26 ; block-move source pointer (low/high) FROMH = $27 CURR = $39 ; CURSOR POSITION: address of the character under the cursor TEMP = $3B ; scratch byte / scratch pointer STATUS = $90 ; KERNAL I/O status byte (ST) STKEY = $91 ; KERNAL STOP-key flag ($7F = STOP pressed) KMSGFLG = $9D ; KERNAL message mode ($80 = show "PRESS PLAY..." etc.) DESTL = $9E ; block-move destination pointer (low/high) DESTH = $9F JIFFYLO = $A2 ; jiffy clock, lowest byte (60 Hz) LLEN = $B4 ; block-move length, low byte HLEN = $B5 ; block-move length, high byte (pages) SCRPTR = $C3 ; pointer into screen RAM used by REFRESH NDX = $C6 ; number of keys in keyboard buffer PNTR = $D3 ; KERNAL cursor column QTSW = $D4 ; KERNAL quote-mode flag INSRT = $D8 ; KERNAL pending-insert count TEX = $FB ; general text pointer FPTR = $FD ; pointer to the line being printed (OUTLINE) RPTFLG = $028A ; key repeat ($80 = all keys repeat) SHFLAG = $028D ; SHIFT/C=/CTRL flags: 1=SHIFT 2=C= 4=CTRL CINV = $0314 ; IRQ vector NMINV = $0318 ; NMI vector (RESTORE key) SCREEN = $0400 ; screen RAM BUFEND = $200E ; end of the cut/paste buffer ($FF00) LENTABLE = $2010 ; length of the top screen line (used to scroll one row) TOPLIN = $2011 ; address of the first character shown on screen row 1 MSGFLG = $2013 ; non-zero = a message is on the command line (clear it on next key) INSMODE = $2014 ; insert mode: 0 = overstrike, $0E = insert (also the command line colour) ENDPOS = $2015 ; end index for UMOVE inner loop / length of the line being printed FINPOS = $2016 ; scratch: index where a printed line ends LASTLINE = $2017 ; END OF DOCUMENT: address just past the last character LIMIT = $2019 ; max length allowed in INPUT INLEN = $201A ; length of the string typed in INPUT BOTSCR = $201B ; address just past the last character on screen LBUFF = $201D ; 40-byte buffer used to build one screen row INBUFF = $2045 ; 40-byte keyboard input buffer (INPUT) FILENAME = $206D ; file name buffer (with "0:" prefix) FNLEN = $2085 ; file name length SAVCURR = $2086 ; saved cursor (ERAS) BCD = $2088 ; ASCHEX: number packed as BCD HEX = $208A ; ASCHEX: binary result TPTR = $208C ; cut/paste buffer: next free byte BUFLEN = $208E ; number of bytes in the buffer GOBLEN = $2090 ; number of bytes being deleted FROMSAV = $2092 ; saved FROM pointer (DEL2) DESTSAV = $2094 ; saved DEST pointer (DEL2) HDLEN = $2096 ; header length (0 = no header) FTLEN = $2097 ; footer length (0 = no footer) LMARGIN = $2098 ; PRINT SETTINGS - 12 bytes set from DEFTAB: left margin RMARGIN = $2099 ; right margin PAGELENGTH = $209A ; page length (lines) TOPMARGIN = $209B ; top margin BOTMARGIN = $209C ; bottom margin (last line printed) SPACING = $209D ; line spacing CONTINUOUS = $209E ; 0 = wait for paper between pages PAGENUM = $209F ; current page number STARTNUM = $20A1 ; first page to actually print PAGEWIDTH = $20A3 ; page width (for centring) NOMARG = $20A4 ; 0 = suppress left margin on next line POS = $20A5 ; current print column LINE = $20A6 ; current print line on page YSAVE = $20A7 ; saved Y register SAVCHAR = $20A8 ; saved character INSLEN = $20A9 ; number of bytes to insert (lo; hi = DEVNO) DEVNO = $20AA ; print device number / INSLEN+1 NEEDASC = $20AB ; 1 = translate to true ASCII UNDERLINE = $20AC ; 1 = underline on FPOS = $20AD ; address where hunt string was last found ($FFxx = none) PCHR = $20AF ; saved character in PCHAR HUNTLEN = $20B0 ; hunt string length HUNTBUFF = $20B1 ; hunt string (30 bytes) REPLEN = $20CF ; replace string length REPBUFF = $20D0 ; replace string (30 bytes) CODEBUFFER = $20EE ; printer key codes: 128 bytes indexed by screen code PRBUFF = $216E ; one formatted print line HDBUFF = $226E ; header text FIRSTRUN = $236E ; $CB once SpeedScript has run (text kept on re-entry) FTBUFF = $236F ; footer text SAVCOL = $246F ; saved screen colour during printing LINEFEED = $2470 ; 10 = send LF after every CR BLINKFLAG = $2471 ; cursor blink phase (1 = cursor currently shown reversed) LINPRT = $BDCD ; BASIC ROM: print unsigned 16-bit number A=hi X=lo VIC_CR1 = $D011 ; VIC control reg 1 RASTER = $D012 ; VIC raster compare VIC_IRR = $D019 ; VIC interrupt latch VIC_IMR = $D01A ; VIC interrupt enable EXTCOL = $D020 ; border colour BGCOL0 = $D021 ; background colour CIA1_CRA = $DC0E ; CIA1 timer A control (the normal 60 Hz IRQ source) CIA2_ICR = $DD0D ; CIA2 interrupt control (NMI source) IRQ_NORMAL = $EA31 ; KERNAL standard IRQ (keyboard scan, clock) WARMSTART = $FE66 ; KERNAL warm start (as after RUN/STOP+RESTORE) NMI_RS232 = $FE72 ; KERNAL NMI handler for RS-232 IRQ_EXIT = $FEBC ; KERNAL: pull Y,X,A and RTI IOINIT = $FF84 ; KERNAL: init I/O devices SCNKEY = $FF9F ; KERNAL: scan keyboard SETLFS = $FFBA ; KERNAL: set logical file, device, secondary addr SETNAM = $FFBD ; KERNAL: set file name (A=len, X/Y=addr) OPEN = $FFC0 ; KERNAL: open file CLOSE = $FFC3 ; KERNAL: close file CHKIN = $FFC6 ; KERNAL: set input channel CHKOUT = $FFC9 ; KERNAL: set output channel CLRCHN = $FFCC ; KERNAL: restore default channels CHRIN = $FFCF ; KERNAL: input a byte CHROUT = $FFD2 ; KERNAL: output a byte LOAD_K = $FFD5 ; KERNAL: LOAD/VERIFY SAVE_K = $FFD8 ; KERNAL: SAVE STOP = $FFE1 ; KERNAL: test STOP key (Z=1 if pressed) GETIN = $FFE4 ; KERNAL: get a key / byte (0 if none) CLALL = $FFE7 ; KERNAL: close all files UMOVE_SRC = UMOVE_LOOP+1 ; UMOVE source address (patched at run time) UMOVE_SRC_HI = UMOVE_LOOP+2 ; UMOVE_DST = UMOVE_STA+1 ; UMOVE destination address (patched) UMOVE_DST_HI = UMOVE_STA+2 ; DMOVE_SRC = DMOVE_LOOP+1 ; DMOVE source address (patched) DMOVE_SRC_HI = DMOVE_LOOP+2 ; DMOVE_DST = DMOVE_STA+1 ; DMOVE destination address (patched) DMOVE_DST_HI = DMOVE_STA+2 ; .org $07FF .word $0801 ; PRG load address header (2 bytes, not loaded into memory) CTRLVEC_HI = CTRLVEC+1 ; high bytes of the CTRLVEC words FMTVEC_HI = FMTVEC+1 ; high bytes of the FMTVEC words ; =========================================================================== ; BASIC STUB: 10 SYS 2061 ; link pointer ($080B), line number 10, the SYS token ($9E), the text ; "2061", a 0 ending the line, and a $0000 link marking end of program. ; =========================================================================== BASIC_STUB: .byte $0B,$08,$0A,$00,$9E,$32,$30,$36,$31,$00,$00,$00 ; link to next line ($080B), line number 10, SYS token ($9E), "2061", 0, end-of-program link $0000 ; =========================================================================== ; START - entry point (SYS 2061) ; INIT sets up memory pointers. FIRSTRUN holds $CB after the first run; if ; it is anything else the text area is filled with spaces. This means that ; if you exit to BASIC and SYS 2061 again, your document is still there. ; =========================================================================== START: jsr INIT ; set up pointers, I/O, colours lda #$CB ; "already ran" signature value cmp FIRSTRUN ; Z=1 if we have run before ... sta FIRSTRUN ; ... (mark that we have run now; STA leaves flags alone) beq START2 ; second run: keep the old text jsr ERASE ; first run: blank the whole text area START2: jsr INIT2 ; clear buffer, install IRQ/NMI, show title jmp MAIN ; enter the editor main loop (never returns) ; =========================================================================== ; UMOVE - copy a block of memory UPWARD (ascending addresses) ; In: FROML/FROMH = source, DESTL/DESTH = destination, ; LLEN/HLEN = number of bytes ; Copies the lowest byte first, so it is safe when DEST < FROM (closing a ; gap / deleting text). For speed it pokes the source and destination ; addresses straight into the operands of the LDA/STA in the inner loop ; (self-modifying code), then copies whole 256-byte pages followed by the ; remaining LLEN bytes. ; =========================================================================== UMOVE: lda FROML ; patch source address into LDA abs,Y sta UMOVE_SRC lda FROMH sta UMOVE_SRC_HI lda DESTL ; patch destination address into STA abs,Y sta UMOVE_DST lda DESTH sta UMOVE_DST_HI ldx HLEN ; X = number of whole pages beq UMOVE_PART ; no whole pages -> just do the partial page UMOVE_PAGE: lda #$00 ; ENDPOS=0: inner loop runs until Y wraps (256 bytes) UMOVE_SETEND: sta ENDPOS ; ENDPOS = where the inner loop stops ldy #$00 UMOVE_LOOP: lda a:$0000,y ; LDA source,Y (operand patched above) UMOVE_STA: sta a:$0000,y ; STA dest,Y (operand patched above) iny cpy ENDPOS ; reached end index? bne UMOVE_LOOP inc UMOVE_SRC_HI ; next page of source inc UMOVE_DST_HI ; next page of destination cpx #$00 ; X=0 means the partial page was just done beq UMOVE_DONE dex ; one fewer whole page bne UMOVE_PAGE ; more whole pages to copy UMOVE_PART: lda LLEN ; then the leftover LLEN bytes (if any) bne UMOVE_SETEND UMOVE_DONE: rts ; =========================================================================== ; DMOVE - copy a block of memory DOWNWARD (descending addresses) ; Same parameters as UMOVE. Copies the highest byte first, so it is safe ; when DEST > FROM (opening a gap / inserting text). The operands are ; patched to point at the LAST page of the block, then it walks backward. ; (When LLEN<>0 the first partial pass copies offsets LLEN..0, i.e. one ; byte more than strictly needed - harmless.) ; =========================================================================== DMOVE: lda HLEN ; X = HLEN (whole pages) tax ora LLEN ; nothing to move at all? bne DMOVE_GO rts ; length 0 -> done DMOVE_GO: clc txa ; source high byte = FROMH + HLEN (last page) adc FROMH sta DMOVE_SRC_HI lda FROML ; source low byte sta DMOVE_SRC clc txa ; dest high byte = DESTH + HLEN adc DESTH sta DMOVE_DST_HI lda DESTL ; dest low byte sta DMOVE_DST inx ; X = HLEN+1 passes (partial page + whole pages) ldy LLEN ; start with the partial (top) page, index LLEN bne DMOVE_LOOP beq DMOVE_NEXTPG ; LLEN = 0: skip straight to the whole pages DMOVE_PAGE: ldy #$FF ; whole page: index $FF down to 0 DMOVE_LOOP: lda $2452,y ; LDA source,Y (patched) DMOVE_STA: sta $2457,y ; STA dest,Y (patched) dey cpy #$FF ; until Y wraps below 0 bne DMOVE_LOOP DMOVE_NEXTPG: dec DMOVE_SRC_HI ; step source and dest back one page dec DMOVE_DST_HI dex ; more pages? bne DMOVE_PAGE rts ; =========================================================================== ; REFRESH - redraw the text window (screen rows 1-24) from TOPLIN ; For each of the 24 rows it copies up to 40 characters into LBUFF, stopping ; early at a return mark. If the row is full it backs up to the last space ; so words are not split (word wrap); a word longer than 40 characters is ; simply cut at column 40. The row is copied to screen RAM, its colour RAM ; is set to TEXCOLR, and the rest of the row is padded with spaces. ; Side effects: LENTABLE = length of row 1 (lets CHECK scroll one row), ; BOTSCR = address just past the last visible character, ; WINDCOLR = INSMODE (command line colour shows insert mode), ; border = SCRCOL. ; =========================================================================== REFRESH: lda #$28 ; row 1 of the screen is $0428 ... sta SCRPTR sta COLRPTR ; ... and its colour RAM is $D828 lda #$04 sta SCRPTR+1 lda #$D8 sta COLRPTR+1 lda TOPLIN ; TEX = first character to show sta TEX lda TOPLIN+1 sta TEX+1 ldx #$01 ; X = screen row counter (1..24) lda INSMODE ; command line colour = 0 (black) or $0E (insert mode) sta WINDCOLR lda SCRCOL ; border = current screen colour sta EXTCOL REF_ROW: ldy #$00 REF_COPY: lda TEXCOLR ; set colour RAM for this column to text colour sta (COLRPTR),y lda (TEX),y ; fetch a text character sta LBUFF,y ; ... and build the row in LBUFF iny and #$7F ; (ignore reverse bit) cmp #$1F ; return mark ends the row beq REF_LINEEND cpy #$28 ; filled all 40 columns? bne REF_COPY dey ; row is full: look back for a space to break at REF_WRAP: lda (TEX),y and #$7F cmp #$20 beq REF_BRKSPACE dey ; not a space, keep looking bne REF_WRAP ldy #$27 ; no space anywhere: hard break at 40 chars REF_BRKSPACE: iny ; Y = number of characters on this row REF_LINEEND: sty TEMP ; save row length dey REF_TOSCREEN: lda LBUFF,y ; copy LBUFF to the screen, right to left sta (SCRPTR),y dey bpl REF_TOSCREEN ldy TEMP ; advance TEX past this row clc tya adc TEX sta TEX lda TEX+1 adc #$00 sta TEX+1 cpx #$01 ; first row? bne REF_PAD sty LENTABLE ; remember its length for one-line scrolling REF_PAD: cpy #$28 ; pad the rest of the row with spaces beq REF_NEXTROW lda #$20 sta (SCRPTR),y iny jmp REF_PAD REF_NEXTROW: clc ; next screen row: +40 on screen and colour pointers lda SCRPTR adc #$28 sta SCRPTR sta COLRPTR bcc REF_ROWINC inc SCRPTR+1 inc COLRPTR+1 REF_ROWINC: inx cpx #$19 ; done 24 rows? beq REF_DONE jmp REF_ROW REF_DONE: lda TEX ; BOTSCR = first character NOT on the screen sta BOTSCR lda TEX+1 sta BOTSCR+1 rts ; =========================================================================== ; ERASE - wipe the whole document ; Resets TOPLIN, LASTLINE and CURR to TEXSTART and fills every page from ; TEXSTART-1 up to TEXEND with spaces. ; =========================================================================== ERASE: lda TEXSTART ; TEX = TOPLIN = LASTLINE = CURR = TEXSTART sta TEX sta TOPLIN sta LASTLINE sta CURR lda TEXSTART+1 sta TEX+1 sta TOPLIN+1 sta LASTLINE+1 sta CURR+1 sec ; X = number of pages in the text area lda TEXEND+1 sbc TEXSTART+1 tax lda #$20 ; fill byte: space (screen code $20) ldy #$FF ; Y=$FF with TEX-256 -> address TEXSTART-1 dec TEX+1 sta (TEX),y ; blank the byte just before the text, too iny ; Y=0 inc TEX+1 ERASE_LOOP: sta (TEX),y ; fill a page with spaces iny bne ERASE_LOOP inc TEX+1 ; next page dex bne ERASE_LOOP sta (TEX),y ; and one byte at the very end rts ; =========================================================================== ; PRMSG - print a zero-terminated string. In: A = low, Y = high address. ; (Strings are PETSCII and may contain control codes. Max 255 chars.) ; =========================================================================== PRMSG: sta TEMP sty TEMP+1 ldy #$00 PRMSG_LOOP: lda (TEMP),y beq PRMSG_DONE ; 0 terminates the string jsr CHROUT iny bne PRMSG_LOOP PRMSG_DONE: rts ; =========================================================================== ; GETAKEY - wait until a key is pressed, return it in A ; =========================================================================== GETAKEY: jsr GETIN beq GETAKEY rts ; =========================================================================== ; INIT - one-time machine set-up ; Clear the screen, switch BASIC ROM out (RAM at $A000-$BFFF), and set the ; memory layout: TEXSTART=$2500 TEXEND=$CF00 TEXBUF=$D000 BUFEND=$FF00. ; Also clears insert mode and the hunt/replace strings. ; =========================================================================== INIT: lda #$93 ; PETSCII CLR: clear screen jsr CHROUT lda #$36 ; $36: BASIC ROM off, KERNAL + I/O on sta R6510 lda #$00 ; zero: sta INSMODE ; overstrike mode sta TEXSTART ; all four pointers start on a page boundary sta TEXEND sta TEXBUF sta BUFEND sta HUNTLEN ; no hunt string sta REPLEN ; no replace string lda #$24 ; TEXSTART high = $24+1 = $25 (text starts at $2500) clc adc #$01 sta TEXSTART+1 lda #$CF ; TEXEND = $CF00 (just below the I/O area) sta TEXEND+1 lda #$D0 ; TEXBUF = $D000 (buffer lives under I/O + KERNAL) sta TEXBUF+1 lda #$FF ; BUFEND = $FF00 sta BUFEND+1 sta FPOS+1 ; FPOS high = $FF: nothing found yet jmp IOINIT ; initialise CIAs etc. and return ; =========================================================================== ; INIT2 / INIT3 - (re)start the editor ; INIT2 also empties the cut/paste buffer. INIT3 is used after "erase ; all text". Sets key repeat for all keys, KERNAL messages on, installs the ; split-screen raster IRQ and the RESTORE-key NMI handler, puts the cursor ; at the top of the text, and shows the title + by-line. ; =========================================================================== INIT2: jsr KILLBUFF ; empty the cut/paste buffer INIT3: lda #$80 ; $80 = sta RPTFLG ; all keys auto-repeat sta KMSGFLG ; KERNAL control messages on ("PRESS PLAY ON TAPE") jsr IRQ_ON ; start the two-colour raster interrupt lda # our RESTORE handler sta NMINV lda #>NMI_HANDLER sta NMINV+1 lda TEXSTART ; cursor = start of text sta CURR lda TEXSTART+1 sta CURR+1 jsr SYSMSG ; show "SpeedScript 3.1" on the command line lda #MSG_BYLINE jsr PRMSG inc MSGFLG ; MSGFLG <> 0: the message is erased by the next key rts DEADBYTES: .byte $B1,$0B ; never executed (stray bytes: LDA ($0B),Y) ; =========================================================================== ; SYSMSG - clear the command line and show the title; MSGFLG = 0 ; =========================================================================== SYSMSG: jsr TOPCLR lda #MSG_TITLE jsr PRMSG lda #$00 sta MSGFLG rts ; =========================================================================== ; NMI_HANDLER - RUN/STOP + RESTORE ; If the NMI came from CIA2 (RS-232) pass it to the KERNAL. Otherwise put ; the real character back under the cursor and ask "EXIT SpeedScript: Are ; you sure?". Y = restore the normal IRQ and warm-start BASIC (the text ; stays in memory). Anything else = reset the stack and restart the editor. ; =========================================================================== NMI_HANDLER: pha ; save A, X, Y txa pha tya pha lda #$7F ; disable all CIA2 interrupts sta CIA2_ICR ldy CIA2_ICR ; read (and clear) CIA2 interrupt flags bpl NMI_RESTORE ; bit 7 clear = not a CIA2 interrupt = RESTORE key jmp NMI_RS232 ; CIA2 (RS-232) NMI: let the KERNAL handle it NMI_RESTORE: lda BLINKFLAG ; is the cursor currently drawn reversed? beq NMI_ASK lda UNDERCURS ; yes: put the real character back ldy #$00 sta (CURR),y NMI_ASK: lda #$02 ; command line red sta WINDCOLR jsr CLRCHN ; make sure I/O goes to screen/keyboard jsr TOPCLR lda #MSG_EXIT jsr PRMSG jsr YORN ; ": Are you sure? (Y/N):" bne NMI_STAY ; not Y: stay in SpeedScript jsr IRQ_OFF ; back to the normal 60 Hz IRQ sei lda #$7F ; A=$7F as the KERNAL NMI code expects jmp WARMSTART ; jump into KERNAL warm start -> BASIC READY. NMI_STAY: jsr IRQ_OFF ; restore normal IRQ (INIT2 re-installs ours) ldx #$FA ; reset the stack (we were deep inside an NMI) txs jsr INIT2 ; restart editor (also clears the buffer) jmp MAIN ; =========================================================================== ; TOPCLR - blank the command line (screen row 0) and HOME the cursor so ; the next CHROUT prints at the top-left ; =========================================================================== TOPCLR: ldx #$27 lda #$20 TOPCLR_LOOP: sta SCREEN,x dex bpl TOPCLR_LOOP lda #$13 ; PETSCII HOME jmp CHROUT ; =========================================================================== ; ASTOIN - convert a PETSCII character to a screen code ; screen = (A AND $3F) OR ((A AND $80) >> 1) ; e.g. "a" $41 -> $01, "A" $C1 -> $41, "0" $30 -> $30, $5F -> $1F ; =========================================================================== ASTOIN: pha and #$80 ; keep bit 7 ... lsr a ; ... and move it to bit 6 sta TEMP pla and #$3F ; low 6 bits ora TEMP ; combine rts ; =========================================================================== ; MAIN - THE EDITOR MAIN LOOP ; 1. Remember the character under the cursor. ; 2. Toggle its reverse bit (blink), redraw the screen. ; 3. Wait for a key; every 16 jiffies with no key go back to 2 (blink). ; 4. Put the real character back. ; 5. Dispatch: left-arrow -> move left, blank char ; RETURN -> becomes $5F (stored as return mark $1F) ; printable char -> store at cursor (insert if INSMODE or if ; we are on a return mark), advance cursor ; control char -> look up in CTRLTBL and call the routine ; Every command routine "returns" to MAIN because the dispatcher pushes ; MAIN-1 as a fake return address. ; =========================================================================== MAIN: ldy #$00 sty BLINKFLAG ; cursor not currently reversed lda (CURR),y ; save the real character under the cursor sta UNDERCURS CURSOR_FLASH: ldy #$00 lda (CURR),y ; flip reverse bit of the cursor character eor #$80 sta (CURR),y lda BLINKFLAG ; remember the blink phase eor #$01 sta BLINKFLAG jsr REFRESH ; redraw so the change shows WAITKEY: jsr GETIN ; key pressed? bne GOTKEY lda JIFFYLO ; no key: has bit 4 of the jiffy clock come on? and #$10 beq WAITKEY lda #$00 ; yes: restart the 16-jiffy timer ... sta JIFFYLO jmp CURSOR_FLASH ; ... and blink GOTKEY: tax ; X = key ldy #$00 lda UNDERCURS ; restore the true character under the cursor sta (CURR),y sty BLINKFLAG ; BLINKFLAG = 0 cpx #$5F ; left-arrow key? bne CHKMSG jsr LEFT ; yes: move left ... lda #$20 ; ... and overwrite with a space ldy #$00 sta (CURR),y jmp MAIN CHKMSG: lda MSGFLG ; is a message showing on the command line? beq NOMSG txa pha jsr SYSMSG ; yes: replace it with the title (keep the key) pla tax NOMSG: txa cmp #$0D ; RETURN? bne NOTCR ldx #$5F ; yes: use $5F, which ASTOIN turns into return mark $1F NOTCR: txa and #$7F ; ignore SHIFT bit ... cmp #$20 ; ... codes $00-$1F / $80-$9F are control keys bcc CONTROL cpx #$A0 ; SHIFT-SPACE ($A0)? bne TYPECHAR ldx #$20 ; treat as plain space TYPECHAR: txa pha ldy #$00 ; is the cursor on a return mark? lda (CURR),y cmp #$1F beq MAKEROOM lda INSMODE ; or is insert mode on? beq STORECHAR MAKEROOM: jsr INSCHAR ; either way: open a one-character gap first STORECHAR: pla jsr ASTOIN ; PETSCII -> screen code PUTCHR: ldy #$00 ; PUTCHR: store character at the cursor sta (CURR),y jsr REFRESH sec ; is cursor >= LASTLINE (typing at the very end)? lda CURR sbc LASTLINE sta TEMP lda CURR+1 sbc LASTLINE+1 ora TEMP bcc ADVANCE ; no: document length unchanged lda CURR ; yes: LASTLINE = CURR+1 (carry is set here) adc #$00 sta LASTLINE lda CURR+1 adc #$00 sta LASTLINE+1 ADVANCE: inc CURR ; advance the cursor bne TYPED_DONE inc CURR+1 TYPED_DONE: jsr CHECK ; keep cursor in range and on screen jmp MAIN ; CONTROL - look a control key up in CTRLTBL and jump to its routine. ; The routine address-1 is pushed and RTS jumps to it; MAIN-1 is pushed ; underneath so the routine's own RTS returns to MAIN. CONTROL: txa ldx CTRLTBL ; X = number of entries (39) CTRL_SEARCH: cmp CTRLTBL,x ; search the table from the end beq CTRL_FOUND dex bne CTRL_SEARCH jmp MAIN ; unknown key: ignore it CTRL_FOUND: dex ; X = (index-1)*2 = offset into CTRLVEC txa asl a tax lda #>(MAIN-1) ; push MAIN-1 (return address for the command) pha lda #<(MAIN-1) pha lda CTRLVEC_HI,x ; push command address-1, high byte first pha lda CTRLVEC,x pha rts ; "return" into the command routine ; CTRLTBL - first byte = entry count, then the 39 key codes. ; CTRLVEC - matching routine addresses (minus 1, for the RTS trick). CTRLTBL: .byte $27 ; count = 39 .byte $1D,$9D,$89,$85,$02,$0C,$8A,$86 ; CRSR-R CRSR-L F2 F1 CTRL-B CTRL-L F4 F3 .byte $14,$94,$04,$13,$09,$93,$87,$8B ; DEL INST CTRL-D HOME CTRL-I CLR F5 F6 .byte $05,$88,$8C,$16,$91,$11,$9F,$12 ; CTRL-E F7 F8 CTRL-V CRSR-U CRSR-D CTRL-4 CTRL-R .byte $18,$1A,$10,$1C,$1E,$06,$01,$0B ; CTRL-X CTRL-Z CTRL-P CTRL-pound CTRL-up CTRL-F CTRL-A CTRL-K .byte $08,$1F,$03,$83,$0A,$8D,$07 ; CTRL-H CTRL-= STOP SH-STOP CTRL-J SH-RET CTRL-G CTRLVEC: .word RIGHT-1 ; CRSR right: one character right .word LEFT-1 ; CRSR left: one character left .word WLEFT-1 ; F2: word left .word WRIGHT-1 ; F1: word right .word BORDER-1 ; CTRL-B: next border/background colour .word LETTERS-1 ; CTRL-L: next text colour .word SLEFT-1 ; F4: sentence left .word SRIGHT-1 ; F3: sentence right .word BACKSPACE-1 ; DEL: delete character to the left .word INSCHAR-1 ; INST: insert a space .word DELETE-1 ; CTRL-D: delete backward (S/W/P) .word HOME-1 ; HOME: top of screen / top of text .word INSTGL-1 ; CTRL-I: toggle insert mode .word CLEARALL-1 ; CLR: erase all text .word PARRIGHT-1 ; F5: paragraph right .word PARLEFT-1 ; F6: paragraph left .word ERAS-1 ; CTRL-E: erase forward (S/W/P) .word LOAD-1 ; F7: load .word SAVE-1 ; F8: save .word VERIFY-1 ; CTRL-V: verify .word SLEFT-1 ; CRSR up: sentence left .word SRIGHT-1 ; CRSR down: sentence right .word CATALOG-1 ; CTRL-4: disk directory .word RETRIEVE-1 ; CTRL-R: retrieve (paste) buffer .word TRANSPOSE-1 ; CTRL-X: transpose characters .word GOEND-1 ; CTRL-Z: end of text .word PRINT-1 ; CTRL-P: print .word FORMAT-1 ; CTRL-pound: insert format command .word DISKCMD-1 ; CTRL-up arrow: DOS command .word DELFWD-1 ; CTRL-F: delete forward .word CASECHG-1 ; CTRL-A: change case .word KILLBUFF-1 ; CTRL-K: kill buffer .word HUNT-1 ; CTRL-H: hunt .word FREEMEM-1 ; CTRL-=: free memory .word TAB5-1 ; RUN/STOP: insert 5 spaces .word INS255-1 ; SHIFT-RUN/STOP: insert 255 spaces .word REPSTART-1 ; CTRL-J: replace .word NEWPARA-1 ; SHIFT-RETURN: new paragraph .word GLOBAL-1 ; CTRL-G: global search & replace ; =========================================================================== ; CHECK - keep the cursor legal and visible ; First CHECK2 clamps CURR into TEXSTART..LASTLINE. Then: ; * cursor above the screen -> make the cursor's address the new TOPLIN ; * cursor below the screen -> scroll down one row at a time (TOPLIN += ; LENTABLE, REFRESH) until it is visible again. ; =========================================================================== CHECK: jsr CHECK2 sec ; CURR < TOPLIN ? lda CURR sbc TOPLIN lda CURR+1 sbc TOPLIN+1 bcs CHKBOTTOM ; no: check the bottom sec ; already at the top of the text? then nothing to do lda TOPLIN sbc TEXSTART sta TEMP lda TOPLIN+1 sbc TEXSTART+1 ora TEMP beq CHKBOTTOM lda CURR ; scroll up: screen now starts at the cursor sta TOPLIN lda CURR+1 sta TOPLIN+1 jsr REFRESH CHKBOTTOM: sec ; BOTSCR - CURR ... lda BOTSCR sbc CURR sta TEX lda BOTSCR+1 sbc CURR+1 sta TEX+1 ora TEX beq SCROLLDN ; zero: cursor just off the bottom -> scroll bcs CHECK_DONE ; positive: cursor is on screen, done SCROLLDN: clc ; scroll down one row: TOPLIN += length of row 1 lda TOPLIN adc LENTABLE sta TOPLIN lda TOPLIN+1 adc #$00 sta TOPLIN+1 jsr REFRESH ; redraw (recomputes LENTABLE and BOTSCR) jmp CHKBOTTOM ; and test again CHECK_DONE: rts ; CHECK2 - clamp pointers: LASTLINE <= TEXEND, TEXSTART <= CURR <= LASTLINE CHECK2: sec ; LASTLINE >= TEXEND ? lda LASTLINE sbc TEXEND sta TEMP lda LASTLINE+1 sbc TEXEND+1 ora TEMP bcc CHK_LOW lda TEXEND ; yes: LASTLINE = TEXEND sta LASTLINE lda TEXEND+1 sta LASTLINE+1 CHK_LOW: sec ; CURR < TEXSTART ? lda CURR sbc TEXSTART sta TEMP lda CURR+1 sbc TEXSTART+1 ora TEMP bcs CHK_HIGH lda TEXSTART ; yes: CURR = TEXSTART sta CURR lda TEXSTART+1 sta CURR+1 rts CHK_HIGH: sec ; CURR >= LASTLINE ? lda CURR sbc LASTLINE sta TEMP lda CURR+1 sbc LASTLINE+1 ora TEMP bcs CLAMP_END rts CLAMP_END: lda LASTLINE ; yes: CURR = LASTLINE sta CURR lda LASTLINE+1 sta CURR+1 rts ; RIGHT / LEFT - move the cursor one character (CRSR keys) RIGHT: inc CURR bne RIGHT_DONE inc CURR+1 RIGHT_DONE: jmp CHECK LEFT: lda CURR bne LEFT_LO dec CURR+1 LEFT_LO: dec CURR jmp CHECK ; =========================================================================== ; WLEFT - move to the start of the previous word (F2) ; TEX = CURR-256 and Y=$FF so (TEX),Y is the byte before the cursor. ; Skip back over spaces/return marks, then over the word itself, then ; place the cursor just after the space found. (Looks back at most 255 ; characters.) ; =========================================================================== WLEFT: lda CURR sta TEX lda CURR+1 sta TEX+1 dec TEX+1 ldy #$FF WL_SKIPSP: lda (TEX),y ; skip spaces ... cmp #$20 beq WL_NEXT cmp #$1F ; ... and return marks bne WL_SKIPWORD WL_NEXT: dey bne WL_SKIPSP WL_SKIPWORD: lda (TEX),y ; now skip the word: stop at a space ... cmp #$20 beq SETCURR cmp #$1F ; ... or a return mark beq SETCURR dey bne WL_SKIPWORD rts ; ran out of range: leave the cursor alone SETCURR: sec ; SETCURR: CURR = TEX+Y+1 (character after the break) tya adc TEX sta CURR lda TEX+1 adc #$00 sta CURR+1 jmp CHECK ; =========================================================================== ; WRIGHT - move to the start of the next word (F1) ; Skip forward over the current word, then over spaces/return marks. ; If we run past LASTLINE, go to the end of the text. ; =========================================================================== WRIGHT: ldy #$00 WR_SKIPWORD: lda (CURR),y ; skip the rest of this word cmp #$20 beq WR_SKIPSP cmp #$1F beq WR_SKIPSP iny bne WR_SKIPWORD rts WR_SKIPSP: iny ; skip spaces/returns, crossing page boundaries bne WR_TEST inc CURR+1 lda CURR+1 cmp LASTLINE+1 ; past the end of the text? bcc WR_TEST bne ENDTEX ; yes: go to the end WR_TEST: lda (CURR),y cmp #$20 beq WR_SKIPSP cmp #$1F beq WR_SKIPSP ADDYCURR: clc ; ADDYCURR: CURR += Y, then CHECK tya adc CURR sta CURR lda CURR+1 adc #$00 sta CURR+1 jmp CHECK ENDTEX: lda LASTLINE ; ENDTEX: cursor = LASTLINE (end of text) sta CURR lda LASTLINE+1 sta CURR+1 jmp CHECK ; GOEND - CTRL-Z: jump to the end of the document. The screen is ; positioned to start about 4 pages (1K) before the end, then CHECK ; scrolls the cursor into view. GOEND: lda #$00 sta TOPLIN ; TOPLIN low = 0 lda LASTLINE+1 ; TOPLIN high = LASTLINE high - 4 ... sec sbc #$04 cmp TEXSTART+1 ; ... but not before the start of text bcs GOEND_SET lda TEXSTART+1 GOEND_SET: sta TOPLIN+1 jsr REFRESH jmp ENDTEX ; BORDER - CTRL-B: next screen colour (0-15) for border and text window. ; LETTERS - CTRL-L: next text (character) colour. SCRCOL and TEXCOLR are ; single data bytes embedded in the code. BORDER: inc SCRCOL lda SCRCOL and #$0F sta SCRCOL rts SCRCOL: .byte $0C ; SCRCOL: border/background colour (12 = medium grey) LETTERS: inc TEXCOLR lda TEXCOLR and #$0F sta TEXCOLR jmp REFRESH TEXCOLR: .byte $0B ; TEXCOLR: text colour (11 = dark grey) ; =========================================================================== ; SLEFT - move to the start of the previous sentence (F4 / CRSR up) ; A sentence ends with ".", "!", "?" or a return mark. Skip back over the ; punctuation the cursor is sitting just after, find the previous one, ; then skip spaces forward to the first letter of the sentence. Goes to ; TEXSTART if no earlier sentence end is found. ; =========================================================================== SLEFT: lda CURR sta TEX lda CURR+1 sta TEX+1 dec TEX+1 ldy #$FF SL_SKIPPUNC: lda (TEX),y ; skip trailing punctuation / return marks cmp #$2E beq SL_NEXT cmp #$21 beq SL_NEXT cmp #$3F beq SL_NEXT cmp #$1F bne SL_FINDPUNC SL_NEXT: dey bne SL_SKIPPUNC rts SL_FINDPUNC: lda (TEX),y ; search back for . ! ? or return mark cmp #$2E beq SL_FOUND cmp #$21 beq SL_FOUND cmp #$3F beq SL_FOUND cmp #$1F beq SL_FOUND dey bne SL_FINDPUNC dec TEX+1 ; crossed into the previous page lda TEX+1 cmp TEXSTART ; before the start of text? bcs SL_FINDPUNC jmp TOPTEXT ; yes: go to the top of the text SL_FOUND: sty TEMP ; found the end of the previous sentence dec TEMP SL_SKIPSP: iny ; skip spaces forward to the first word beq SL_PAGEWRAP lda (TEX),y cmp #$20 beq SL_SKIPSP dey ; back up one: SETCURR adds 1 jmp SETCURR SL_PAGEWRAP: ldy TEMP ; Y wrapped to 0 while skipping: keep searching back jmp SL_FINDPUNC TOPTEXT: lda TEXSTART ; TOPTEXT: cursor = TEXSTART sta CURR lda TEXSTART+1 sta CURR+1 jmp CHECK ; =========================================================================== ; SRIGHT - move to the start of the next sentence (F3 / CRSR down) ; Search forward for ".", "!", "?" or a return mark, then skip any run of ; spaces/punctuation; stop at the first other character. ; =========================================================================== SRIGHT: ldy #$00 SR_FINDPUNC: lda (CURR),y cmp #$2E beq SR_SKIP cmp #$21 beq SR_SKIP cmp #$3F beq SR_SKIP cmp #$1F beq SR_SKIP iny bne SR_FINDPUNC inc CURR+1 ; next page; stop at the end of the text lda CURR+1 cmp LASTLINE+1 beq SR_FINDPUNC bcc SR_FINDPUNC jmp ENDTEX SR_SKIP: iny ; skip spaces and punctuation after the sentence end bne SR_TEST inc CURR+1 lda CURR+1 cmp LASTLINE+1 bcc SR_TEST beq SR_TEST jmp ENDTEX SR_TEST: lda (CURR),y cmp #$20 beq SR_SKIP cmp #$2E beq SR_SKIP cmp #$21 beq SR_SKIP cmp #$3F beq SR_SKIP cmp #$1F beq SR_SKIP jmp ADDYCURR ; CURR += Y ; =========================================================================== ; KILLBUFF - CTRL-K: empty the cut/paste buffer (TPTR = TEXBUF) and ; show "Buffer Cleared" ; =========================================================================== KILLBUFF: lda TEXBUF sta TPTR lda TEXBUF+1 sta TPTR+1 jsr TOPCLR lda #MSG_BUFCLR jsr PRMSG lda #$01 sta MSGFLG rts ; =========================================================================== ; DELETION ENGINE ; DELSETUP: if the cursor is at TEXSTART there is nothing to delete ; behind it, so it pops its caller's return address and returns ; straight to MAIN. Otherwise FROM = CURR (end of the doomed text). ; The caller then moves CURR back (one char / word / sentence / paragraph) ; and calls DEL1. ; ; DEL1: DEST = CURR (start), GOBLEN = FROM - DEST (bytes to remove). ; DEL2: If the deleted text fits, append it to the cut/paste buffer at ; TPTR (with all-RAM banking since the buffer is under I/O), then ; close the gap by UMOVEing everything after it down, and reduce ; LASTLINE. If the buffer would overflow: "Buffer Full", nothing ; is deleted and the keyboard buffer is flushed. ; =========================================================================== DELSETUP: sec ; CURR = TEXSTART ? lda CURR sbc TEXSTART sta TEMP lda CURR+1 sbc TEXSTART+1 ora TEMP bne DELSETUP2 pla ; yes: discard return address -> back to MAIN pla rts DELSETUP2: lda CURR ; FROM = CURR sta FROML lda CURR+1 sta FROMH rts DEL1: sec ; DEST = CURR; GOBLEN = FROM - CURR lda CURR sta DESTL eor #$FF ; (EOR #$FF + ADC with carry set = subtract) adc FROML sta GOBLEN lda CURR+1 sta DESTH eor #$FF adc FROMH sta GOBLEN+1 DEL2: lda FROML ; save FROM and DEST sta FROMSAV lda FROMH sta FROMSAV+1 lda DESTL sta DESTSAV sta FROML ; FROM = DEST: start of the text to copy to the buffer lda DESTH sta DESTSAV+1 sta FROMH sec ; will TPTR + GOBLEN pass BUFEND? lda GOBLEN+1 adc TPTR+1 cmp BUFEND+1 bcc DEL3 jsr TOPCLR ; yes: "Buffer Full" lda #MSG_BUFFULL jsr PRMSG lda #$01 sta MSGFLG lda #$00 ; flush the keyboard buffer sta NDX rts DEL3: lda TPTR ; DEST = TPTR (end of what is in the buffer) sta DESTL lda TPTR+1 sta DESTH lda GOBLEN ; length = GOBLEN, and TPTR += GOBLEN sta LLEN clc adc TPTR sta TPTR lda GOBLEN+1 sta HLEN adc TPTR+1 sta TPTR+1 lda #$00 ; stop raster IRQs while I/O is banked out sta VIC_IMR lda #$34 ; $34: all RAM (buffer is under I/O/KERNAL) sta R6510 jsr UMOVE ; copy deleted text into the buffer lda #$36 ; back to normal banking sta R6510 lda #$01 ; raster IRQ on again sta VIC_IMR lda FROMSAV ; FROM = old CURR (end), DEST = new CURR (start) sta FROML lda FROMSAV+1 sta FROMH lda DESTSAV sta DESTL lda DESTSAV+1 sta DESTH sec ; length = LASTLINE - DEST lda LASTLINE sbc DESTL sta LLEN lda LASTLINE+1 sbc DESTH sta HLEN jsr UMOVE ; close the gap sec ; LASTLINE -= GOBLEN lda LASTLINE sbc GOBLEN sta LASTLINE lda LASTLINE+1 sbc GOBLEN+1 sta LASTLINE+1 rts ; BACKSPACE - DEL key: delete the character left of the cursor. It goes ; through the buffer machinery, but TPTR is then backed up one byte so ; single deleted characters do not accumulate in the buffer. BACKSPACE: jsr DELSETUP jsr LEFT jsr DEL1 TPTRDEC: sec ; TPTRDEC: forget the last byte put in the buffer lda TPTR sbc #$01 sta TPTR lda TPTR+1 sbc #$00 sta TPTR+1 rts ; DELFWD - CTRL-F: delete the character under the cursor (move right, then ; delete left). SHIFT+CTRL-F instead deletes the run of spaces at the ; cursor (DELSPACES). DELFWD: lda SHFLAG ; SHIFT and CTRL both held (1+4)? cmp #$05 bne DELFWD1 jmp DELSPACES DELFWD1: jsr RIGHT jsr DELSETUP jsr LEFT jsr DEL1 jmp TPTRDEC ; =========================================================================== ; DELETE - CTRL-D: "Delete (S,W,P)" - delete the previous Sentence, Word ; or Paragraph into the (first emptied) buffer ; =========================================================================== DELETE: jsr KILLBUFF ; start with an empty buffer lda #$02 ; command line red sta WINDCOLR jsr TOPCLR lda #MSG_DELETE jsr PRMSG jsr GETAKEY pha jsr SYSMSG pla and #$BF ; clear bit 6: S/W/P -> $13/$17/$10 cmp #$17 ; W: word bne DEL_NOTW jsr DELSETUP jsr WLEFT jmp DEL1 DEL_NOTW: cmp #$13 ; S: sentence bne DEL_NOTS jsr DELSETUP jsr SLEFT jmp DEL1 DEL_NOTS: cmp #$10 ; P: paragraph bne DEL_EXIT jsr DELSETUP jsr PARLEFT jmp DEL1 DEL_EXIT: rts ; HOME - first press: cursor to the top of the screen; if it is already ; there, go to the top of the text. HOME: sec lda CURR sbc TOPLIN sta TEMP lda CURR+1 sbc TOPLIN+1 ora TEMP beq HOME_TOP lda TOPLIN sta CURR lda TOPLIN+1 sta CURR+1 rts HOME_TOP: lda TEXSTART sta CURR lda TEXSTART+1 sta CURR+1 jmp CHECK ; DELSPACES - SHIFT-CTRL-F: remove all spaces from the cursor forward up to ; the next non-space (not copied to the buffer). DELSPACES: lda CURR sta TEX sta DESTL lda CURR+1 sta TEX+1 sta DESTH ldy #$00 DS_SCAN: lda (TEX),y ; scan forward while spaces cmp #$20 bne DS_FOUND iny bne DS_SCAN lda TEX+1 ; next page, unless past the end of the text cmp LASTLINE+1 bcc DS_NEXTPG lda LASTLINE sta TEX lda LASTLINE+1 sta TEX+1 ldy #$00 jmp DS_FOUND DS_NEXTPG: inc TEX+1 jmp DS_SCAN DS_FOUND: clc ; FROM = first non-space tya adc TEX sta FROML lda #$00 adc TEX+1 sta FROMH sec ; length = LASTLINE - CURR lda LASTLINE sbc DESTL sta LLEN lda LASTLINE+1 sbc DESTH sta HLEN sec ; GOBLEN = number of spaces removed lda FROML sbc DESTL sta GOBLEN lda FROMH sbc DESTH sta GOBLEN+1 jsr UMOVE ; slide the text down over the spaces sec ; LASTLINE -= GOBLEN lda LASTLINE sbc GOBLEN sta LASTLINE lda LASTLINE+1 sbc GOBLEN+1 sta LASTLINE+1 rts ; INS255 - SHIFT-RUN/STOP: insert 255 spaces. ; TAB5 - RUN/STOP: insert 5 spaces and move past them. ; INSSPACES - open a gap of INSLEN bytes at the cursor, fill with spaces. INS255: lda #$FF sta INSLEN jmp INSSPACES TAB5: lda #$05 sta INSLEN jsr INSSPACES lda (CURR),y cmp #$20 bne TAB_DONE iny TAB_DONE: jmp ADDYCURR INSSPACES: lda #$00 sta INSLEN+1 ; INSLEN high byte = 0 jsr INSBLOCK ; open the gap lda #$20 ldx INSLEN ldy #$00 INSS_FILL: sta (CURR),y iny dex bne INSS_FILL rts ; NEWPARA - SHIFT-RETURN: insert two return marks (a blank line) followed ; by a 5-space paragraph indent. NEWPARA: jsr INSCHAR jsr INSCHAR lda #$1F ; two return marks ldy #$00 sta (CURR),y iny sta (CURR),y jsr REFRESH jsr RIGHT jsr RIGHT jmp TAB5 ; =========================================================================== ; INSCHAR - insert one space at the cursor (INST key, insert mode) ; INSBLOCK - open a gap of INSLEN (16-bit) bytes at the cursor by DMOVEing ; CURR..LASTLINE up. If the text would pass TEXEND it silently ; aborts AND drops the caller's return address (back to MAIN). ; =========================================================================== INSCHAR: lda #$01 sta INSLEN lda #$00 sta DEVNO jsr INSBLOCK lda #$20 ldy #$00 sta (CURR),y jmp CHECK INSBLOCK: clc ; LASTLINE + INSLEN (high byte) ... lda LASTLINE adc INSLEN lda LASTLINE+1 adc DEVNO cmp TEXEND+1 ; ... reaches TEXEND? bcc INSB_OK pla ; yes: out of memory - abandon the caller pla jmp INSB_EXIT INSB_OK: clc ; FROM = CURR, DEST = CURR + INSLEN lda CURR sta FROML adc INSLEN sta DESTL lda CURR+1 sta FROMH adc INSLEN+1 sta DESTH sec ; length = LASTLINE - CURR lda LASTLINE sbc FROML sta LLEN lda LASTLINE+1 sbc FROMH sta HLEN jsr DMOVE ; move the text up (high end first) clc ; LASTLINE += INSLEN lda LASTLINE adc INSLEN sta LASTLINE lda LASTLINE+1 adc INSLEN+1 sta LASTLINE+1 INSB_EXIT: rts ; INSTGL - CTRL-I: toggle insert mode (0 <-> $0E; the command line turns ; light blue in insert mode) INSTGL: lda INSMODE eor #$0E sta INSMODE rts ; YORN - print "Are you sure? (Y/N)" and wait for a key. It calls SCNKEY ; itself because this is also used from the NMI handler, when the IRQ that ; normally scans the keyboard may not be running. Returns Z=1 for "Y". YORN: lda #MSG_SURE jsr PRMSG YORN_WAIT: jsr SCNKEY jsr GETIN beq YORN_WAIT cmp #$93 ; ignore CLR (still in the buffer from SHIFT-HOME) beq YORN_WAIT and #$7F ; remove SHIFT bit cmp #$59 ; Z = 1 if "y"/"Y" rts ; CLEARALL - SHIFT-CLR/HOME: "ERASE ALL TEXT" - after Y, wipe the document ; and restart the editor. CLEARALL: lda #$02 sta WINDCOLR jsr TOPCLR lda #MSG_ERASEALL jsr PRMSG jsr YORN beq CLEARALL_YES jmp SYSMSG CLEARALL_YES: ldx #$FA ; reset the stack, erase, re-init, main loop txs jsr ERASE jsr INIT3 jmp MAIN ; PARRIGHT - F5: move to just after the next return mark. PARRIGHT: ldy #$00 PR_SCAN: lda (CURR),y cmp #$1F beq PR_FOUND iny bne PR_SCAN inc CURR+1 lda CURR+1 cmp LASTLINE+1 bcc PR_SCAN beq PR_SCAN jmp ENDTEX PR_FOUND: iny bne PR_SET inc CURR+1 PR_SET: jmp ADDYCURR ; PARLEFT - F6: move to the start of the paragraph (just after the previous ; return mark). If the cursor is already right after one, keep looking ; further back. PARLEFT: lda CURR sta TEX lda CURR+1 sta TEX+1 dec TEX+1 ldy #$FF PL_SCAN: lda (TEX),y cmp #$1F beq PL_FOUND PL_NEXT: dey cpy #$FF bne PL_SCAN dec TEX+1 lda TEX+1 cmp TEXSTART+1 bcs PL_SCAN jmp TOPTEXT PL_FOUND: sec tya adc TEX sta TEX lda #$00 adc TEX+1 sta TEX+1 sec ; did we land where we started? lda TEX sbc CURR sta TEMP lda TEX+1 sbc CURR+1 ora TEMP bne PL_SET sty TEMP ; yes: step back past that return mark and keep looking clc lda TEX sbc TEMP sta TEX lda TEX+1 sbc #$00 sta TEX+1 jmp PL_NEXT PL_SET: lda TEX sta CURR lda TEX+1 sta CURR+1 jmp CHECK ; =========================================================================== ; IRQ_ON - install the split-screen raster interrupt ; Stops the CIA1 timer IRQ and uses a VIC raster interrupt instead, so the ; command line (row 0) can have a different background colour from the ; text area. The keyboard is still scanned once per frame. ; =========================================================================== IRQ_ON: sei lda #$00 ; stop CIA1 timer A (no 60 Hz timer IRQ) sta CIA1_CRA lda #$1B ; screen on, 25 rows, raster bit 8 = 0 sta VIC_CR1 lda # IRQ_HANDLER sta CINV lda #>IRQ_HANDLER sta CINV+1 lda #$01 ; enable raster interrupts ... sta VIC_IMR sta RASTER ; ... first at raster line 1 cli rts ; IRQ_HANDLER - runs twice per frame: ; raster 1 : background = WINDCOLR (colour of the command line, row 0); ; schedule the next IRQ for line 58; exit quickly. ; raster 58 : (the bottom of text row 0) background = SCRCOL for the text ; area; schedule line 1; then run the normal KERNAL IRQ ; (keyboard scan, jiffy clock) - so that still happens once ; per frame, replacing the CIA timer IRQ that was stopped. IRQ_HANDLER: lda #$3A ; line 58 = bottom of the command line ldy WINDCOLR ; assume: colour for the command line cmp RASTER ; which interrupt is this? bne IRQ_SETBG lda #$01 ; line 58: next interrupt at line 1 ... ldy SCRCOL ; ... and switch to the text-area colour IRQ_SETBG: sty BGCOL0 ; set the background colour sta RASTER ; schedule the next interrupt cmp #$01 ; A=1 means this was the line-58 interrupt beq IRQ_FULL lda #$01 ; acknowledge VIC interrupt sta VIC_IRR jmp IRQ_EXIT ; line-1 interrupt: quick exit (pull regs, RTI) IRQ_FULL: lda #$01 ; acknowledge VIC interrupt sta VIC_IRR jmp IRQ_NORMAL ; line-58 interrupt: full KERNAL IRQ (scan keys, clock) ; =========================================================================== ; ERAS - CTRL-E: "Erase (S,W,P): RETURN to exit" ; Repeatedly erases the next Sentence, Word or Paragraph FORWARD from the ; cursor, copying it to the buffer. CTRL-E clears the buffer first; ; SHIFT-CTRL-E keeps it and appends. Any other key exits. ; =========================================================================== ERAS: lda SHFLAG ; SHIFT held? and #$01 bne ERAS_PROMPT ; yes: keep (append to) the buffer jsr KILLBUFF ERAS_PROMPT: jsr TOPCLR lda #MSG_ERASE jsr PRMSG ERAS_LOOP: ldy #$00 ; show the cursor (reverse) while we wait lda (CURR),y eor #$80 sta (CURR),y jsr REFRESH ldy #$00 ; then un-reverse it in memory lda (CURR),y eor #$80 sta (CURR),y lda #$02 sta WINDCOLR jsr GETAKEY ora #$40 ; fold to upper/lower-insensitive: w/s/p cmp #$57 ; W: word bne ERAS_NOTW jsr ERAS_MARK jsr WRIGHT jmp ERAS_DO ERAS_NOTW: cmp #$53 ; S: sentence bne ERAS_NOTS jsr ERAS_MARK jsr SRIGHT jmp ERAS_DO ERAS_NOTS: cmp #$50 ; P: paragraph bne ERAS_EXIT jsr ERAS_MARK jsr PARRIGHT jmp ERAS_DO ERAS_EXIT: jsr CHECK ; other key: exit jmp SYSMSG ERAS_MARK: lda CURR ; ERAS_MARK: remember the start (SAVCURR and DEST) sta DESTL sta SAVCURR lda CURR+1 sta DESTH sta SAVCURR+1 rts ERAS_DO: sec ; ERAS_DO: FROM = new CURR (end); GOBLEN = CURR - SAVCURR lda CURR sta FROML sbc SAVCURR sta GOBLEN lda CURR+1 sta FROMH sbc SAVCURR+1 sta GOBLEN+1 jsr DEL2 ; copy to buffer and close the gap lda SAVCURR ; cursor back to where the erase started sta CURR lda SAVCURR+1 sta CURR+1 jsr REFRESH jmp ERAS_LOOP ; =========================================================================== ; INPUT - read a line from the keyboard on the command line ; Draws its own reverse-space cursor in light green, accepts DEL, ignores ; other control keys, stops at LIMIT (to the end of the 40-column line). ; The text goes to INBUFF (0-terminated), its length to INLEN and A ; (Z=1 if nothing was typed). ; Note: GETAKEY uses GETIN, so if a CHKIN is active this reads from that ; file instead of the keyboard - READERR uses that to read the disk ; error channel. ; =========================================================================== INPUT: lda #$27 ; LIMIT = 39 - cursor column (carry assumed set) sbc PNTR sta LIMIT ldy #$00 INP_LOOP: lda #$99 ; light green, RVS ON, space, CRSR left: draw cursor jsr CHROUT lda #$12 jsr CHROUT lda #$20 jsr CHROUT lda #$9D jsr CHROUT sty INLEN ; save Y (GETIN clobbers it) jsr GETAKEY ldy INLEN sta TEMP lda #$92 ; RVS OFF, space, CRSR left: erase cursor jsr CHROUT lda #$20 jsr CHROUT lda #$9D jsr CHROUT lda #$9B ; back to light grey jsr CHROUT lda TEMP cmp #$0D ; RETURN ends input beq INP_DONE cmp #$14 ; DEL? bne INP_CHAR dey ; back up one character, if there is one bpl INP_BACK iny jmp INP_LOOP INP_BACK: lda #$9D jsr CHROUT jmp INP_LOOP INP_CHAR: lda TEMP and #$7F ; ignore other control characters cmp #$20 bcc INP_LOOP cpy LIMIT ; line full? beq INP_LOOP lda TEMP ; store and echo the character sta INBUFF,y jsr CHROUT lda #$00 ; cancel quote mode and pending inserts so sta QTSW sta INSRT ; further control keys are not printed literally iny jmp INP_LOOP INP_DONE: jsr CHROUT ; print the RETURN lda #$00 sta INBUFF,y ; terminate the string tya ; A = length rts ; =========================================================================== ; SAVE - F8: "Save:" filename, Tape or Disk, then save TEXSTART..LASTLINE ; =========================================================================== SAVE: jsr TOPCLR lda #MSG_SAVE jsr PRMSG jsr TOPEN ; get filename + device; returns to MAIN if cancelled bcs IOERROR lda TEXSTART ; TEX = start address sta TEX lda TEXSTART+1 sta TEX+1 ldx LASTLINE ; X/Y = end address ldy LASTLINE+1 lda #= 8) read and show the drive's error channel. ; NOTE: for tape (IODEV = 1) the code at TAPEERR_HANG loops forever - it ; looks like a bug; the "Tape ERROR" message after it can never be reached. IOERROR: beq STOPPED ; A=0: stopped by the user lda IODEV ; disk? cmp #$08 bcc TAPEERR_HANG jsr READERR ; show the disk drive error message jmp ENDIO TAPEERR_HANG: lda IODEV ; tape: IODEV is 1, so this spins forever (!) cmp #$01 beq TAPEERR_HANG jsr TOPCLR lda #MSG_TAPEERR jsr PRMSG ENDIO: jsr IRQ_ON ; ENDIO: back to the raster IRQ, leave the message showing lda #$01 sta MSGFLG rts STOPPED: jsr TOPCLR lda #MSG_STOPPED jsr PRMSG jmp ENDIO IODEV: .byte $00 ; IODEV: device for load/save/verify (1 = tape, 8 = disk) ; =========================================================================== ; TOPEN - get a filename and "Tape or Disk?", then SETLFS / SETNAM ; If nothing is entered or the answer is neither T nor D, it drops the ; caller's return address and returns to MAIN. For disk, unless the name ; already has a drive prefix ("0:name" / "s0:name"), "0:" is prepended. ; Finishes by restoring the normal IRQ (tape and serial bus timing need ; the CIA timer, and the raster IRQ must not run during I/O). ; =========================================================================== TOPEN: jsr INPUT ; read the filename beq TOPEN_ABORT ; empty: cancel lda #MSG_TAPEDISK jsr PRMSG jsr GETAKEY ldx #$08 ; D -> device 8 cmp #$44 beq TOPEN_DEV ldx #$01 ; T -> device 1 cmp #$54 beq TOPEN_DEV TOPEN_ABORT: jsr SYSMSG ; anything else: restore title and pla ; return to MAIN (skip the caller) pla rts TOPEN_DEV: stx IODEV lda #$01 ; logical file 1, secondary address 0 ldy #$00 jsr SETLFS ldy #$00 cpx #$01 ; tape: use the name as typed beq TOPEN_COPY lda INBUFF,y ; leftover of a test for "@" (save-with-replace); cmp #$40 nop ; the branch has been NOPed out nop lda INBUFF+1,y ; drive prefix already given ("x:" or "xx:")? cmp #$3A beq TOPEN_COPY lda INBUFF+2,y cmp #$3A beq TOPEN_COPY lda #$30 ; no: build "0:" + name sta FILENAME lda #$3A sta FILENAME+1 TOPEN_ADD0: lda INBUFF,y ; copy name after the "0:" sta FILENAME+2,y iny cpy INLEN bcc TOPEN_ADD0 beq TOPEN_ADD0 iny jmp TOPEN_NAME TOPEN_COPY: lda INBUFF,y ; copy the name unchanged sta FILENAME,y iny cpy INLEN bne TOPEN_COPY TOPEN_NAME: sty FNLEN ; FNLEN = length jsr TOPCLR lda #INBUFF jsr PRMSG lda FNLEN ; SETNAM(FNLEN, FILENAME) ldx #FILENAME jsr SETNAM lda #$0D jsr CHROUT jmp IRQ_OFF ; normal IRQ for the I/O to come ; FORMAT - CTRL-pound: "Press format key:" - the next key is stored in the ; text as a REVERSE character, which the print routine treats as a ; formatting command. FORMAT: jsr TOPCLR lda #MSG_FORMAT jsr PRMSG jsr GETAKEY jsr ASTOIN ; to screen code ora #$80 ; set reverse bit = format command pha lda INSMODE beq FORMAT_PUT jsr INSCHAR FORMAT_PUT: jsr SYSMSG pla jmp PUTCHR ; =========================================================================== ; LOAD - F7: "Load:" filename, Tape or Disk ; If the cursor is at the start of the text, the old text is erased and ; the file loaded at TEXSTART. If the cursor is anywhere else, the ; command line turns green and the file is loaded AT THE CURSOR, ; replacing what follows (a merge/append). ; =========================================================================== LOAD: sec ; cursor at TEXSTART? lda CURR sbc TEXSTART sta TEMP lda CURR+1 sbc TEXSTART+1 ora TEMP beq LOAD_ASK lda #$05 ; no: green command line = load at cursor sta WINDCOLR LOAD_ASK: jsr TOPCLR lda #MSG_LOAD jsr PRMSG jsr TOPEN lda WINDCOLR ; normal load (not green)? cmp #$05 beq LOAD_GO jsr ERASE ; yes: erase old text (CURR = TEXSTART) LOAD_GO: lda #$00 ; A=0: LOAD, to address X/Y (secondary address 0) ldx CURR ldy CURR+1 jsr LOAD_K bcc LOAD_OK jmp IOERROR LOAD_OK: stx LASTLINE ; LASTLINE = end address of the loaded data sty LASTLINE+1 NOERRS: jsr CLALL ; NOERRS: close files, "No errors" jsr TOPCLR lda #MSG_NOERR jsr PRMSG jmp ENDIO ; VERIFY - CTRL-V: compare a file with the text in memory. VERIFY: jsr TOPCLR lda #MSG_VERIFY jsr PRMSG jsr TOPEN lda #$01 ; A=1: VERIFY at TEXSTART ldx TEXSTART ldy TEXSTART+1 jsr LOAD_K lda STATUS and #$BF beq NOERRS jsr TOPCLR lda #MSG_VERERR jsr PRMSG jmp ENDIO ; IRQ_OFF - restore the standard KERNAL IRQ (CIA timer), black border and ; background, raster interrupts off. IRQ_OFF: sei lda #$00 sta VIC_IMR sta EXTCOL sta BGCOL0 lda #IRQ_NORMAL sta CINV+1 lda #$01 sta CIA1_CRA cli rts ; =========================================================================== ; CATALOG - CTRL-4: show the disk directory, then "Press RETURN" ; =========================================================================== CATALOG: lda #$93 jsr CHROUT lda #$0D jsr CHROUT jsr IRQ_OFF jsr DIRECTORY lda #$0D jsr CHROUT lda #MSG_RETURN jsr PRMSG CAT_WAIT: jsr GETIN ; wait for RETURN cmp #$0D bne CAT_WAIT jsr IRQ_ON jmp SYSMSG ; DCLOSE - restore channels and close file 1. DCLOSE: jsr CLRCHN lda #$01 jsr CLOSE rts ; DIRECTORY - read "$" from drive 8 as a BASIC program and list it: ; skip the 2-byte load address; for each "line" read the 2-byte link ; (0 = end), the 2-byte block count (printed with LINPRT), then the text ; up to the 0 terminator. Holding SPACE pauses the listing. DIRECTORY: jsr CLALL lda #$01 ldx #$08 ldy #$00 jsr SETLFS lda #$01 ; filename "$" (first byte of the "$No Room" string) ldx #DIRNAME jsr SETNAM jsr OPEN bcs DCLOSE ldx #$01 jsr CHKIN jsr DCHRIN ; skip load address jsr DCHRIN DIR_LINE: jsr DCHRIN ; link address: high byte 0 = end of listing jsr DCHRIN beq DCLOSE jsr CLRCHN jsr GETIN ; SPACE pressed? then wait for another key cmp #$20 bne DIR_BLOCKS jsr GETAKEY DIR_BLOCKS: ldx #$01 jsr CHKIN jsr DCHRIN ; block count low/high pha jsr DCHRIN tay pla tax tya ldy #$37 ; bank BASIC ROM in to call LINPRT sty R6510 jsr LINPRT ldy #$36 sty R6510 lda #$20 jsr CHROUT DIR_NAME: jsr DCHRIN ; print the rest of the line beq DIR_EOL jsr CHROUT jmp DIR_NAME DIR_EOL: lda #$0D jsr CHROUT jmp DIR_LINE ; DCHRIN - CHRIN with error check: on any status (other than EOI) it throws ; away its own return address and its caller's, closes the file and so ; returns from DIRECTORY entirely. DCHRIN: jsr CHRIN pha lda STATUS and #$BF beq DCHRIN_OK pla pla pla jmp DCLOSE DCHRIN_OK: pla rts ; =========================================================================== ; ASCHEX - convert a decimal number in the text at (TEX),Y to binary ; Step 1: pack the digits as BCD into BCD/BCD+1 (shift left 4, OR digit). ; Step 2: in DECIMAL mode count BCD down to zero while counting HEX up in ; binary - a tiny but slow BCD->binary conversion. ; Returns the low byte in A, the full 16-bit result in HEX/HEX+1, and Y ; pointing past the digits. ; =========================================================================== ASCHEX: ldx #$00 stx BCD stx BCD+1 stx HEX stx HEX+1 AH_DIGIT: sec lda (TEX),y ; digit? sbc #$30 bcc AH_TOBIN cmp #$0A bcs AH_TOBIN asl BCD ; BCD <<= 4 rol BCD+1 asl BCD rol BCD+1 asl BCD rol BCD+1 asl BCD rol BCD+1 ora BCD ; add the new digit sta BCD iny bne AH_DIGIT inc TEX+1 jmp AH_DIGIT AH_TOBIN: sed ; decimal mode for the countdown AH_COUNT: lda BCD ora BCD+1 beq AH_DONE sec ; BCD -= 1 (in decimal) lda BCD sbc #$01 sta BCD lda BCD+1 sbc #$00 sta BCD+1 inc HEX ; HEX += 1 (INC is binary even in decimal mode) bne AH_NEXT inc HEX+1 AH_NEXT: jmp AH_COUNT AH_DONE: lda HEX cld rts ; =========================================================================== ; RETRIEVE - CTRL-R: insert the whole cut/paste buffer at the cursor ; =========================================================================== RETRIEVE: sec ; BUFLEN = TPTR - TEXBUF lda TPTR sbc TEXBUF sta BUFLEN lda TPTR+1 sbc TEXBUF+1 sta BUFLEN+1 ora BUFLEN bne RET_MAKEROOM jsr TOPCLR ; empty: "No text in buffer." lda #MSG_NOTEXT jsr PRMSG lda #$01 sta MSGFLG rts RET_MAKEROOM: clc ; FROM = CURR, DEST = CURR + BUFLEN lda CURR sta FROML adc BUFLEN sta DESTL lda CURR+1 sta FROMH adc BUFLEN+1 sta DESTH sec ; length = LASTLINE - CURR lda LASTLINE sbc FROML sta LLEN lda LASTLINE+1 sbc FROMH sta HLEN clc ; would the end pass TEXEND? adc DESTH cmp TEXEND+1 bcc RET_COPY jsr TOPCLR ; yes: "No Room" (skips the "$" of the string) lda #MSG_NOROOM jsr PRMSG lda #$01 sta MSGFLG rts RET_COPY: jsr DMOVE ; open the gap clc ; LASTLINE += BUFLEN; length = BUFLEN lda BUFLEN sta LLEN adc LASTLINE sta LASTLINE lda BUFLEN+1 sta HLEN adc LASTLINE+1 sta LASTLINE+1 lda CURR ; DEST = CURR, FROM = TEXBUF sta DESTL lda CURR+1 sta DESTH lda TEXBUF sta FROML lda TEXBUF+1 sta FROMH lda #$00 ; all-RAM banking and copy the buffer in sta VIC_IMR lda #$34 sta R6510 jsr UMOVE lda #$36 sta R6510 lda #$01 sta VIC_IMR jmp CHECK ; TRANSPOSE - CTRL-X: swap the character under the cursor with the next one. ; CASECHG - CTRL-A: flip upper/lower case of a letter, move right. TRANSPOSE: ldy #$00 lda (CURR),y tax iny lda (CURR),y dey sta (CURR),y iny txa sta (CURR),y rts CASECHG: ldy #$00 lda (CURR),y and #$3F ; letter? (screen codes 1-26 in either case) beq CASE_DONE cmp #$1B bcs CASE_DONE lda (CURR),y eor #$40 ; bit 6 is the case bit in screen codes sta (CURR),y CASE_DONE: jmp RIGHT ; INTOAS - convert a screen code to PETSCII (inverse of ASTOIN). ; bit 6 of the screen code -> bit 7; bit 5 = 0 (letters/@[]) -> set bit 6. ; Result also left in TEMP. INTOAS: sta TEMP and #$3F asl TEMP ; N = old bit 6, V = old bit 5 bit TEMP bpl INTOAS_B5 ; old bit 6 set: shifted/upper-case -> add $80 ora #$80 INTOAS_B5: bvs INTOAS_DONE ; old bit 5 clear: $00-$1F/$40-$5F range -> add $40 ora #$40 INTOAS_DONE: sta TEMP rts ; DEFTAB - default print settings copied to LMARGIN..PAGEWIDTH by PRINIT, ; DEFCODES - default printer key definitions for reverse 1,2,3,4. DEFTAB: .byte $05 ; left margin 5 .byte $4B ; right margin 75 .byte $42 ; page length 66 lines (11 inches at 6 lpi) .byte $05 ; top margin 5 .byte $3A ; bottom margin 58 .byte $01 ; single spacing .byte $01 ; continuous paper (no wait) .byte $01 ; page number 1 (lo) .byte $00 ; (hi) .byte $01 ; start printing at page 1 (lo) .byte $00 ; (hi) DEF_PAGEWIDTH: .byte $50 ; page width 80 DEFCODES: .byte $1B ; key "1" = 27 (ESC) .byte $0E ; key "2" = 14 .byte $0F ; key "3" = 15 .byte $12 ; key "4" = 18 ; =========================================================================== ; PCHAR - send one character to the print device ; Characters are only output once PAGENUM >= STARTNUM (the "?" command). ; Holding SHIFT pauses output (the border shows the shift state). ; Pressing STOP aborts the whole print job. ; =========================================================================== PCHAR: sta PCHR txa pha tya pha sec ; PAGENUM < STARTNUM ? then swallow the character lda PAGENUM sbc STARTNUM lda PAGENUM+1 sbc STARTNUM+1 bcc PCHAR_EXIT lda PCHR jsr CHROUT SHIFTPAUSE: lda SHFLAG ; while SHIFT is held: pause (border turns white) and #$01 sta EXTCOL bne SHIFTPAUSE lda STKEY ; STOP key? cmp #$7F bne PCHAR_EXIT inc EXTCOL ; aborting: change border, end line, finish up jsr CR jmp PREND PCHAR_EXIT: pla tay pla tax lda PCHR rts ; PRINTINGMSG - clear the screen line and show "Printing...". PRINTINGMSG: jsr TOPCLR lda #MSG_PRINTING jmp PRMSG ; PEXIT - abandon printing. PEXIT: jmp PREND ; =========================================================================== ; PRINT - CTRL-P: print the document ; CTRL-P alone prints to device 4, secondary address 7 (lower case on ; Commodore printers). SHIFT-CTRL-P asks "Print to: Screen, Disk, ; Printer?": S = device 3, D = asks for a filename and writes a ",W" ; file on device 8, P = asks for the device number (4-9) and, for a ; printer (4-7), a secondary address. ; =========================================================================== PRINT: lda SCRCOL ; remember the screen colour, go black while printing sta SAVCOL lda #$00 sta WINDCOLR sta EXTCOL sta SCRCOL jsr SETNAM ; no filename (A=0) lda #$04 ; default: device 4 ... sta DEVNO ldy #$07 ; ... secondary address 7 lda SHFLAG ; SHIFT held? and #$01 bne PRMENU jmp PROPEN PRMENU: jsr TOPCLR lda #MSG_PRINTTO jsr PRMSG jsr GETAKEY and #$7F ldx #$03 ; S: screen = device 3 stx DEVNO cmp #$53 beq PRSECADDR ldx #$08 ; D: disk = device 8 stx DEVNO cmp #$44 beq PRDISK cmp #$50 ; P: printer, ask the device number bne PEXIT jsr TOPCLR lda #MSG_DEVNUM jsr PRMSG jsr GETAKEY sec ; digit -> number sbc #$30 cmp #$04 bcc PEXIT cmp #$50 bcs PEXIT sta DEVNO jmp PRSECADDR PRDISK: jsr TOPCLR ; disk: ask for a filename lda #MSG_PRFILE jsr PRMSG jsr INPUT beq PEXIT ldy INLEN ; append ",W" (open a SEQ file for writing) lda #$2C sta INBUFF,y iny lda #$57 sta INBUFF,y iny sty INLEN lda INLEN ldx #INBUFF jsr SETNAM PRSECADDR: lda DEVNO ; Y = device; printers 4-7 also need a sec. address tay cmp #$04 bcc PROPEN cmp #$08 bcs PROPEN jsr TOPCLR lda #MSG_SECADDR jsr PRMSG jsr GETAKEY sec ; digit -> secondary address sbc #$30 tay bpl PROPEN jmp PEXIT PROPEN: lda #$01 ; SETLFS(1, device, secondary address) ldx DEVNO jsr SETLFS jsr PRINTINGMSG lda #$01 ; close file 1 in case it was left open jsr CLOSE jsr OPEN ldx #$01 ; all output to file 1 jsr CHKOUT bcc PRINIT jmp PREND ; PRINIT - reset all print settings to DEFTAB/DEFCODES, no header/footer, ; LINE = $FF (= "top of the first page not printed yet"). PRINIT: ldx #$00 stx FTLEN stx HDLEN stx NEEDASC stx UNDERLINE stx LINEFEED PRINIT_DEF: lda DEFTAB,x ; copy the 12 defaults sta LMARGIN,x inx cpx #$0C bne PRINIT_DEF lda #$FF ; LINE = $FF: need a top-of-page sta LINE sta NOMARG ldx #$04 ; copy printer codes for keys 1-4 PRINIT_CODES: lda DEFCODES-1,x sta CODEBUFFER+$30,x dex bne PRINIT_CODES PRSTART: lda TEXSTART ; PRSTART: begin at the top of the text sta TEX lda TEXSTART+1 sta TEX+1 ; =========================================================================== ; PLOOP - format and print one line ; Copies characters from (TEX) into PRBUFF until the right margin (or a ; return mark). Reverse characters are format commands (FMTCMD). If the ; margin is reached, backs up to the last space (word wrap). Then: ; top-of-page if needed, left margin, the line itself (OUTLINE), line ; spacing, end-of-page check, end-of-text check. ; =========================================================================== PLOOP: ldy #$00 ; POS = 0 ... sty POS cpy NOMARG ; ... or the left margin unless suppressed beq PL_GETCH lda LMARGIN sta POS PL_GETCH: lda (TEX),y ; reverse character = format command bpl PL_NOTCMD jmp FMTCMD PL_NOTCMD: cmp #$1F ; return mark = end of line beq PL_ENDLINE PL_STORE: sta PRBUFF,y ; store into the print line iny inc POS lda POS ; reached the right margin? cmp RMARGIN bcc PL_GETCH sty FINPOS ; line full: back up to a space PL_BACKUP: lda (TEX),y cmp #$20 beq PL_ENDLINE dec POS dey bne PL_BACKUP ldy FINPOS ; no space at all: break at the margin jmp PL_ADVANCE DEADCODE: iny ; unreachable code (left over from an earlier version) lda (TEX),y cmp #$20 beq PL_ENDLINE dey PL_ENDLINE: sty FINPOS ; FINPOS = end of line PL_ADVANCE: tya ; TEX += Y+1 (skip the space / return mark too) sec adc TEX sta TEX lda TEX+1 adc #$00 sta TEX+1 ldy #$00 lda LINE ; first line of a page? cmp #$FF bne PL_LMARG jsr TOP ; yes: header and top margin PL_LMARG: lda NOMARG ; left margin, unless the "m" command suppressed it beq PL_OUTPUT jsr LMARG PL_OUTPUT: sec ; NOMARG back to "margin on" rol NOMARG lda FINPOS ; line length for OUTLINE sta ENDPOS lda #PRBUFF sta FPTR+1 jsr OUTLINE jsr CRSPACING ; CR(s) for line spacing lda LINE ; reached the bottom margin? cmp BOTMARGIN bcc PL_CHKEND jsr PAGE ; yes: finish the page PL_CHKEND: sec ; past the end of the text? lda TEX sbc LASTLINE sta TEMP lda TEX+1 sbc LASTLINE+1 ora TEMP beq PL_AGAIN bcc PL_AGAIN lda FTLEN ; if there is a footer, run to the bottom of the page beq PL_FINISHED lda #$00 ; (no header / top margin for a new page) sta HDLEN sta TOPMARGIN jsr PAGE PL_FINISHED: lda DEVNO ; printing to screen: wait for a key to see it cmp #$03 bne PREND jsr GETAKEY PREND: jsr STOP ; PREND: wait until STOP is released beq PREND lda #$01 ; close the print file jsr CLOSE jsr CLALL lda SAVCOL ; restore the screen colour sta SCRCOL ldx #$FA ; reset the stack and go back to editing txs jsr SYSMSG jmp MAIN PL_AGAIN: jmp PLOOP ; =========================================================================== ; PAGE - finish the current page and start a new one ; Line-feeds down to the footer position, prints the footer (with left ; margin), ejects with three CRs, bumps PAGENUM, and - unless printing ; continuously, to screen or to disk - asks for the next sheet. Then ; falls into TOP. ; =========================================================================== PAGE: sec ; lines left to the footer = PAGELENGTH - LINE - 2 lda PAGELENGTH sbc LINE tay dey dey beq PAGE_FOOTER bmi PAGE_FOOTER PAGE_FILL: jsr CR dey bne PAGE_FILL PAGE_FOOTER: lda FTLEN ; footer? beq PAGE_EJECT sta ENDPOS lda #FTBUFF sta FPTR+1 jsr LMARG jsr OUTLINE PAGE_EJECT: jsr CR ; end of the page jsr CR jsr CR inc PAGENUM ; PAGENUM += 1 bne PAGE_WAIT inc PAGENUM+1 PAGE_WAIT: lda CONTINUOUS ; continuous paper? bne TOP lda DEVNO ; screen? cmp #$03 beq TOP cmp #$08 ; disk file? beq TOP sec ; still skipping pages before STARTNUM? lda PAGENUM sbc STARTNUM lda PAGENUM+1 sbc STARTNUM+1 bcc TOP jsr CLRCHN ; ask for a new sheet jsr TOPCLR lda #MSG_NEXTSHEET jsr PRMSG jsr GETAKEY jsr PRINTINGMSG ldx #$01 ; output back to the printer jsr CHKOUT ; TOP - start of page: print the header (with left margin) if there is ; one, then the top margin as blank lines; LINE = TOPMARGIN. TOP: lda HDLEN beq TOP_MARGIN sta ENDPOS lda #HDBUFF sta FPTR+1 jsr LMARG jsr OUTLINE TOP_MARGIN: ldy TOPMARGIN sty LINE dey beq TOP_DONE bmi TOP_DONE TOP_CRS: jsr CR dey bne TOP_CRS TOP_DONE: rts ; LMARG - print LMARGIN spaces; POS = LMARGIN. LMARG: lda #$20 ldy LMARGIN sty POS beq LMARG_DONE LMARG_LOOP: jsr PCHAR dey bne LMARG_LOOP LMARG_DONE: rts ; CRSPACING - print SPACING carriage returns; LINE += SPACING. CRSPACING: ldy SPACING clc tya adc LINE sta LINE CRSP_LOOP: jsr CR dey bne CRSP_LOOP rts ; CR - print one carriage return, plus a linefeed if the "j" command was used. CR: lda #$0D jsr PCHAR lda LINEFEED beq CR_DONE jsr PCHAR CR_DONE: rts ; =========================================================================== ; FMTCMD - a reverse character was met while formatting a line ; Convert it to PETSCII and look it up in FMTTAB. Found: call the handler ; with FMT_RESUME-1 pushed as the return address. Handlers parse their ; argument, leave YSAVE pointing at what follows, and FMT_RESUME skips ; TEX past the command and restarts the line. ; Not in the table: if followed by "=" it defines a printer key code ; (e.g. reverse 1 =27); otherwise it is a printer key / in-line command ; (c, e, u, # ...) and is copied into the print line for OUTLINE. ; =========================================================================== FMTCMD: sta SAVCHAR ; keep the original (reverse) character and #$7F jsr INTOAS ; to PETSCII ldx FMTTAB FMT_SEARCH: cmp FMTTAB,x beq FMT_DISPATCH dex bne FMT_SEARCH dec POS ; not a format command: takes no column jmp PRKEY FMT_DISPATCH: dex txa asl a tax sty YSAVE ; YSAVE = position of the command lda #>(FMT_RESUME-1) ; push FMT_RESUME-1 pha lda #<(FMT_RESUME-1) pha lda FMTVEC_HI,x ; push handler address-1 and RTS to it pha lda FMTVEC,x pha rts FMT_RESUME: sec ; FMT_RESUME: TEX += YSAVE+1 lda YSAVE adc TEX sta TEX lda TEX+1 adc #$00 sta TEX+1 jmp PLOOP ; restart the line SKIPPARM: lda (TEX),y ; SKIPPARM: if we stopped on a return mark, skip it cmp #$1F beq SKIPPARM2 dey ; otherwise do not skip the stopping character SKIPPARM2: sty YSAVE rts ; FMTTAB - first byte = count (18), then the command letters (PETSCII). ; FMTVEC - handler addresses minus 1. FMTTAB: .byte $12 ; count = 18 .byte $57 ; "w" .byte $41 ; "a" .byte $4C ; "l" .byte $52 ; "r" .byte $54 ; "t" .byte $42 ; "b" .byte $53 ; "s" .byte $4E ; "n" .byte $48 ; "h" .byte $46 ; "f" .byte $40 ; "@" .byte $50 ; "p" .byte $3F ; "?" .byte $58 ; "x" .byte $4D ; "m" .byte $49 ; "i" .byte $47 ; "g" .byte $4A ; "j" FMTVEC: .word F_WAIT-1 ; w: wait between pages .word F_ASCII-1 ; a: true ASCII .word F_LMARGIN-1 ; l: left margin .word F_RMARGIN-1 ; r: right margin .word F_TOPMARGIN-1 ; t: top margin .word F_BOTMARGIN-1 ; b: bottom margin .word F_SPACING-1 ; s: line spacing .word F_NEXTPAGE-1 ; n: next page .word F_HEADER-1 ; h: header .word F_FOOTER-1 ; f: footer .word F_PAGENUM-1 ; @: page number .word F_PAGELEN-1 ; p: page length .word F_STARTPAGE-1 ; ?: start printing at page .word F_PAGEWIDTH-1 ; x: page width .word F_NOMARGIN-1 ; m: no left margin .word F_IGNORE-1 ; i: ignore line .word F_LINK-1 ; g: link to next file .word F_LINEFEED-1 ; j: linefeeds ; The format-command handlers. On entry Y indexes the command character. ; Numeric arguments are read with ASCHEX. F_NOMARGIN: iny ; m: next line printed without the left margin lda #$00 sta NOMARG jmp SKIPPARM F_PAGEWIDTH: iny ; x n: page width jsr ASCHEX sta PAGEWIDTH jmp SKIPPARM F_STARTPAGE: iny ; ? n: first page to print (16-bit) jsr ASCHEX sta STARTNUM lda HEX+1 sta STARTNUM+1 jmp SKIPPARM F_PAGENUM: iny ; @ n: page number (16-bit) jsr ASCHEX sta PAGENUM lda HEX+1 sta PAGENUM+1 jmp SKIPPARM F_PAGELEN: iny ; p n: page length jsr ASCHEX sta PAGELENGTH jmp SKIPPARM F_WAIT: lda #$00 ; w: wait for a new sheet after each page sta CONTINUOUS iny jmp SKIPPARM F_LINEFEED: lda #$0A ; j: send LF ($0A) after every CR sta LINEFEED iny jmp SKIPPARM F_ASCII: iny ; a: translate to true ASCII lda #$01 sta NEEDASC jmp SKIPPARM F_LMARGIN: iny ; l n: left margin jsr ASCHEX sta LMARGIN jmp SKIPPARM F_RMARGIN: iny ; r n: right margin jsr ASCHEX sta RMARGIN jmp SKIPPARM F_TOPMARGIN: iny ; t n: top margin jsr ASCHEX sta TOPMARGIN jmp SKIPPARM F_BOTMARGIN: iny ; b n: bottom margin jsr ASCHEX sta BOTMARGIN jmp SKIPPARM F_SPACING: iny ; s n: line spacing jsr ASCHEX sta SPACING jmp SKIPPARM F_NEXTPAGE: ldy YSAVE ; n: force a new page now iny tya pha jsr PAGE pla tay sty YSAVE rts F_HEADER: jsr FINDRET ; h text: header = rest of the line dey sty HDLEN ldy #$01 HDR_COPY: lda (TEX),y ; copy it into HDBUFF sta HDBUFF-1,y iny cpy HDLEN bcc HDR_COPY beq HDR_COPY iny jmp SKIPPARM FINDRET: iny ; FINDRET: advance Y to the next return mark lda (TEX),y cmp #$1F bne FINDRET rts F_FOOTER: jsr FINDRET ; f text: footer = rest of the line dey sty FTLEN ldy #$01 FTR_COPY: lda (TEX),y sta FIRSTRUN,y iny cpy FTLEN bcc FTR_COPY beq FTR_COPY jmp SKIPPARM F_IGNORE: jsr FINDRET ; i: ignore everything to the end of the line jmp SKIPPARM PRKEY: iny ; PRKEY: is the next character "=" ? lda (TEX),y cmp #$3D beq PRKEY_DEFINE dey ; no: copy the reverse char into the print line lda SAVCHAR jmp PL_STORE PRKEY_DEFINE: iny ; yes: CODEBUFFER[key] = number jsr ASCHEX pha lda SAVCHAR and #$7F tax pla sta CODEBUFFER,x jsr SKIPPARM jmp FMT_RESUME F_LINK: iny ; g d:name / g t:name - chain to the next file ldx #$08 lda (TEX),y and #$3F ; "d" (screen code 4) -> disk, device 8 cmp #$04 beq LINK_COLON ldx #$01 ; "t" (screen code 20) -> tape, device 1 cmp #$14 beq LINK_COLON jmp PEXIT LINK_COLON: stx IODEV iny lda (TEX),y ; must be followed by ":" cmp #$3A beq LINK_NAME jmp PEXIT LINK_NAME: iny ; copy the filename (assumes the command starts the line) lda (TEX),y cmp #$1F beq LINK_LOAD jsr INTOAS sta FILENAME-3,y jmp LINK_NAME LINK_LOAD: tya ; length = Y - 3 sec sbc #$03 ldx #FILENAME jsr SETNAM jsr CLRCHN ; output back to the screen, close file 2 lda #$02 jsr CLOSE lda #$02 ; SETLFS(2, device, 0) ldx IODEV ldy #$00 jsr SETLFS jsr ERASE ; erase the current text and load the next file lda #$00 ldx CURR ldy CURR+1 jsr LOAD_K bcc LINK_OK jmp PEXIT LINK_OK: stx LASTLINE ; new end of text sty LASTLINE+1 pla ; drop the return to FMT_RESUME pla ldx #$01 ; output back to the printer jsr CHKOUT jmp PRSTART ; and print the new text from the top ; =========================================================================== ; DISKCMD - CTRL-up arrow: "Disk command:" - send the typed text to the ; drive's command channel (15). Just RETURN reads and shows the drive ; status instead. READERR is also used after a failed disk load/save. ; =========================================================================== DISKCMD: jsr CLALL lda #$00 jsr SETNAM lda #$0F ; open 15,8,15 ldx #$08 ldy #$0F jsr SETLFS jsr OPEN bcc DCMD_ASK DCMD_CLOSE: lda #$0F ; DCMD_CLOSE: close 15 and give up jsr CLOSE jsr CLALL jmp SYSMSG DCMD_ASK: jsr TOPCLR lda #MSG_DISKCMD jsr PRMSG jsr INPUT beq READERR ; nothing typed: just read the error channel ldx #$0F jsr CHKOUT bcs DCMD_CLOSE lda #INBUFF jsr PRMSG lda #$0D jsr CHROUT jsr CLRCHN READERR: jsr CLALL ; READERR: reopen 15,8,15 lda #$00 jsr SETNAM lda #$0F ldx #$08 ldy #$0F jsr SETLFS jsr OPEN bcs DCMD_CLOSE jsr TOPCLR ldx #$0F ; input from the error channel ... jsr CHKIN jsr INPUT ; ... INPUT now reads (and echoes) the status line jsr CLRCHN lda #$0F jsr CLOSE jsr CLALL lda #$01 sta MSGFLG rts ; GLOBAL - CTRL-G: ask hunt and replace strings, then replace every ; occurrence from the cursor to the end of the text. GLOBAL: jsr ASKHUNT lda HUNTLEN beq GLOBAL_DONE jsr ASKREP GLOBAL_LOOP: jsr CONTSRCH lda FPOS+1 cmp #$FF beq GLOBAL_DONE jsr REPLACE jsr REFRESH jmp GLOBAL_LOOP GLOBAL_DONE: jmp SYSMSG ; HUNT - CTRL-H: find the next occurrence of the hunt string. ; SHIFT-CTRL-H: ask for a new hunt string ("Hunt for:"). HUNT: lda SHFLAG cmp #$05 bne CONTSRCH ASKHUNT: jsr TOPCLR lda #MSG_HUNT jsr PRMSG jsr INPUT sta HUNTLEN ; HUNTLEN = length bne ASKHUNT_COPY jmp SYSMSG ASKHUNT_COPY: ldy #$00 AH_COPYLOOP: lda INBUFF,y ; copy INBUFF -> HUNTBUFF sta HUNTBUFF,y iny cpy INLEN bne AH_COPYLOOP jmp SYSMSG ; =========================================================================== ; CONTSRCH - search forward from CURR+1 for HUNTBUFF (case-sensitive, in ; screen codes). Found: cursor and FPOS = start of the match. Else "Not ; Found" and FPOS high = $FF. ; The matcher is simple: on a mismatch it restarts comparing at the next ; text position without backing up, so some overlapping matches can be ; missed (e.g. "aab" inside "aaab"). ; =========================================================================== CONTSRCH: lda CURR sta TEX lda CURR+1 sta TEX+1 lda #$FF ; FPOS = "not found" sta FPOS+1 ldy #$01 ; start one past the cursor ldx #$00 lda HUNTLEN beq NOTFOUND SRCH_CMP: lda HUNTBUFF,x ; compare hunt character (as screen code) jsr ASTOIN cmp (TEX),y beq SRCH_NEXT ldx #$FF ; mismatch: X=$FF so INX restarts the match SRCH_NEXT: iny bne SRCH_COUNT inc TEX+1 lda TEX+1 ; past the end of the text? cmp LASTLINE+1 beq SRCH_COUNT bcs NOTFOUND SRCH_COUNT: inx cpx HUNTLEN ; matched all HUNTLEN characters? bne SRCH_CMP clc ; TEMP = address just after the match tya adc TEX sta TEMP lda TEX+1 adc #$00 sta TEMP+1 lda LASTLINE ; beyond LASTLINE? then not found cmp TEMP lda LASTLINE+1 sbc TEMP+1 bcc NOTFOUND sec ; CURR = FPOS = TEMP - HUNTLEN lda TEMP sbc HUNTLEN sta CURR sta FPOS lda TEMP+1 sbc #$00 sta CURR+1 sta FPOS+1 jsr CHECK rts NOTFOUND: jsr TOPCLR lda #MSG_NOTFOUND jsr PRMSG lda #$01 sta MSGFLG rts ; REPSTART - CTRL-J: replace the text just found. ; SHIFT-CTRL-J: ask for the replacement ("Replace with:"). REPSTART: lda SHFLAG cmp #$05 bne REPLACE ASKREP: jsr TOPCLR lda #MSG_REPLACE jsr PRMSG jsr INPUT sta REPLEN beq ASKREP_DONE ldy #$00 ASKREP_COPY: lda INBUFF,y sta REPBUFF,y iny cpy INLEN bne ASKREP_COPY ASKREP_DONE: jmp SYSMSG ; REPLACE - only if the cursor is still at FPOS (where the hunt string was ; found): delete HUNTLEN characters, insert REPBUFF, and move the cursor ; past the replacement. REPLACE: sec lda CURR sta DESTL sbc FPOS sta TEMP lda CURR+1 sta DESTH sbc FPOS+1 ora TEMP bne REP_DONE ; cursor moved since the hunt: do nothing lda #$FF ; can only replace once per find sta FPOS+1 clc ; FROM = CURR + HUNTLEN lda HUNTLEN adc CURR sta FROML lda #$00 adc CURR+1 sta FROMH sec ; length = LASTLINE - CURR lda LASTLINE sbc DESTL sta LLEN lda LASTLINE+1 sbc DESTH sta HLEN jsr UMOVE ; remove the found text sec ; LASTLINE -= HUNTLEN lda LASTLINE sbc HUNTLEN sta LASTLINE lda LASTLINE+1 sbc #$00 sta LASTLINE+1 lda REPLEN ; replacement string empty? done beq REP_DONE sta INSLEN ; make room for REPLEN characters lda #$00 sta INSLEN+1 jsr INSBLOCK ldy #$00 REP_COPY: lda REPBUFF,y ; copy the replacement (as screen codes) jsr ASTOIN sta (CURR),y iny cpy REPLEN bne REP_COPY clc ; cursor after the replacement lda CURR adc REPLEN sta CURR lda CURR+1 adc #$00 sta CURR+1 REP_DONE: jmp CHECK ; =========================================================================== ; OUTLINE - send ENDPOS characters from (FPTR) to the printer ; Normal characters: screen code -> PETSCII (-> true ASCII if "a") and out; ; with underline on, each is followed by BACKSPACE + "_". ; Reverse characters are in-line commands: ; c centre the line (pad (PAGEWIDTH-length)/2 - LMARGIN spaces) ; e flush right (pad RMARGIN - length - LMARGIN spaces) ; u toggle underline (then also sends its CODEBUFFER entry) ; # print the page number ; anything else: send the byte defined for it with "key=n" ; =========================================================================== OUTLINE: ldy #$00 OUT_LOOP: cpy ENDPOS beq OUT_DONE lda (FPTR),y ; reverse: in-line command bmi SPECIAL jsr INTOAS ; screen code -> PETSCII jsr CONVASC ; optional ASCII translation jsr PCHAR lda UNDERLINE ; underlining? beq OUT_NEXT lda #$08 ; backspace and underscore jsr PCHAR lda #$5F jsr PCHAR OUT_NEXT: iny jmp OUT_LOOP OUT_DONE: rts SPECIAL: sty YSAVE and #$7F sta SAVCHAR jsr INTOAS cmp #$43 ; "c": centre bne SPC_NOTC sec lda PAGEWIDTH sbc ENDPOS lsr a sec sbc LMARGIN tay lda #$20 SPC_SPACES: jsr PCHAR dey bne SPC_SPACES ldy YSAVE jmp OUT_NEXT SPC_NOTC: cmp #$45 ; "e": flush right (edge) bne SPC_NOTE sec lda RMARGIN sbc ENDPOS sec sbc LMARGIN tay lda #$20 jmp SPC_SPACES SPC_NOTE: cmp #$55 ; "u": underline toggle bne SPC_NOTU lda UNDERLINE eor #$01 sta UNDERLINE SPC_NOTU: cmp #$23 ; "#": page number bne SENDCODE sty YSAVE ldx PAGENUM ; A/X = page number; print with BASIC's LINPRT lda PAGENUM+1 ldy #$37 sty R6510 jsr LINPRT ldy #$36 sty R6510 ldy YSAVE jmp OUT_NEXT SENDCODE: ldx SAVCHAR ; SENDCODE: send the user-defined printer code lda CODEBUFFER,x jsr PCHAR jmp OUT_NEXT ; CONVASC - if "a" (NEEDASC) is on, swap the case of letters so a true ; ASCII printer gets lower case $61-$7A and upper case $41-$5A. CONVASC: ldx NEEDASC beq CONVASC_DONE sta TEMP and #$7F cmp #$41 bcc CONVASC_DONE cmp #$5B bcs CONVASC_DONE tax lda TEMP ; $20 if it was lower case, 0 if upper and #$80 eor #$80 lsr a lsr a sta TEMP txa ora TEMP CONVASC_DONE: rts ; =========================================================================== ; FREEMEM - CTRL-=: show TEXEND - LASTLINE (free bytes) on the command line ; =========================================================================== FREEMEM: jsr TOPCLR sec lda TEXEND sbc LASTLINE tax lda TEXEND+1 sbc LASTLINE+1 ldy #$37 sty R6510 jsr LINPRT ldy #$36 sty R6510 lda #$01 sta MSGFLG rts ; =========================================================================== ; MESSAGES (PETSCII, 0-terminated). The editor runs in the lower/upper ; case character set, so $41-$5A print as lower case and $C1-$DA as ; capitals. {xx} = control code: $08 disable C=/SHIFT case switch, ; $0E lower case set, $9B light grey, $92 RVS off, $12 RVS on, $93 CLR. ; =========================================================================== MSG_TITLE: .byte $08,$0E,$9B,$92,$D3,$50,$45,$45,$44,$D3,$43,$52,$49,$50,$54,$20,$33,$2E,$31,$00 ; "{08}{0E}{9B}{92}SpeedScript 3.1" + 0 MSG_BYLINE: .byte $20,$42,$59,$20,$C3,$48,$41,$52,$4C,$45,$53,$20,$C2,$52,$41,$4E,$4E,$4F,$4E,$00 ; " by Charles Brannon" + 0 MSG_BUFCLR: .byte $C2,$55,$46,$46,$45,$52,$20,$C3,$4C,$45,$41,$52,$45,$44,$00 ; "Buffer Cleared" + 0 MSG_BUFFULL: .byte $C2,$55,$46,$46,$45,$52,$20,$C6,$55,$4C,$4C,$00 ; "Buffer Full" + 0 MSG_DELETE: .byte $C4,$45,$4C,$45,$54,$45,$20,$28,$D3,$2C,$D7,$2C,$D0,$29,$00 ; "Delete (S,W,P)" + 0 MSG_SURE: .byte $3A,$20,$C1,$52,$45,$20,$59,$4F,$55,$20,$53,$55,$52,$45,$3F,$20,$28,$D9,$2F,$CE,$29,$3A,$00 ; ": Are you sure? (Y/N):" + 0 MSG_ERASEALL: .byte $C5,$D2,$C1,$D3,$C5,$20,$C1,$CC,$CC,$20,$D4,$C5,$D8,$D4,$00 ; "ERASE ALL TEXT" + 0 MSG_ERASE: .byte $C5,$52,$41,$53,$45,$20,$28,$D3,$2C,$D7,$2C,$D0,$29,$3A,$20,$12,$D2,$C5,$D4,$D5,$D2,$CE,$92,$20,$54,$4F,$20,$45,$58,$49,$54,$00 ; "Erase (S,W,P): {12}RETURN{92} to exit" + 0 MSG_FORMAT: .byte $D0,$52,$45,$53,$53,$20,$46,$4F,$52,$4D,$41,$54,$20,$4B,$45,$59,$3A,$00 ; "Press format key:" + 0 MSG_SAVE: .byte $D3,$41,$56,$45,$3A,$00 ; "Save:" + 0 MSG_TAPEERR: .byte $D4,$41,$50,$45,$20,$C5,$D2,$D2,$CF,$D2,$00 ; "Tape ERROR" + 0 MSG_STOPPED: .byte $D3,$54,$4F,$50,$50,$45,$44,$00 ; "Stopped" + 0 MSG_VERERR: .byte $D6,$45,$52,$49,$46,$59,$20,$C5,$52,$52,$4F,$52,$00 ; "Verify Error" + 0 MSG_NOERR: .byte $CE,$4F,$20,$45,$52,$52,$4F,$52,$53,$00 ; "No errors" + 0 MSG_TAPEDISK: .byte $93,$20,$12,$D4,$92,$41,$50,$45,$20,$4F,$52,$20,$12,$C4,$92,$49,$53,$4B,$3F,$00 ; "{93} {12}T{92}ape or {12}D{92}isk?" + 0 MSG_LOAD: .byte $CC,$4F,$41,$44,$3A,$00 ; "Load:" + 0 MSG_VERIFY: .byte $D6,$45,$52,$49,$46,$59,$3A,$00 ; "Verify:" + 0 MSG_RETURN: .byte $D0,$52,$45,$53,$53,$20,$12,$D2,$C5,$D4,$D5,$D2,$CE,$92,$00 ; "Press {12}RETURN{92}" + 0 MSG_DISKCMD: .byte $C4,$49,$53,$4B,$20,$43,$4F,$4D,$4D,$41,$4E,$44,$3A,$00 ; "Disk command:" + 0 DIRNAME: .byte $24 ; "$" MSG_NOROOM: .byte $CE,$4F,$20,$D2,$4F,$4F,$4D,$00 ; "No Room" + 0 MSG_NOTEXT: .byte $CE,$4F,$20,$54,$45,$58,$54,$20,$49,$4E,$20,$42,$55,$46,$46,$45,$52,$2E,$00 ; "No text in buffer." + 0 MSG_PRINTTO: .byte $93,$D0,$52,$49,$4E,$54,$20,$54,$4F,$3A,$20,$12,$D3,$92,$43,$52,$45,$45,$4E,$2C,$12,$C4,$92,$49,$53,$4B,$2C,$12,$D0,$92,$52,$49,$4E,$54,$45,$52,$3F,$00 ; "{93}Print to: {12}S{92}creen,{12}D{92}isk,{12}P{92}rinter?" + 0 MSG_DEVNUM: .byte $C4,$45,$56,$49,$43,$45,$20,$4E,$55,$4D,$42,$45,$52,$3F,$00 ; "Device number?" + 0 MSG_SECADDR: .byte $D3,$45,$43,$4F,$4E,$44,$41,$52,$59,$20,$C1,$44,$44,$52,$45,$53,$53,$20,$23,$3F,$00 ; "Secondary Address #?" + 0 MSG_PRFILE: .byte $D0,$52,$49,$4E,$54,$20,$54,$4F,$20,$46,$49,$4C,$45,$4E,$41,$4D,$45,$3A,$00 ; "Print to filename:" + 0 MSG_PRINTING: .byte $93,$D0,$52,$49,$4E,$54,$49,$4E,$47,$2E,$2E,$2E,$0D,$0D,$00 ; "{93}Printing..." + 0 MSG_NEXTSHEET: .byte $C9,$4E,$53,$45,$52,$54,$20,$4E,$45,$58,$54,$20,$53,$48,$45,$45,$54,$2C,$20,$50,$52,$45,$53,$53,$20,$12,$D2,$C5,$D4,$D5,$D2,$CE,$92,$00 ; "Insert next sheet, press {12}RETURN{92}" + 0 MSG_HUNT: .byte $C8,$55,$4E,$54,$20,$46,$4F,$52,$3A,$00 ; "Hunt for:" + 0 MSG_NOTFOUND: .byte $CE,$4F,$54,$20,$C6,$4F,$55,$4E,$44,$00 ; "Not Found" + 0 MSG_REPLACE: .byte $D2,$45,$50,$4C,$41,$43,$45,$20,$57,$49,$54,$48,$3A,$00 ; "Replace with:" + 0 MSG_EXIT: .byte $C5,$D8,$C9,$D4,$20,$D3,$50,$45,$45,$44,$D3,$43,$52,$49,$50,$54,$00 ; "EXIT SpeedScript" + 0 ; Pointer low bytes that happen to sit at the end of the file (set by INIT). ; TEXSTART ($2008/$2009) start of text, TEXEND ($200A/$200B) end of the ; text area, TEXBUF ($200C/$200D) start of the cut/paste buffer. TEXSTART: .byte $0D .byte $0D TEXEND: .byte $0D .byte $0D TEXBUF: .byte $0D