;---------------------------------------
;   RATSWORD V4.2.10 Text editor "rat"

;    (C)1988-91 Baird Productions
;      Last ammendment 22/2/91

;---------------------------------------
         *= $6000

 ;Intend flaging a varible width
 ;But would need to calculate maxlines,
 ;wid-40,wid-1,buffer+wid-1. Table?

screen   = 1024
textbase = $e000
wid      = 80      ;Watch out if wid>127
maxlines = 100
input    = screen+975
devdir   = 6
totdevs  = 6

tabkey   = 133  ;Command Keys
togkey   = 137
trmkey   = 144
lodkey   = 136
imodekey = 144
capskey  = 5
typekey  = 28
savkey   = 140
endkey   = 3
inskey   = 135
remkey   = 139
pupkey   = 138
pdownkey = 134

indr1    = $02
indr2    = $04
flength  = $0c
fname    = $0e

stdost   = $60
stdonum  = $61
stdomode = $62
stdinum  = $63
stdist   = $64
stdolen  = $6a
stdilen  = $6b

tty      = $0830
con      = $0831
kbd      = $0832

convmode = $40
acl      = $d9  ;OK to use, since not
ach      = $da  ;using $ffd2
xtndl    = $db
xtndh    = $dc
auxl     = $dd
auxh     = $de

indr     = $5e
spot     = $65
xpos     = $67
ypos     = $68
max      = $fb
xreal    = 828
yreal    = 829
store    = 830

open     = $0903
openfnum = $0906
close    = $0909
chrout   = $090c
chrin    = $090f
chkexist = $0924
pageon   = $092a
pageoff  = $092d
convert  = $0930
flagchk  = $101e

getin    = $ffe4
ystore   = 782

cbmcbm   = 0
cbmpet   = 1
cbmasc   = 2
petcbm   = 3
asccbm   = 4
petasc   = 5
ascpet   = 6
ascdump  = 7

;---------------------------------------

         ldx #0
         stx xpos
         stx ypos
         stx modifed
         ldy #maxlines      ;Find topmem
         jsr calcwid
         lda acl
         sta max
         lda ach
         sta max+1

         ldx #asccbm  ;Get input filetyp
         lda #"p"
         jsr flagchk
         bcs *+4
         ldx #petcbm
         lda #"c"
         jsr flagchk
         bcs *+4
         ldx #cbmcbm
         stx intype
         lda inout,x
         sta outtype

         ldy #0          ;Initalise tabs
loop2    ldx #3
         lda #45
loop1    sta tabbar,y
         iny
         dex
         bne loop1
         lda #42
         sta tabbar,y
         iny
         cpy #wid
         bcc loop2
         sta tabbar+38

         lda stdinum  ;If declared input
         cmp #totdevs ;then load in file
         bcc *+5
         jmp preload

;---------------------------------------

refresh  jsr pageon
         jsr $e544
         lda #22
         sta 53272

         ldy #39
         lda #61
loop4    sta screen+920,y
         dey
         bpl loop4

         ldy #16
loop3    lda title,y
         sta screen,y
         dey
         bpl loop3

         ldx #"C"
         lda caps
         bne *+4
         ldx #"c"+64
         stx screen+31

         ldx #"I"
         lda imode
         beq *+4
         ldx #"D"
         stx screen+33

         ldx #"a"
         lda intype
         cmp #petcbm
         bne *+4
         ldx #"p"
         cmp #cbmcbm
         bne *+4
         ldx #"c"
         stx screen+28

         ldx #"a"
         lda outtype
         cmp #cbmpet
         bne *+4
         ldx #"p"
         cmp #cbmcbm
         bne *+4
         ldx #"c"
         stx screen+29


draw     ldy #0
         lda ypos
         cmp #10
         bcc bran2
         sbc #10
         tay
bran1    cmp #maxlines-20
         bcc bran2
         ldy #maxlines-20
bran2    sty yreal

         lda xpos
         ldx #0
         cmp #19
         bcc bran4
         sbc #19
         tax
bran3    cmp #wid-40
         bcc bran4
         ldx #wid-40
