[comp.sys.cbm] RS232 Replacement Routine

aliu@nunki.usc.edu (Terminal Entry) (08/24/89)

I found this source on a bbs, and thought I'd share it with everybody.
It replaces the bult-in RS232 routines on the c64 allowing reliable 2400
baud communications.

The file was originally in PAL format, and had absolutelly no comments. So,
you may blame me for faulty commenting. But please don't blame me for faulty
coding.

When I tested it out, I first called SETUP and INABLE (sic) after I opened
the channel as file #2 (or 5,  use them interchangebly). Then, I used RSGET
instead of the kernal getin ($ffe4) to get bytes from the modem. I am not sure
what you would use RSOUT for, I didn't find it required. However, I did NOT
make extensive test wth it. Just a couple of minutes on-line at 2400. The
text came throught crystal clear at 2400.

Enjoy.

------------------------------- CUT HERE --------------------------------------

.obj "new-rs232.ml"   ; buddy pseudo-op to assemble  to disk
 
 
;   Replacement  rs-232 routine for the Commodore 64
;   to enable reliable transmission at 2400 bits/sec
    
;    Note: file is in Power Assembler 'EBUD' format 
;           object file is $1ee bytes.


;-----  equates table  -------------------------------

ribuf  = $f7    ; rs232 input buffer pointer
robuf  = $f9    ; rs232 ouput buffer pointer
baudof = $0299  ; rs232 baud rate
ridbe  = $029b  ; rs232 index to end of input buffer
ridbs  = $029c  ; rs232 start of input buffer
rodbs  = $029d  ; rs232 start of output buffer
rodbe  = $029e  ; rs232 index to end of ouput buffer
enabl  = $02a1  ; rs232 enable
nminv  = $0318  ; vector: nmi
ichkin = $031e  ; vector: chkin
ibsout = $0326  ; vector: chrout
dataa  = $dd00  ; data port a
datab  = $dd01  ; data port b
timblo = $dd06  ; timer B lo byte
timbhi = $dd07  ; timer B hi byte
rdnmi  = $dd0d  ; read/mask nmi
crega  = $dd0e  ; cia control reg a
cregb  = $dd0f  ; cia control reg b
rstkey = $fe56  ; 
norest = $fe72
return = $febc
oldout = $f1ca
oldchk = $f21b
findfn = $f30f
devnum = $f31f 
nofile = $f701 


;  jump table

jmp setup     ; setup new rs-232 routine
jmp inable    ; enable the new routine
jmp disabl    ; disable the new routine
jmp rsget 
jmp rsout 


nop

strt24 .byte 203,1
strt12 .byte  66,4
strt03 .byte  51,19
full24 .byte 165,1
full12 .byte  77,3
full03 .byte  82,13

setup = *

lda #<nmi64    ; redirect nmi vector
ldy #>nmi64
sta nminv+00
sty nminv+01
:
lda #<nchkin   ; redirect chkin vector
ldy #>nchkin
sta ichkin+00
sty ichkin+01
:
lda #<nbsout   ; redirect chrout vector
ldy #>nbsout
sta ibsout+00
sty ibsout+01
rts

;-----  new nmi routine here  -----------------------

nmi64 = *
:
pha          ; save registers
txa
pha
tya
pha
cld
ldx timbhi   ; $dd07
lda #$7f
sta rdnmi    ; $dd0d
lda rdnmi
bpl notcia
cpx timbhi   ; $dd07
ldy datab    ; $dd01
bcs mask
ora #$02
ora rdnmi    ; $dd0d

mask = *
:
and enabl
tax
lsr
bcc ckflag
lda dataa    ; $dd00
and #$fb
ora $b5
sta dataa    ; $dd00
ckflag txa
and #$10
beq nmion
 
strtlo = *
:
lda #$42
sta timblo   ; $dd06
 
strthi = *
:
lda #$04
sta timbhi   ; $dd07
             
lda #$11
sta cregb    ; $dd0f
lda #$12
eor enabl
sta enabl
sta $dd0d
 
fulllo = *
:
lda #$4d
sta timblo   ; $dd06
 
fullhi = *
:
lda #$03
sta timbhi   ; $dd07
 