bran4    stx xreal

         ldx xreal            ;Draw tabs
         ldy #0
loop5    lda tabbar,x
         sta screen+40,y
         inx
         iny
         cpy #40
         bne loop5

         jsr pageoff
         ldx xpos
         ldy ypos
         jsr calcwid   ;Cursor
         ldy #0
         lda (acl),y
         sta store
         eor #128
         sta (acl),y
         lda ach
         sta spot+1
         lda acl
         sta spot

         ldx xreal    ;Screen  pos calcs
         ldy yreal    ;absolute location
         jsr calcwid        ;& indirects
         lda acl
         sta smc1+1
         lda ach
         sta smc1+2
         lda #<screen+80
         sta smc2+1
         lda #>screen+80
         sta smc2+2

         ldx #20          ;Transfer text
loop6    ldy #39            ;onto screen
smc1     lda $6000,y
smc2     sta $0400,y
         dey
         bpl smc1
         clc
         lda smc1+1
         adc #wid
         sta smc1+1
         lda smc1+2
         adc #0
         sta smc1+2
         clc
         lda smc2+1
         adc #40
         sta smc2+1
         lda smc2+2
         adc #0
         sta smc2+2
         dex
         bpl loop6

         ldy #0            ;Restore char
         lda store         ;under cursor
         sta (spot),y

         lda #<screen+35  ;Show cursor
         sta indr         ;coordinates
         lda #>screen+35
         sta indr+1
         lda xpos
         jsr hex
         inc indr
         lda ypos
         jsr hex

         jsr pageon

;--------

inkey    jsr getin            ;Key input
         cmp #0
         beq inkey
         ldx xpos
         ldy ypos

;--------
key0     cmp #17
         bne key1
         cpy #maxlines
         bcs key1
         inc ypos
         jmp draw

;--------
key1     cmp #145
         bne key2
         cpy #0
         beq key2
         dec ypos
         jmp draw

;--------
key2     cmp #29
         bne key3
         cpx #wid-1
         bcs key2a
         inc xpos
key2a    jmp draw

;--------
key3     cmp #157
         bne key8
         cpx #0
         beq key8
         dec xpos
         jmp draw

;--------
key8     cmp #19
         bne key20
         lda #0
         sta xpos
         sta ypos
         jmp draw

;--------
key20    cmp #endkey
         bne key22
         lda modifed
         beq *+5
         jsr chkexit
         lda #147
         jsr $ffd2
         clc
         lda #0
         rts

;--------
key22    cmp #savkey
         bne key26
         jmp save

;--------
key26    cmp #pupkey
         bne key27
         lda ypos
         sec
         sbc #16
         bcs *+4
         lda #0
         sta ypos
         jmp draw

;--------
key27    cmp #pdownkey
         bne key13
         sec           ;Prevent mod256
         lda #maxlines ;wrapping
         sbc ypos
         cmp #16
         bcs key27a
         lda #maxlines
         bne key27b

key27a   lda ypos
         sec
         clc
         adc #16
key27b   sta ypos
         jmp draw

;--------
key13    cmp #typekey
         bne key9
         lda outtype
         tax
         cmp #cbmcbm
         bne *+4
         ldx #cbmasc
         cmp #cbmasc
         bne *+4
         ldx #cbmpet
         cmp #cbmpet
         bne *+4
         ldx #cbmcbm
         stx outtype
         jmp refresh

;---------
key9     sta modifed

         cmp #147          ;Clear buffer
         bne key4
         lda #0
         jsr message
         jsr answer
         cmp #89
         beq *+5
         jmp inkey
         jsr clrbuf
         jmp refresh

;--------
key4     cmp #tabkey
         bne key5
         jmp loop7a

loop7    inx
         lda imode
         bne loop7b
         txa
         pha
         jsr inschar
         inc xpos
         pla
         tax

loop7b   lda tabbar,x
         cmp #42
         beq key4a
loop7a   cpx #wid-1
         bne loop7
key4a    stx xpos
         jmp draw

;--------
key5     cmp #togkey
         bne key6
         lda tabbar,x
         eor #7
         sta tabbar,x
         jmp draw

;--------
key6     cmp #20
         bne key7
         jsr pageoff
         jsr getline
         ldx xpos
loop8    lda buffer,x
         sta buffer-1,x
         inx
         cpx #wid
         bcc loop8
         lda #32
         sta buffer+wid-1
         jsr putline
         ldx xpos
         beq key14
         dec xpos
key14    jmp draw

;--------
key7     cmp #148
         bne key10
         jsr inschar
         jmp draw

;--------
key10    cmp #141
         beq key10b

key10a   cmp #13
         bne key11
key10b   tax
         cpy #maxlines
         bcs key11
         inc ypos
         lda #0
         sta xpos
         cpx #141
         beq key10c
         lda imode
         bne key10c
         jmp insert
key10c   jmp draw

;--------
key11    cmp #lodkey
         bne key12
         jmp load

;--------
key12    cmp #capskey
         bne key18
         lda caps
         eor #1
         sta caps
         jmp refresh

;--------
key18    cmp #inskey
         bne key19
         jmp insert

;--------
key19    cmp #remkey
         bne key21
         jmp remove

;--------
key21    cmp #imodekey
         bne key28
         lda imode
         eor #1
         sta imode
         jmp refresh

;--------
key28    pha
         and #31
         sta store
         pla
         lsr a
         lsr a
         lsr a
         lsr a
         lsr a
         tay

         lda ascmask,y
         cmp #255
         bne *+5
         jmp inkey   ;Bad char, ignore

         ldx caps
         beq conv    ;Chk if capslock
         cmp #0
         bne conv
         lda #64
conv     ora store
         pha
         lda imode
         bne keyz
         ldx xpos
         cpx #wid-1
         beq keyz
         jsr inschar
keyz     pla
         jsr pageoff
         ldy #0
         sta (spot),y
         ldx xpos
         lda #29
         jmp key2

;---------------------------------------

title    .text "{CBM-Z}{CBM-Z}{CBM-Z}RATSWORD{$a0}V{CBM-H}{CBM-Z}{CBM-Z}{CBM-Z}"
imode    .byte 0
caps     .byte 0
intype   .byte 0
outtype  .byte 0
inout    .byte 0,0,0,1,2
hextxt   .text "0123456789abcdef"
ascmask  .byte 255,32,0,64,255,96,64,96

;---------------------------------------

inschar  jsr pageoff
         jsr getline
         ldx #wid-1
insc1    lda buffer-1,x
         sta buffer,x
         dex
         cpx xpos
         bne insc1
         lda #32
         sta buffer,x
         jsr putline
         jmp pageon

;---------------------------------------

calcwid  sty auxl    ;Find start of line
         lda #0
         sta ach
         sta auxh
         lda #wid   ;Wid >256, come on!
         sta acl
         clc
         txa
         adc #<textbase
         sta xtndl
         lda #>textbase  ;" above
         sta xtndh

         ldy #16    ;Thanks to (The) Woz
mul2     lda acl    ;& Bob Hart for this
         lsr a         ;multiply routine
         bcc mul4
         clc
         ldx #254
mul3     lda xtndl+2,x
         adc auxl+2,x
         sta xtndl+2,x
         inx
         bne mul3
mul4     ldx #3
mul5     ror acl,x
         dex
         bpl mul5
         dey
         bne mul2
         rts

;---------------------------------------

hex      pha
         lsr a
         lsr a
         lsr a
         lsr a
         ldy #0
         jsr hex1
         pla
         and #15
hex1     tax
         lda hextxt,x
         sta (indr),y
         inc indr
         rts

;---------------------------------------

getline  ldx #0         ;Get line#A and
         ldy ypos       ;place in buffer
         jsr calcwid
         ldy #wid-1
loop10   lda (acl),y
         sta buffer,y
         dey
         cpy #255
         bne loop10
         rts

;---------------------------------------

putline  ldy #wid-1      ;Opposite above
loop11   lda buffer,y
         sta (acl),y
         dey
         cpy #255
         bne loop11
         rts