lda #$08
sta $a8
bne chktxd
 
notcia = *
:
ldy #00
jmp rstkey
 
nmion = *
:
lda enabl
sta rdnmi    ; $dd0d
txa
and #$02
beq chktxd
tya
lsr
ror $aa
dec $a8
bne txd
ldy ridbe
lda $aa
sta (ribuf),y
inc ridbe
lda #$00
sta cregb    ; $dd0f
lda #$12
 
switch = *     
:
ldy #$7f
sty rdnmi    ; $dd0d
sty rdnmi    ; $dd0d  (redundand?)
eor enabl
sta enabl
sta rdnmi    ; $dd0d
 
txd = *
:
txa
lsr
 
chktxd = *
:
bcc exit
dec $b4
bmi char
lda #$04
ror $b6
bcs store
 
low = *
:
lda #$00
 
store = *
:
sta $b5
 
exit = *
:
jmp return
 
char = *
:
ldy rodbs
cpy rodbe
beq txoff
 
getbuf = *
:
lda (robuf),y
inc rodbs
sta $b6
lda #$09
sta $b4
bne low
 
txoff = *
:
ldx #$00
stx crega    ; $dd0e
lda #$01
bne switch


;-----  routine to disable the new-rs232  ------------

disabl = *
:
pha
  
test = *
:
lda enabl
and #$03
bne test
lda #$10
sta $dd0d
lda #$02
and enabl
bne test
sta enabl
pla
rts


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

nbsout = *
:
pha
lda $9a
cmp #$02
bne notmod
pla
 
rsout = *
:
sta $9e
sty $97
 
point = *
:
ldy rodbe
sta (robuf),y
iny
cpy rodbs
beq fulbuf
sty rodbe
 
strtup = *
:
lda enabl
and #$01
bne ret3
sta $b5
lda #$09
sta $b4
ldy rodbs
lda (robuf),y
sta $b6
inc rodbs
lda baudof
sta $dd04
lda baudof+1
sta $dd05
lda #$11
sta crega    ; $dd0e
lda #$81
 
change = *
:
sta rdnmi    ; $dd0d
php
sei
ldy #$7f
sty rdnmi    ; $dd0d
sty rdnmi    ; $dd0d (redundand?)
ora enabl
sta enabl
sta rdnmi
plp
 
ret3 = *
:
clc
ldy $97
lda $9e
rts
 
fulbuf = *
:
jsr strtup
jmp point
 
notmod = *
:
pla
jmp oldout


;

nchkin = *
:
jsr findfn
bne nosuch
jsr devnum
lda $ba
cmp #$02
bne back
sta $99
 
inable = *
:
sta $9e
sty $97
 
baud = *
:
lda baudof+1
and #$06
tay
lda strt24,y
sta strtlo+1
lda strt24+1,y
sta strthi+1
lda full24,y
sta fulllo+1
lda full24+1,y
sta fullhi+1
lda enabl
and #$12
bne ret1
sta cregb    ; $dd0f
lda #$90
jmp change
 
nosuch = *
:
jmp nofile
 
back = *
:
lda $ba
jmp oldchk


;-----  new rs232 get routine  -----------------------

rsget = *
:
sta $9e
sty $97
ldy ridbs
cpy ridbe
beq ret2
lda (ribuf),y
sta $9e
inc ridbs
 
ret1 = *
:
clc

ret2 = *
:
ldy $97
lda $9e

last = *
:
rts

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

.end

Nelson.Dasilva@zswamp.fidonet.org (Nelson Dasilva) (08/31/89)

I have a 2400 baud modem but cant use it at 2400 baud because i dont have 
the right software,T.T. 3.9 dont work with it,the only thing that work 
was VIP Termial but that has garbage sometime on 1200 baud(to bad)
Now how could i use that code you made to work the 2400 baud.
I dont know much about ml code or entry it,closes thing it the back of a 
Compute! mag. !!!



--  
Nelson Dasilva - via FidoNet node 1:221/171
UUCP: {{uunet!}watmath!xenitec!}zswamp!Nelson.Dasilva
ARPA: Nelson.Dasilva@zswamp.fidonet.org