;---------------------------------------

message  asl a          ;Get string from
         tay            ;table & display
         lda mesvect,y
         sta indr
         lda mesvect+1,y
         ldy #0
         sta indr+1
loop13   lda (indr),y
         cmp #64
         bne mess1
         rts
mess1    sta screen+960,y
         iny
         bne loop13

;---------------------------------------

mesvect  .word cls,lfile,sfile,nomem
         .word savbad2,loadbad,exitt

cls      .text "clear screen?@"
lfile    .text "load filename:@"
sfile    .text "save filename:@"
nomem    .text "memory empty@"
savbad2  .text "write error@"
loadbad  .text "load error@"
exitt    .text "save modified buffer?@"

   ;If the mesvect table is wrong,things
   ;will crash

;---------------------------------------

answer   jsr getin         ;Await Y or N
         cmp #89
         beq answ1
         cmp #78
         bne answer
answ1    pha
         jsr clrbot
         pla
         rts

;--------
waitany  jsr getin       ;Await anything
         beq waitany
         rts

;---------------------------------------

clrbot   lda #32          ;Scrub message
         ldx #39
loop14   sta screen+960,x
         dex
         bpl loop14
         clc
         rts

;---------------------------------------

filenam  ldy #0 ;Input filename from usr
         sty store
         lda #petcbm
         sta convmode

fileget  jsr getin
         beq fileget
         cmp #20
         beq filedel
         cmp #32           ;I wish GETIN
         bcc file1      ;talked real ASC
         cmp #128
         bcc fileg1
         cmp #224
         bcs fileget
         cmp #192
         bcc fileget

fileg1   sta tmp1
         jsr convert
         ldy store
         sta input,y
         lda tmp1
         cpy #16
         beq file1
         inc store

file1    cmp #13
         beq endfile
         jmp fileget

filedel  ldy store            ;Backspace
         dey
         bmi fileget-2
         lda #32
         sta input,y
         sty store
         jmp fileget


endfile  ldy store      ;Exit from input
         bne loop     ;empty line, abort
         jsr clrbot
         sec
         rts

loop     lda input,y    ;Move into fname
         ldx #cbmpet
         stx convmode
         jsr convert
         sta buffer,y
         dey
         bpl loop
         ldx store
         stx flength
         ldx #<buffer
         ldy #>buffer
         stx fname
         sty fname+1
         jmp clrbot

;---------------------------------------

insert   ldx #0       ;Insert blank line
         ldy ypos
         jsr calcwid
         jsr pageoff
         lda acl
         sta spot
         lda ach
         sta spot+1
         lda max
         sta smc3+1
         lda max+1
         sta smc3+2
         bne ins2

ins1     lda smc4+2
         cmp spot+1
         bne ins2
         lda smc4+1
         cmp spot
         bne ins2

         lda #32
         ldy #wid-1
ins3     sta (spot),y
         dey
         cpy #255
         bne ins3
         jmp draw

ins2     clc
         lda smc3+1
         adc #wid
         sta smc4+1
         lda smc3+2
         adc #0
         sta smc4+2

         ldy #wid-1
smc3     lda $d000,y
smc4     sta $d050,y
         dey
         cpy #255
         bne smc3
         sec
         lda smc3+1
         sbc #wid
         sta smc3+1
         lda smc3+2
         sbc #0
         sta smc3+2
         jmp ins1

;---------------------------------------

remove   ldx #0
         ldy ypos
         jsr calcwid
         jsr pageoff
move1    clc
         lda acl
         adc #wid
         sta indr
         lda ach
         adc #0
         sta indr+1
move2    ldy #wid-1
move3    lda (indr),y
         sta (acl),y
         dey
         bpl move3
         clc
         lda acl
         adc #wid
         sta acl
         lda ach
         adc #0
         sta ach
         cmp max+1
         bne move1
         lda acl
         cmp max
         bne move1
         ldy #wid-1
         lda #32
move4    sta (max),y
         dey
         bpl move4
         jmp draw

;---------------------------------------

preload  jsr clrbuf
         lda #<textbase
         sta spot
         lda #>textbase
         sta spot+1
         ldx stdinum
         lda #3
         jsr openfnum
         lda stdinum
         jmp load2

;--------
load     lda #1
         jsr message
         jsr filenam
         bcc load1
         jmp draw

load1    lda #3
         jsr open
         bcs loaderr
load2    sta filenum

         lda spot      ;Load from cursor
         sta indr
         lda spot+1
         sta indr+1

         ldy #0
loadchr  ldx filenum
         jsr chrin
         bcc loadchr1
         cmp #12
         beq lexit
         bne loaderr

loadchr1 cmp #13
         beq carr
         ldx intype
         stx convmode
         jsr convert
         jsr pageoff
         sta (indr),y
         jsr pageon
         iny
         bne loadchr

carr     clc
         lda indr
         adc #wid
         sta indr
         lda indr+1
         adc #0
         sta indr+1
         ldy #0
         lda indr
         cmp max
         lda indr+1
         sbc max+1
         bcs lexit
         bcc loadchr

lexit    lda filenum
         jsr close
         jmp refresh

loaderr  lda #5
         jsr message
         jsr waitany
         jsr clrbot
         jmp lexit

;---------------------------------------

save     jsr fend
         bcc save1
         jmp draw

save1    lda #2
         jsr message
         jsr filenam
         bcc save2
         jmp draw

save2    lda #5
         jsr open
         bcs saverr
         sta filenum

savman   lda spot               ;Chk EOT
         cmp acl
         lda spot+1
         sbc ach
         bcs savend

         ldy #wid-1            ;Find EOL
         jsr pageoff
savlp    lda (spot),y
         cmp #32
         bne bran
         dey
         cpy #255
         bne savlp
         beq term

bran     sty tmp1
         ldy #0              ;Write line
savlp2   lda (spot),y
         jsr pageon
         ldx outtype
         stx convmode
         jsr convert
         ldx filenum
         jsr chrout
         jsr pageoff
         bcs saverr
         cpy tmp1
         beq term
         iny
         bne savlp2

term     jsr pageon
         lda #13
         ldx filenum
         jsr chrout
         bcs saverr
         clc
         lda spot
         adc #wid
         sta spot
         lda spot+1
         adc #0
         sta spot+1
         jmp savman

savend   lda filenum
         jsr close
         lda #0
         sta modifed
         jmp draw


saverr   lda #4         ;Write failed
         jsr message
         jsr waitany
         jsr clrbot
         jmp savend

;---------------------------------------

fend     lda max    ;locate last char of
         ldx max+1  ;file (non-space)
         sta acl
         stx ach
         ldy #0
         jsr pageoff
         lda #<textbase
         sta spot
         lda #>textbase
         sta spot+1
finend   lda (acl),y
         cmp #32
         bne fend1
         ldx acl
         bne decend
         dec ach
decend   dec acl
         lda ach
         cmp #>textbase
         bne finend
         lda acl
         cmp #<textbase
         bne finend
         jsr pageon
         lda #3
         jsr message
         jsr waitany
         jsr clrbot
         sec
         rts
fend1    clc
         jmp pageon

;---------------------------------------

clrbuf   lda max           ;Clear buffer
         sta indr
         lda max+1
         sta indr+1
         jsr pageoff

loop12   ldy #wid-1
         lda #32
cls1     sta (indr),y
         dey
         cpy #255
         bne cls1

         sec
         lda indr
         sbc #wid
         sta indr
         lda indr+1
         sbc #0
         sta indr+1

         lda indr
         cmp #<textbase
         lda indr+1
         sbc #>textbase
         bcs loop12

         lda #0
         sta xpos
         sta ypos
         jmp pageon

;---------------------------------------

chkexit  lda #6
         jsr message
         jsr answer
         cmp #"n"
         bne *+3
         rts
         jmp save

;---------------------------------------
tmp1     .byte 0
filenum  .byte 0
modifed  .byte 0

;---------------------------------------

buffer   = *
tabbar   = buffer+256

;---------------------------------------


