300 31 12MB
English Pages 194 si [208] Year 1985
The Commodore 64 Kernal and Hardware Revealed
Also by Nick Hampshire
The Commodore 64 ROMs Revealed
o 00
383087 X
Advanced Commodore 64 Graphics and Sound
o 00
383089 6
Advanced Commodore 64 BASIC Revealed
o 00
383088 8
The Commodore 64 Disk Drive Revealed
o 00
383091 8
The Commodore 64 IJ use
T CAP. Z20Pf ONly WITP-j Rr v!A.SrON OF '5",
ClOO
.22
A 001 , .....DeI ......
----,q.-"~AC
LED (ONNEtTDR eNID
4
3
Fig. 1.2. (contd.)
The Commodore 64 Kernal and Hardware Revealed
4
The required input/ output is supplied by two 6526 CIA chips. Together these two chips supply 4 timers used for IRQ timing, the tape system and serial. Their I/O ports are used for keyboard scanning, the user port, serial ports and VIC chip bank select. These chips also have serial ports and time of day clocks but these are not used in the CBM 64. Lastly sound is generated by the SID chip. This has 3 voices each with 4
I
8
I
7
5.J!~'q;",,1 ~~Bn ~::ff ~7fV:ZZfl ~Gn c:~-tl. Cz~snJ:on, ~'"
.,~
U1"3 Y
~
q"' ~~:
~ 80
1. ~
,
..
~f1oI4 ~ I~"'A"
C~4
t'3
Bt
~8' G:'
r----' I
ClO1
=
~
I
UII
UZS
UIO
un
U1
UZI
41CAi
41CO>H
RAM
RAM
4"4-2
41104.1 RAM
4"
ROM
""
r=
-
~ ~
(AOOO-
=~1'S
~
~
f,l
,I~"'I..J,,~, Inl •
~ AI
~
~
~
07[)1;.OS[IIIOlOlOlCC
AO,
. . - - - ! '"
~
=-
~' ~ ~ ~ 7
-r f'J' ¥'
'l
•" ........ 00..,
B
RAM
;
Ul23w
~
RAM
~~
~
ffr ..!
c
UZ4 41(04
'3 02',>4 029:) 0291 0:l98
0299 029l'. 0298 029['. 029B 029C 02U 0290 0290 \j21:. 029F 029F 02A1 02A1 02A1 0300 0314 0j16 0318 031A 031C 0j1E 0320 0322
0324 0326 0328 032A 032C 03:.!E 0330 0332 033" 0334 033C 03fC 03FC 0400
*'"*+1 *"'*+1 *"'*+1 *=*+1 *"'*+1 *"'*+1 *=*+1 *=*+1 *=*+1 *=;.:+2
*"*+1 *"'*+1
;ACTIVE COLOUR NIBBLE ,URIGINAL COLOUR BEFORE CURSOR ,BASE LUCATIUN OF SCR!'.EN (TOP) ;KEY REPEAT FLAG ,SHIFT FLAG BYTE ,LAST SHIFT PA1TERN ;INOIRECT FOR KEYBUARD fABLE SETUP ;0-PI:T MUDE, 1-CArTACANNA ; AUTO SCRULL DOWN FLAG ("'0 ON, 00 OFF>
:RS-232 STORAGE
·
M26C"1 R M26CUR M26AJB RSSIAT 8I1NUM l'.AUDOF
*=*+ 1 *=*+1 *"'*+2 *=*+1 *"'*+1 *=*+2
,6:)26 CONTROL REGISTER ;6526 CUMMANO REGISTER ;NUN STANDARD (BITTIME/2-100) ;RS-232 STATUS REGISTER ;NUMBER UF BI1S 10 SENO (FAST RESPONSE) ;BAUO RAfE FULL BIT fIME
RIOBE RIL)l'.S
*"'*+1 *=*+1
;INPUT BUFFER INDEX 1U ENO ;INPUT BUFFER PUINTER TO S1ART
;rRANSMITTER SrORAGE , ROUBS *=*+1 ROUE'.f:. *='-'+1
;OUTPUT BUFFER INDEX 10 SlART ;OUTPur BUFFER INDEX 10 ENO
IRtlfMP
*=*+~!
;HOLDS IRQ DURING TAPE OPS
*=$0300 *=$0300+20 *=*+2
;PROGRAM INOIR!'.CTS(10) :KERNAL/OS INUIRECTS (20) ; IRGI RAM VECTOR ;BRK INsrR RAM VECTUR ;NMI RAM VEC/UR ;INOIRECTS FUR CODE ;CUNFORMS TO KERNAL SPEC 8/19/80
·;RE.CIEVER STORAGE
·
CINV CBINV
*=:.:-+2
NMIIN
*=*+~
IOF·I:.N lCLUS!'. ICHKIN ICI:OUT lCLRCH I l'.ASI N IBSUUT 15"1 UP
*=*+2 *=*+2 *=*+2 *=*+2
*=*+2 *=*+2 *=*+2 *=*+2 WETIN *=*+2 ICLALL *=*+2 USRCMO *=*+2 ILUAD *=*+2 ISAVE *=*+2
,SAVESP
*=$0300+60 Tl'.UFFR *=*+192
,CASSETTE DATA BUFFER
*=$0400 CE'.MSCN *=*+99
;64 SCREEN
0/EI
07E7 0800 0800
; TOP UF MEMURY ;IEEE TIMEOUT FLAG
,RAMLUC
*=$0800
Serial Communications 41 LOC 01300 0000 D0:.!!'" 00:.!1'" 0400 0410 0410 01300 08E.! OSE} 08E7 OSE7 OC00 DC00 OC01 OC01 DC02 0(;03 OC04 OC05 OC06 OC07 DC08 OC09 OC0A DC08 oe0C DC00 OC0E oC0F 0(;10 DC10 D000 D001 0002 0003 0004 ODes 0006 0007 0008 0009 000A 0008 000C 0000 Ll00E 000F LlO10 0010 OLl10 OLl10 0010 0010 0010 D010 0010 0010 0010 D010 0010 D010 0010 0010 0010 0010 D010
CODE
LINE
*=$Ll000 VICREU *=IH4i'
,VIC REUlSlERS
*=$0400 SIORE.6 ;o;=IH29
,SID REGISTERS
C~.MCUL
·
*=$0800 *=11,+999
,64 COLOUR NIBBLES
;I1U DEVICES
COLM 01DPA ROWS D1DI-·~.
LllLlORA 0100RB LlIJAL OlTAH 01T8L 01 re.H 1>1 TOLl 1 01TU02 01 ruOJ 01T004 1H lOLlS 1H ICR 01CkA 01CRS 020PA 020F'S I):.!LlORA D20DRB 021AL 02TAH 02H·.L 021'~.~1
02.,.001 02T002 Ll2'J 003 021'004 021008 D21CR D2CRA 02CRB
·
*
=$OC00
;6526 (IRQ)
;KEYBOARO MATRIX
*=*+1
; KEY~.OAR(.J MATR I X
*=*+1 *=*+1 *=*+1 *=*+1 *=If+ 1 *=*+1 *=*+1 *=IHl *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *= $0000 *=*+1 *=*+1 *=*+1 *=*+1 *=IHl *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *=*+1 *=lHl *=*+1 *"'*+1
;6526 (NMO
,TAPE BLOCK TYPES I
EOT
=5
~\LF
,END OF TAPE
"1 "2 =3 =4 "192
,BASIC LOAD FILE ,BASIC DATA f"lLE ;I'IXED PROGRAM HPE ,BASIC DATA FILE HEAOER ;BUFFER SIZE
SOF f'LI' BOFH BUFSl
,; TAPE ERROk
TYPES
;
SPERR CKERR SSERR LBERR
=16 =32 =4 =8
SCREEN EDITOR CONSTANTS
42 The Commodore 64 Kernal and Hardware Revealed LOC CODE LINE L.L£N LLEN2 NUNES BLUE LTF.LUE CR MAXCHR
0010 0010 D010 0010 0010 0010 0010 0010 0010 0010
N~JRAF'
=40 =90 =2~j
=6 =14 =$0 =80 =2
-SINGLE LINE 40 COLUMNS ;OOUBLE LINE 80 COLUMNS ;25 ROWS ON SCREEN ;BLUE SCREEN COLOUR -LT BLU£ CHAR COLOUR ;CARRIAGE RETURN
.ENO .LIB KSERl
TALK
Entry point: $FFB4 Function: Command serial device to talk (transmit data) Input parameters: .A device number Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within I ms Frame error (in ST) - no data accepted response (data low) within I ms of last bit of byte being sent. Description: This routine ORs the device number in the .A register with $4.0. Before the command is sent the single character serial buffer is checked for being empty. If it is not the character in it is sent (with end message marker (EOI)). After this the attention line is set low (bit 3 set in chip 2 port A (assembler label D2DP A in listing). Then the command byte is sent with the attention line held low. LOC
CODE
LINE
0010 E::009
*=$1::.009
W09
;.COMMANO SERIAL BUS 10 TALK. ,*THE ACCUMULATOR MUST BE LOAOI::.O WITH THE ,.OEVICE NUMBER THAT YOU WISH TO TALK. ;**************************************** L936 ORA "$40 ,MAKE ADDR TALK .BYT $2C ,SKIP NEXT COMMAND
E009 E009 E009 E009 El>08
;*********~***********************~******
09 40 2C
LISTEN
Entr.-v point: $FFBI
Serial Communications 43 Function: Command serial device to listen Input parameters: .A device number Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within 1 ms Frame error (in ST) - no data accepted response (data low) within 1 ms of last bit of byte being sent Description: This routine ORs the device number in the .A register with $2.0. Before the command is sent the single character serial buffer is checked for being empty. If it is not the character in it is sent with an EOI handshake to mark it as the last byte of its message. After this the attention line is set low. Then the command byte is sent with the attention line held low. This routine includes the main routine to send a byte to the serial bus. This is done as follows: L842 -
L859 -
L85.0 -
L848 -
L855 -
Set clock low Set data high Delay I ms Set data high (released) Set clock line high If EOI no handshake required then L850 ; Wait with clock high for End Or Identify handshake Wait for data high Wait for data low ; That is end of hold, until serial device is ready Wait for data high Set clock low Put 8 in counter If data not high framing error Get next bit of byte to send ; Low bit first If bit is zero then set data low Set clock high ; flag bit Sort pause Set data high and clock low Decrease bit counter Go to L848 if not all sent Set timer for I ms Has timer expired? If so then L847 (framing error) If data not low go back to L855 Exit
44 LOC
The Commodore 64 Kernal and Hardware Revealed CODE LINE
I:.oec E00C E.00C ED0C 1:.00C E00C E00C E00E EOll E012 !:.O14 E016 ED16 E016 1:.016 E017 ED19 E01C !:.Ol1:. E020 E021 E023 E024 E02l E029 E02B E:02E E031 E033 1:.036 E036 E037 E03A E030 E04e E041 E044 E047 E049 E04C ED4E EO!)0 E05e E053 EDS!) EO!)!:! 1:.05A E050 EOSF E062 E062 £D62 [062 E064 E066 E069 E06C l06E ED6F E071 ED73 E07!) ED7e lOlA EOID E080 ED81 [082
09 20 4!:! 24 10
20 A4 F0 94 0A
;**************************************** ;*CUMMANO SERIAL BUS TO LISTEN. ,*TU USE THIS ROUTINE. THE ACCUMULATOR MUST ;*FIRsr BE LOADED WITH THE DEVICE NUMBER THAT ,*YOU WISH TU LISTEN (RE.CEIVE DATA). ;**************************************** L966 URA Mt20 ;MAKE AOOR LISTEN JSR eF0A4 ,PROTECT FROM RSJ2J NI'II L9se PHA BII" C3f'O ,GHAR IN BUFF·ER? BPL L864 ,NO
,
,SEND BUFFERED CHAR
38 66 20 46 46 68 85 78 20 C9 De 20 AD 09 80
A3 40 ED 94 AJ L864 95
97 JF 03 8S 00 08 410
EE EE 00
Le39
DO J
78 20 20 20 78 20 20 B0 20 24 10
97 EE A9 EE 64 85 EE AJ 0A
20 90 241 B0 20 90 241
A9 FB A9 FB A9 FB 8E
8E EE 97 EE B3 EE
EE.
EE EE EE
L842
Le!:!9
SEC ROR JSR L9R LSR F'LA STA SEI JSR CMf' BNE JSR LOA URA SlA SEI JSR JSR JSR SEI JSR JSR SCS JSR SIr Bf'L
;00 EOI L840 JSR BCC JSR L849 8CS JSR L8se I!.CC JSR
R202 L8SS' CJPO R202 eSOUR L844 UJF UI39 Le75 020PA 11$08 020PA L843 L844 L!:!46 L844 L8S4 L856 L87S R202 L8se L!:!S4 L84e L854 L849 L8S4 La!)0 L843
,SET EOI FLAO ,SEND LAST CHAR ,BUFFER CLEAR ,CLEAR !:.OI RAG ,rALK/LISTEN AOOR
,CLKHI ONLY ON UNLISTEN ,ASSERT ATTENTION
ISH CLOCK LINE LOW JOELAY 1 MS ,DISABLE IR~l ;MAKE SURE DATA IS RELEASED ,0AlA SHOULD BE LOW ;CLUCK LINE HI ;EOI FLAG TEST ,WAIT FUR DATA HI ,WAIT FOR DATA LO ;WAIT FOR DATA HI ;SET CLOCK L.U
;
; SET "10 SENO OAT A A9 85 AD CD 00 0A 'Ie 66 Be 20 De 20 20 EA EA EA
418 A5 041 00 00 00 Fa
Le48
B~IE
3F 'I::;
0S A0 1:":E 413 97 EE 85 E:E
LOA STA LOA CMP
LaSl L8S3
ASL BCC RUR BCS JSR SNE JSR JSR NUP NOP NOP
11$08 COUNT D20f'A D2DPA L848 A L847 8S0UR L8S1 L841 LS53 L944 L87S
; COUNT B BITS !
OH.OUNCE BUS
;DATA MUST BE HI ,NEXT BIT INro CARRY
Serial Communications 45 LOC
COOl:.
EDB3 EOl:l4 [01:17 EOBY EoaB I:.08E [0918 El>y:;! E.094 EO'll E099 tD9C E09F EOA2 EDA4 EDM EDAY EDAB [DAC El>AD EOAF EOB0 U)B2 EOB5 EDB6 EDBI
LINt
EA AD 00 DO 29 DF 0Y 10
aD
C6 De A9 80 A9 8D AD AD
NOP LOA AND ORA SlA DEC
00 00 A~
04 04 187 DC
19 01'
DC
00 DC eo DC
L855
2Y 02 018 tA
20 A9 n: "'.0 F4 51:1 618 A9 B0 2C A'~ 03 20 lC Ft 58 18 90 4A
La56 LI:I47 L852
020f'A 11$0': 11$19
020f'A COUNT ~.NI:: L84B LOA 11$04 srA OlTBH L[)A 11$19 STA DICRB LOA OllCR U)A OIICR AND 11$02 I'.HE LB47 JSR L854 ~.CS L855 CLI RTS LOA 11$80 .IWT $2C LDA 11$03 JSR $FEIC
,DAIA HI ,CLOCK LO
,SET TIMER FOR 1 1'19
;ENAl'.LE IRtl ,DEVICE Nor PRESENT
eLI
;FRAMINO ERROR ;SEND MtSSAGE ;ENABLE IRQ
CLC BCC L1004
;ALWAYS
SECOND
Entry point: $FF93 Function: Send secondary address after listen Input parameters: Secondary address in .A register ORed with $6.0 Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within I ms Frame error (in ST) - no data accepted response (data low) within 1 ms of last bit of byte being sent Description: The secondary address is stored in the serial buffer and then sent to listening devices. Next the attention line is released (set high). LOC
CODE
EDB9 EOIW EDB9 EUB9
;~*************~*****~*******************
;~SEND SECONDARY ADDRESS AFTER LISTEN. ;*rHlS ROUTINE IS USED TO SEND A SECONDARY ,.AOORESS AFTER A CALL TO THE LISTEN COMMAND.
E.[)IW
EOB9
LIN£
85 95
, ••••••••••••••••••• ********** •••••••••••
L811
srA BSOUR
;BUFFER CHAR
46 The Commodore 64 Kernal and Hardware Revealed EUE'.H 20 36 ED JSR LB42 ;SI:":ND IT EUHE : WBE :RELEASE ATTENT'ON EOSE : toHE AD 00 00 L9B3 LOA D2DPA EOCI 29 F7 AND II$F7 ; RELEASE I:":DC3 BD 0" DD srA D2DPA EDe6 60 RTS
[TKSA Entry point: $FF96 Function: Send secondary address after talk Input parameters: Secondary address in .A register Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within I ms Frame error (in ST) - no data accepted response (data low) within I ms of last bit of byte being sent Description: The secondary address is loaded into the serial buffer and then sent to the serial bus. This routine then waits for the new talking device to acknowledge it is the new talker by changing the clock line. This is done as follows: Hold data low Set attention high (release) Set clock high Then wait for clock to go low LOC
CODE
LINE
EDC7 mGl [oC7
;*~**************************************
[DC7
tDCl lOCI EDC7 EDC9 EDCC EDec EDCC EDCC EDCO £000
EDD3 EDD6 EDD9 EDDB EDDC
85 95 20 36 ED
lB
20 A0 EE
20 20 20 30 58 60
BE EO 85 !::E. A9 EE:: FB
I*SEND lALK SA. :*THIS ROUTINE IS USED TU SEND A SECONDARY :"ADDRESS TO A DEVICE THAT HAS ALREADY e.EEN ;*CUMMANDED TU TAU(. ;**************************************** Sf A BSUUR ,HUFFER CHAR LB6" JSR LB42 ,SEND SA , ;SHIFT OVER TO LISTENER : L970 SEI ,DISAe.LE IRQ JSR L841 ,DATA LINE LO JSR L9B3 JSR L875 ,CLOCK LINE HI L968 JSR L854 ,WAlT FOR CLOCK LO BI'lI L968 eLI ; DONE
.
RTS
Serial Communications
47
ClOUT
Entry point: $FF A8 Function: Send byte to serial bus Input parameters: Byte to send in .A Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within I ms Frame error (in ST) - no data accepted response (data low) within I ms oflast bit of byte being sent Description: Any character in the serial buffer is sent to the serial port. Then the current character is stored in the buffer. CODE
l.OC EDOO !:.DO 0 !:.DOD EOOO EDOF EI)U
EDE2 EDE4 EOE6 EOE6 EuE7 EOEA EDEB EDEO EI)EE
LINE
J**************************************** ;l'.UFFEREO ourpUT TO SERIAL BUS 24 94
;******~***********M**W******************
L86!
30 0~ 38 66 94 00 0S
1'.1 T
C3PO BMI l.949 SEC ROR C3PO !:'.NE L862
;BUFFERED CHAR? ;YES, SEND LAST ;NO ;!:>ET BUFFERED CHAR FLAG ,ALWAYS
PHA JSR UI59 F'lA SlA SSOUR CLC RTS
,SAVE CURRENl CHAR ;SENI) LAST CHAR ,RESTORE CURRENT ,BUFFER rr I (WOI) EXIT
;
48
L949
20 40 ED
68 85 95 18 60
U:l62
UNTlK
Entry point: $FF AB Function: Send command UNT ALK Input parameters: None Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within 1 ms
The Commodore 64 Kernal and Hardware Revealed
48
Frame error (in ST) - no data accepted response (data low) within I ms of last bit of byte being sent
Description: This routine sends the $5F under attention to serial bus. This tells the current talking to stop. After a delay this routine ends by releasing clock and data lines. LINE
CODE
LOC EDEF EOEF EDEF EOEF EDEF EDEF EDEF EOF0 EDF3 EOF6 EOFS [OF!!. EOFD
.***~************************************
; *SEND UN IAU(. ;.THIS RUUTINE SENDS AN 'UNTALK' TU THE SERIAL ;*8US. IT WILL TELL ALL DEVICES IN lALK •• MOOE TO STOP TALKIN6 (SENDING DATAl. ;** ••••••••••••••• * •• * •• * •••••••••••••••• UJ63 SEl JSR L84J LDA D2DPA ,PULL ATN URA N$08 STA D2DPA LOA N$5F ;UNTALK .BYT $2C ,SKIP NEXT CUMMAND
78 20 8E EE AD 00 DO 09 08 80 00 00
A9
51"
2C
UNlSN
Entry point: $FF AE Function: Send command UN LISTEN Input parameters: None Output parameters: None Registers used: .A Error messages: Device not present (returned in STatus var. $9.0) - attention not acknowledged by data low within I ms Frame error (in ST) - no data accepted response (data low) within I ms of last bit of byte being sent Description: This routine sends the $3F under attention to serial bus. This tells the current listening devices to stop. After a delay this routine ends by releasing clock and data lines. LOC
CODE
EDFE
LINE ;*****~**********************************
; *SENO UNLISTEN. ;.THIS ROUTINE SENDS AN 'UNLISTEN' TU ,.THE SERIAL BUS. IT WILL TELL ALL DEVICES ;.IN LISTEN MUDE TO STOP LISTENIN6.
EOH:: EO~E.
EOFE EDFE
;
EOF- E
EOf'E
AY 3F
E[00
20 11 EO
E.E03 EE03
....................................... .
L1006
LDA M$3F JSR L980
,UNLISTEN CUMMANO ,SEND
I
;RELEASE ALL LINES
EE03
EE03 EE06
20 BE EO
JSR L983
,RELEASE ATN
Serial Communications 49 EE06 EE06 EEfl6 EE07 EE09 EE0A EEflC EE00 EE10
,
,DELAY THEN RELEASE CLOCK AND DATA
SA
LS58
A2 0A CA
L876
00 FO AA 20 8S EE 4C 97 E.E.
TXA LDX 1I$0A DEX SNE LS76 TAX JSR L875 JPlP L844
,DELAY APPROX 60 MICRO SECS
ACPTR
Entry point: $FF A5 Function: Input byte from serial port Input parameters: None Output parameters: Character in .A Registers used: .A Error messages: Read timeout (in ST) - no clock low response within 0.2 ms of data being released Description: This routine gets a byte from serial bus and returns it in the .A register. It does this as follows:
L865 L866 L872 L868 -
L870 L869 -
Zero COUNT Release clock line Wait for clock to go high Set timer to 256 ms Release data If timer expired go to L868 If clock still high go back to L872 Otherwise go to L870, to read byte If COUNT non zero flag read timeout in ST and exit via a routine to release lines Otherwise assume EOI ; Handshake EOI Set data low Pause and release data Flag EOI in ST Increase COUNT Go back to L866, to wait for clock ; Get a byte Set COUNT for 8 bits Wait for clock to go high Get next bit of byte from data line Wait for clock to go low
50
The Commodore 64 Kernal and Hardware Revealed
Decrease COUNT If COUNT not zero go back to L869 to get next bit Acknowledge byte by sending data low Check EO! flag in ST ; EO! flags end of message If set then delay and release data Exit this byte read in .A LOC EE13 EE13 EI::13 EE13 EE14 EE16 EEHI EEIB EEIE lE20 EE22 EE25 EE27 EE2A EE2D EE30 EE33 EE3:'i EE37 EE:.sA EE3C EE3E EE3E EE.3E EI::40 EE42 EE44 EE47 lE47 EE47 EE47 EE4A EE40 EE4F EE52 EE54 EE56 EI::56 EE56 E.E56 E.E58 E[5A EE50 EE60 EI:.62 I'..E63 EE65 EE67 El6A EE60 lE6F EE70 EE72 EE74 EE16
CODE
LINE
78 A9 00 as A5 20 85 EE 20 A9 EE 10 FB A'! 01
SO 07 DC A9 19 80 0F DC 20 97 EE AD 00 DC AD 00 DC 29 02
;**************************************** ;INPUT A BYTE FRO" SERIAL BUS ;****************************************
L86S
L943 L866
L872
00 07
20 A9 EE. 30 F4 10 18 AS AS
Fe 05 A9 02 4C 82 ED
L8b8
SEI LDA SlA JSR JSR Bf'L Ll>A STA LOA SlA JSR LOA LOA AND e.NE JSR BM!
;OISABLE IRQ ,SET EOI/ERROR fLAG
"$0" COUNT La7S L8S4 L943 1t$01 OITBH
;RELEASE CLOCK LINE ,WAIT FOR CLOCK HI ,SET TIMER B FOR 25b U9
M$19
~·F'L
01CRS L844 OIICR OIICR 1t$02 Lab8 L854 La72 L870
LOA BEQ LOA JMP
COUNT Lab? U02 .L852
,CBECK THE TIMER ,RAN OUT ,CHECK HIE CLOCI( LINE ;NOT YET ,YES ,CHECK FOR ERRUR ,8T"2, REAl> rIME OUT
;
20 A0 EE 20 1:15 EE A9 40 20 lC FE E6 AS 00 CA
,,TIMER RAN OUT,
L867
JSR JSR LOA JSR INC BNE
DO AN EOI
La41 ,DATA LINE LO LI:I75 ;DELAY, SET DATA HI Itt40 $FEIC lOR AN EOI BIT INTO ST COUNT AND AGAIN FOR ERROR CHECK L86b
;
; e.YTE TRANSFER A9 85 AD CO 00 0A 10 66 AD CD 00 0A 30 C6 00 20
01:1 AS
00 DD 00 DO f8
FS AI, 00 DD 00 DO F8 F5 A5 E4 A0 EE
;
La70 L869
LOA SlA LOA CMF' SNE
1t$08 COUNT 020PA 02l>PA L.869
; SET UFo COUNTER
;WAIT FOR CLOCK HI ;l>EBOUNCE
MiL A
LB73
IWL ROR LOA CMF' SNE ASL BMI DEC e.NE JSR
L869 SSOURI 020F'A D2DF'A L873 A
;ROTATE DAlA IN ;WAIT FOR CLOCK LO ;OEe.OUNCE
U!73
COUNT La69 La41
;MORE BITS ;OAlA LO
Serial Communications 51 EE79 t:.E7B EE70 EEB0 EEB2 EEBJ EEB4
24 90 ::;0 03
20 06 I I AS A4 58 18 60
L874
En T STAfUS EWC l874 JSR lBsa lOA BSOUR1 CLI ClC RTS
ICHECK FOR EOI :NONE ;DElAY AND DATA HI ;ENABlE IRQ ,GOOD EXIT
3.3 General lrOutines
All routines change only the .A register. Set clock high, set clock low, set data high & set data low all just set or unset a bit in port A of CIA chip 2. Note that the bit is set to send a line low. Debounce CIA routine first loops until a consistent value is read from port A of the CIA. It then sets the carry flag to the state of the data line, and the sign flag to the state of the clock line. The I millisecond delay is a software delay loop lasting approx I ms. lOC
CODE
,
t:.EB~
EEB5 EE85 EEB5 EE88 EEBA r::E8D r::EaE EEBE EE8E EEBE EE91 EE93 EE96 EE97 EE97 r::EY7 EE97 EE9A EE9C EE9F EEA0 EEA0 EEA0 EEA0 EEAJ EEA5 EEA8 EEA9 EEA9 EEA9 EEA9 EEAC EEAF fEB 1 EEB2 EEB3 EEBJ EEB3 EEB3
lINE
:Sr::T CLOCK lINE HI (INVERTED)
AO 00 [)O 29 EF 80 00 DO 60
, laJ5
lDA D20PA ANO H$EF STA D20PA RTS
;
:SET CLOCK lINE lO (INVERTED) AD 00 OD 09 10 80 00 DO 60
AO 00 00 29 OF aD 00 00 60
I
l843
,;SET DATA lINE HI ,
La44
:
AD 00 00 09 20 aD 00 00 60
lOA 020PA ORA H$10 STA 020l"A RTS
,;SET
l841
LOA 020PA AND OI$DF STA D20PA RTS DATA lINE lO (INVERTED) lOA 020PA ORA H$20 SlA 020l"A
Rrs
; OEP.OUNCE THE F'IA ;
AD 00 DO CO 00 DO D0 FB 0A 60
La54
lOA CMF' BNE ASl RTS
, ;DELAY 1 MS
8A
,
la46
(INVERTE[)
TXA
02DPA 020PA l854 A
52 The Commodore 64 Kernal and Hardware Revealed EEE'.4 EEB6 EEBI EH.9 EEM EH.E'. EEBB
A2 B8 CA 1>0 FD AA 60
L845
.END
LI>X II$BB DEX E'.NE L845 TAX RTS • LIE'. KSER2
GETiN Entry point: $FFE4 Function: Get a character from the current input device Input parameters: None Output parameters: .A holds character, CARRY clear Registers used: .A Error messages: None Description: For serial devices, GETIN is redirected to BASIN. LOC
CODE
LINE *=$F13E:.
EEBE'H3E F13E F13E F13E 1'1310
;
;**************************************** ;* (JEllN -- GET CHARAcn.R FROM CHANNEL. CHANNEL IS DETERMINED BY DFLTN. ;* ;* IF I>EVICE IS 0, KEYEWARD QUEUE IS ;* EXAMINED AND A CHARACTER REMOVED IF ;* AVAILABLE. IF QUEUE IS EMPTY , Z ;* FLAG IS RETURNED SET. DEVICES 1-31 ;* ADVANCE 10 BASIN. THE CHARACTER IS :* RETURNED IN • A. IF ZERO, NULL CHAR • 1****************************************
F13E
Fi3E F13E F13E:: F131:: F13E F13E F13E F140 F142 F144 F146 F147 F14A F14C F14E 1'150 F153 F155 F156
;
A5 00 A5 F0 78 4C C9 00 84 20 A4 18 60
99 08 C6 0F B4 E5 02 18 97 86 1'0 97
BASIN
Entry point: $FFCF
NGETIN LOA E'.NE LOA BEQ SEI JMP L924 CMF' SNE L926 STY JSR LOY L944 CLC RTS
DFLTN L924 NOX L944
;CHECf{ DEVICE ;NOT KEYI!·OARO ,QUEUE INDEX ,NOTHING THERE, EXIT
$E5B4 11$02 L927 XSAV $F0S6 XSAV
,REMOVE A CHAR ;RS-232? ,NO, USE BASIN ,SAVE • Y, USED IN RS-232 ;RESTORE .Y ,0000 RETURN
Serial Communications 53
Function: Get a character from the current input device Input parameters: None Output parameters: .A holds character, CARRY clear Registers used: .A Error messages: None Description: If the status from the last character read was #0 (EOF), the character 13 (carriage return) is returned with CARRY clear. Otherwise one byte is read using the ACPTR routine. LOC F157 F157 F157 Fl57 F157 Fl57 F157 F157 F157 F157 F157 F157 F157 F157 F157 F157 F157 F157 F1::59 FlSB f"1SB F1Sf? Fl5f? F150 F15F F161 Fl63 F166 Fl66 F168 F16A F16C F16!:: F170 F173 F173 Fll5 Fl77 F179 F179 FlAt> FlAO FlAt> FlAt> FIAF FlBI FIB3 FlB4 Fl85 FlB5 FIB8
CUDE
LINE
.
;****************~******~*********~******
;* BASIN-- INPUT CHARACTER FRUM CHANNEL.
;* ;* ;;.; ;* ;* J*
1* ;* ;* ;*
,*
BASIN DIFFERS FRUM GlTIN ON KEYBOARD AND RS-232 ONLY. THE SCREEN EOITOR I'1AI(ES REAOY AN ENTIRE LINE WHICH IS PASSED CHARACTER BY CHARACTER UP TO THE CARRIAGE RETURN. THE CHARACTER IS RETURNED IN .A. ZERO FUR NULL CHAR OTHER DEVICES AREI 0 KEYBOARD 1 CASSETTE 2 --- RS-232 3 --- SCREEN 4-31 --- SERIAL BUS
,* ,****************************************
AS 99 00 08
;
NBASIN LOA DVUN 8NE L927
:CHECK DEVICE ;NOT KEYBOARD
;
,INPUT FROI'I KEY80ARD
A5 85 A5 85 4C
03 CA 06 C9 32 E6
C9 03 09 85 00 A5 05
[927
00
85 C8
4C 32 E6
B0 38 C9 02 Fe 3F
LOA STA LOA SlA JI'IP
f'NTR LSTP Tf?LX LSXP $E632
,SAVE CURRENT: ,CURSOR COLUMN,
Cl'lf" 8NE STA LOA STA JMP
00$03 L928 CRSW LNMX INOX $E632
,SCREEN? :NO ;FAI{E CARRIAGE RETURN ,ENDED: ION THIS LINE ;PICK UP CHARACTERS
;LINE NUMBER ;BLINI{ CURSOR UNTIL RETURN
;
L928
BCS L939 CMP 11$02 BEQ $F188
:OEVICES>3 ;RS-232? ;YES
JI'IP L86S
,GUOO, HANOSHAI(E
641
,,------------------------*=$FIAI> , :INPUT FROM SERIAL BUS , L939 LOA STATUS ;STATUS FROM LAST 8EQ L941 ,o.te. l.932 LOA 11$00 ,BAt>, ALL DONE L946 CLC ;VALID I)AIA L94S RTS
4C 13 EE
L941
.
A5 90 04 A9 00 18
F0
J
,------------------------
54
The Commodore 64 Kernal and Hardware Revealed
BSOUT
Entry point: $FFD2 Function: Output the character stored in .A to the current output device. Input parameters: .A holds character Output parameters: .A holds same character, CARRY clear Registers used: None Error messages: None Description: This routine just jumps to the send buffered character to serial routine.
Loe
CODE
FIBS FICA FICA FlCA FICA FICA fICA FlCA FICA FICA FICA FICA FICA FICe. FICO FIeF FIDI F1D2 FID5 FID5 FI07 F1lJ7 FlO7 FID7 FlO8
FlOB
LINE
, ;******~****~*******************~********
:* BSOUT -- OUTPUT CHAR STORED IN .A TO
i*
;* ;*
;*
;* ;*
48 A5 9A C9 03 D~ 04 68
4C 16 E7 90 04
CHANNEL DETERMINED BY VAHIABLLE DFLTO: 0 INVALID 1 CASSETTE 2 --- RS-232
3 --- SCREEN 4-31 --- SERIAL BUS
J***********************~*********~****** 1
NBSOUT PHA LDA OFLTO CMP "$03 BNE L933 PLA JMP $E716 , L933 £l.cc $F10B
;FRESERVE .A ,CHECK DEVICE ,SCREEN? ,NO ,YES, RESTORE .A ;PRINT TO SCREEN ,DEVICE 1 OR 2
I
,PRINT TO SERIAL BUS
68
4C 00 ED
FlOB FlOI'.
Pl.:A JMP
L861
,.----------------------.LII'. f:SER3
CHKIN
Entry point: $FFC6 Function: Set a previously OPENed file for input. Input parameters: .X holds the logical file number of the OPENed file.
Serial Communications 55 Output parameters: CARRY clear - OK CARR Y set - error, error number in .A Registers used: .A, .X Error messages: File not open - if the logical file number in .X is not in the LFN table Device not present - if bit 7 of ST is set, the device did not respond to the TALK command Description: This routine first checks that the LFN in .X has a reference in the LFN table. If not, the message File not open is sent. The device referenced by the LFN is told to TALK and a secondary address is sent (if present). After sending the TALK secondary address, the device is shifted over to listener. Ifbit 7 of the ST ATUS byte (ST) is set, the message Device not present is sent.
LOC FIOB F20E. F20E F20E F20E F-20E F20E F20E F20E F20E F20E F20E F20E F20E F20E F20E F20E F20E F211 F213 F216 F219 F21B F21D 1'210 F21D F21D F21F F221 F223 F225 F221
F22A F22A F22A F22A F22C F22E F230 F233 F235 F236 F237
COOE
LINE
,
*=$F20E ;*~****~*****************************~***
;* CHKIN -- UPEN CHANNEL FOR INPUT. THE NUMBER OF THE LOGICAL FILE TO ;*
1* 1"-',: UPENED FOR INPUT IS PASSED IN . x. ;* CHKIN SEAf.:CHES THE LOtHCAL FILE TO ;* LOm( UP ()EVICE AND CUMMAND INFO. ;* ERRURS ARE REPORTED IF THE DEVICE WAS ;* NOT OPENED FOR INPUT, (E.G. CASSETTE ;* Wf.:ITE rILE) , OR THE LOGICAL FILE HAS NO REFERENCE IN THE TABLES. DEVICE 0, ~* t I(EY[,.OARD) , AND DEVICE 3 (SCREEN), ;* REQUIRE NO TABLE ENTRIES AND ARE ;~ HANOLED SEPARATELY. 1****************************************
,*
20 F0 4C 20 A5 F0
0F F3 03 01 F7 IF F3 e.A
16
I
NCHlaN JSR BEQ JMP L950 JSR LOA BEQ
L1000 L950 L1009 L1002 FA L963
,FILE OPENED? ;YES ;NU, FILE NOT OPEN ; GET FILE INFO ;KEYBUARD
;
,COULD BE SCREEN, RS-232, UR SERIAL C9 03 Fe 12 ['.0 14 C9 02 00 03 4C 40 Fe
CMP BEQ Bes CMP [,.NE JMP
tI$03 L963 L961 11$02 L958 $F04D
;SCREEN? ;YES, OUNE ,SERIAL ,RS-232? ,NO, MUST BE TAPE ;RS-232
; CHECI( FOR INPUT FILE ON TAPE ,
E'.9 60 F0 03 4C 0A F7 85 99
A6 1::0
18
60
L958
L963
LOX CPX BEQ JMP STA CLC RTS
; CHECI( SECONDARY AD SA 11$60 ; INPUT? L963 ;YES L9/1 ,NO, NOT INPUT FILE OF-LTN ; SET INPUT :GOUO RETURN
The Commodore 64 Kernal and Hardware Revealed
56
COI)E
LOC F231 F237 F231 F238 F2:.m F23D F23F F242 F245 F248 F249
LINE ;A SERIAL DEVICE MUST TAU(
,
L.961
AA
20 09 A5 \?9 H) 06 20 CC 4C 48 20 C7 8A 24 90
EO ED F2 ED
L962 L967
t:6
F24B
10
F24D
4C 07 F7
TAX JSR LOA !'.PL JSR JMP JSR TXA Bil
L836 SA L962 L970 L967 L860 51
AlUS
BPI- 1-903
JMf' L1026
;SAVE DEVICE II ;TALK ;SECOND? ;YES, SEND IT ;NO, LET GO ; SEND TAU( SA ;OID IT
LISTEN?
;YES
,DEVICE NOT PRESENT
CHKOUT
Entry point: $FFC9 Function: Set a previously OPENed file for output. Input parameters:
.x holds the logical file number of the OPENed file.
Output parameters: CARR Y clear - OK CARR Y set - error, error number in .A Registers used: .A, .X Error messages: File not open - if the logical file number in .X is not in the LFN table Device not present - if bit 7 of ST is set, the device did not respond to the LISTEN command Description: This routine first checks that the LFN in .X has a reference in the LFN table. If not, the message File not open is sent. The device referenced by the LFN is told to LISTEN and a secondary address is sent (if present). Ifbit 7 of the STATUS byte (ST) is set, the message Device not present is sent.
LOC
CODE
LINE
F250 F250 F2::i0
;**************************************** ;_ CHKOUT -- OPEN CHANNEL FOR OUTPUT. ;_ THE NUMBER OF THE LOGICAL FILE TU ;* BE OPENED FOR OUTPUT IS PASSED IN .X. ;_ CHKUUT SEARCHES THE LOGICAL FILE TO ;* LOOK UP DEVICE AND COMMAND INFO. ERRORS ARE REPORTED IF THE DEVICE WAS ;* NOT OPENED FOR INPUT, (E.G. KEYBOARD) ;* OR THE LOGICAL FILE HAS NO REFERENCE ;* IN THE TABLES. DEVICE 3 (SCREEN) ; _ RHlllIRES NO TABLE ENTRY AND IS ;- HANDLED SEPARATELY.
F250
;*********~**********~*******************
F250 F250
F250 F250
F250 F2::i0 F250 F250 F250
,*
Serial Communications 57 LUC
CUl>E
F250 F250 F253 F255 F258 F25B Fl::iO F25F F262 F262 F262 F262 F262 F264 F266 F268 F26A F26C F26F F26F F26F F26F F271 F273 F275 F2"17 F278 F279 F279 F279 F279 F27A F27D F27F F281 F284 F286 F289 F28A F28C F2BE F291 F291
20 Fe 4C 20 A5 00 4C
0F 03
LINE
"-3
01 F7
1F F3 BA 93 eo F7
;
N(;KOUT JSR SEQ Jl'lf' L969 JSR LOA BNE L972 JMF'
,
Lue0 L969 L1009 L1002 FA L979 L965
,FILE IN TABLE; ,YES ;1'10, FILE NOT OPEN ;!JET TABLE INFO ,NOT KEYI!.OARD ,KEYSOARO, NOT OUTF'UT FILE
,COULD BE SCREEN, SERIAL, , CASSETTE, OR RS-232
C9 F0 B0 C9 00 4C
03 0F
,
L979
CI'IP f,Eg, BCS CriP BNE JrlP
11
02 e3 El EF I
IC~JECK
A6 E0 F0 85 18 60
B9 60 EA 9A
I L973
,SCREEN7 ,YES, DONE ;1'10, SERIAL ,RS-2327 ;1'10, MUST BE CASSETTE I SET UF' FOR RS-232
FOR CASSETTE FILE TYf'E l.DX CPX BEg SlA CLC RTS
L977
11$03 L977 L975 11$02 L973 tEFEl
SA JI$6e L972 DFL10
; INPUT FILE7 ;YES, ERROR ;SET OUTPUT ;0000 RETURN
;
;SERIAL DEVICES ;
L975
AA
20 AS 10 20 00 20 8A 24 10 4C
0C ED 1?,9 05 I?,E ED 03 B9 ED
TAX JSR LOA BPL JSR BNE JSR TXA BIT Bf'L JMF'
L976 L981
90 E7 07 F7
L966 SA L916 L983 L981 L871 STATUS L977 L1026
;SAVE DEVICE It ;LISTEN ;AND SECOND? ;YES ;1'10, RELEASE LINES ;ALWAYS ;SEND LISTEN SA DID IT LISTEN'? YES, FINISH NO, DEVICE NOT PRESENT
.END .LIB KSER4
CLOSE
Entry point: $FFC3 Function: Close a logical file. Input parameters: .A holds the logical file number to close. Output parameters: CARRY clear. Registers used: .A, .Y,
.x
Error messages: None. Description: The LFN table is checked for the file to be closed. If the file is not open the routine exits, otherwise the device is told to listen and then unlisten and the file entry is removed from the table.
58
The Commodore 64 Kernal and Hardware Revealed CUOE LINE
LUC
F291 F291 F291 F291 F291 F291 F2'?1 F291 F291 F291 F291 F291 F291 F291 F291 F291 F294 F296 F297 F29B F29B F29C F290 F29F F2Al F2A3 F2A5 F2A7 F2A9 F2AB F2AB F2EE F2EE F2EE F~~F
, ;**************************************** ;* CLOSE -- CLOSE LOGICAL FILE. ;* THE LOGICAL FILE NUMBER OF THE ;* FILE TO BE CLOSED IS PASSEO IN .A. ;* KEYBOARD, SCREEN, AND FILES NOT OPEN ;* PASS STRAIGHT THROUGH. TAPE FILES ;;; OPEN FUR WRITE ARE CLOSED BY DUMPHIG J. THE LAST BUFFER AND CONDITIONALLY ;;; WR IT I NG AN END OF TAFE I?,LOCK. SER I AL ;* FILES ARE CLOSED BY SENDING A CLOSE ;* FILE COMMAND IF A SECONDARY ADDRESS ;* WAS SPECIFIED IN ITS OPEN COMMAND. 20 14 F3 F0 02 18 6~)
20 8A 48 A5 F0 C9 F0
IF n
BA 50 03 4C B0 47 C9 02 00 10
CMP I?HI BCS CMF' SNE
F~;0A
;LOOl{ UP FILE ;FOUND ;ELSE RETUR~I
L1002
;GET FILE DATA ,SAVE TABLE INDEX
FA L987 11$03 L987 L997 H$02 '&F2C8
;CHECK DEVICE II ;I(EYI?oOARD, DONE ;SCREEN? ,YES, DONE ,SERIAL ,RS-232;' ; NO, MUST (,!,E TAPE
·,--------------------------
20 42 f6
L991
68
, L987 L986
1
F30D F30E F30F F30F F30F F311 F313 F3I4 F316 F317 F319 F3IC F31E F31F F31F F31F F31F F322
L957 L982
.=$F2EE ;CLOSE A SERIAL FILE
F2Fl F2~
NCLOSE JSR BEQ CLC RTS L982 JSR TXA F'UA LOA ~,Etl
1
F2Fl 1'"2F2 F2F3 F2F:5 F2F7 F2F9 F2FB F2FE F301 F304 F307
....................................... .
; ;
AA
C6 E4 F0 A4 B9 90 IW
90 B9 90 18 60
98 98 14 98 59 59 63 63 60 60
02 02 02 02 02 02
F'LA TAX DEC CPX BHI LOY LOA SIA LOA SlA LDA Sf A L989 CLC RTS ;FIND FILE ,
A9 85 8A A6 CA 30 DO 00 60
00 90
L1000
98
L9:57 L984
15 59 02 F8
I?,D 59 02 85 B8
JSR LIeS1
·,;REMOVE FILE ENTRIES FROM TABLES
LOA SIA TXA LOX DEX BMI CMF' BNE RTS
;GET TABLE INDEX LDTND LDTND L989 LDTND LAT,Y LAT,X FAT,Y FAT,X SAT,Y SAT,X
;6000 EXIT ENTRY 11$00 STATUS LDTND L960 LAT,X L984
·,;FETCH TABLE ENTRIES ;
L1002
,IS IT AT END? ;YES, DONE ;NO, SHIFT LAST ENTRY ,INTO DELETED ENTRY'S ,POSITION
LOA LAT,X SIA LA
Serial Communications 59 LOC
CODE
F324 F327 F329 F32C F32E F32F F32F
P..D 63 02 85 SA SO 60 02 B5 S9 60
LINE LOA SlA LOA SlA RTS
L960 .END
FAT,X FA SAT,X SA
.LIB I{SER5
ClAll Entry point: $FFE7 Function: Close all logical files. Input parameters: None Output parameters: None Registers used: .A, .X Error messages: None Description: The number of files open is zeroed and the CLRCH routine is entered. LOC
CODE
F32F F32f F32F F32F F32F F32F F32F F32F F32F F331
LINE J
;***~~********~***********************~*~ J~
;*
;*
:*
A9 00 85 98
CLAlL -- CLOSE ALL LOGICAL FILES. DELETES ALL TABLE ENTRIES AND RESTORES DEFAULT 1/0 CHANNELS AND CLEARS SERIAL pora DEVICES
;**************************************** .NClAlL LOA N$00 STA LDTND
,FORGET ALL FILES
ClRCH Entry point: $FFCC Function: Abort any serial 110 files and reset default 1/0. Input parameters: None Output parameters: None Registers used: .A, .X Error messages: None
The Commodore 64 Kernal and Hardware Revealed
60
Description: The output device is checked and if it is serial, the command UNLISTEN is sent to it. The input device is then checked and ifthat is serial the command UNT ALK is sent to it. The input device is then set to 0 (keyboard) and the output device is set to 3 (screen).
CODE
LOC F333 F333 F333 F333 F333 F333 F333 F333 F333 F335 F337 F339 F33C F33E F340 F343 FJ45 F347 F349 F34A F34A
LINE ;
;**************************************** ;* CLRCH -- CLEAR CHANNELS. ,IE
UNLIsrEN OR UNTALK SERlAL DEVICES, 8l1T LEAVE OTHERS ALONE. ;* DEFAULT CHANNELS ARE RESTORED.
,*
:**************************************** A2 E4 B0 20 1::4 1?0 20 96 A9 95 60
03 9A 03
FE ED 99 03 EF ED 9A 00 99
;
NCLRCH LDX 11$03 CPX DFLTO 8CS Ut01 JSR U006 L1001 CPX DFLTN I?CS U603 JSR L963 L1003 STX DFLTO LOA ..... STA DFUN RTS .END .LI8 I=4 TO 31 AS SELECTED BY ;* VAf.:IABLE FA. ;* START OF SAVE IS INDIRECT AT .A ;* END OF SAVE IS .X, .Y ;* USE SETLFS & SETNAA BEFORE THIS ROUTINE J*****~*~*********~~************~*******~
86 AE 84 AF
L1072
AA
B3 85 B5 85
be
00 Cl 01 C2 32 03
A5 00 4C C9 F0 90 A9 8::i A4 00 4C
SA 03 13 03 F9 5F 61 89 1'.7 03 10 20 05 20 8F
NSAVE F7
F7 F3 F6
L1242 L1075
L1074
A5 I:.\A
20 A5 20 A0 20 A::i 20 AS 20 20 B0 1'.1 20 20 D0 20 A9
0C E.D B9 B9 ED 00 8E FB AC DD ED
AD DO ED 01 FC 16 AC 00 EO U
L1077
FF
07 42 Fb 00
L1084
20 DB FC 00 E5
L1054
38
60 I
STX EAL STY EAH TAX LOA $00,X STA STAL LOA $01,X STA STAH JMP (ISAVE) LoA FA E'.NE L1075 JMP L1049 CMP U03 BEtI L1242 BeC $F659 LOA "$61 SlA SA LOY FNLEN BNE L1074 JMP L974 JSR L1021 JSR 1.1087 LOA FA JSR L966 LOA SA JSR L871 LOY "$00 JSR $FE'.BE LIlA SAL JSR LB61 LIlA SAH JSR L861 JSR $FC01 BCS L1082 Ll'A (SAL), Y JSR LB61 JSR $FFEl E.'.NE L1054 JSR L10Bl l.DA "$00 StC RTS JSR $FCDB BNE L1077
,SET UF' START
;~~AD DEVICE ,SERIAL? ,SCREEN, BAD DEVICE ;NO, 1 APE ;YES
;MISSING FILE NAME ;OPEN ; 'SAVING' ;LISTEN ;LISTEN SA
;COMPARE START TO ENO ;HAVE REACHED END ;STOP KEY? ;NU ;YES, CLOSE
;INCREMENT CURRENT ADDR
Serial Communications 67 LUC
CODE
LINE
F 6~~F F642 F644 F6'.6 F648
20 24 30 A::i 20 A5 29 09
L10H2 L1081
~64B
F64D F64F F651 F654 F65? F658 F659 F65''> F68E F68F ~ 68F F68F F68F F691 F693 F695 F698 F69B F69B
FE E.D B9 11 BA 6[; ED
B9 EF 1'::0 20 B9 EO 20 FE ED 18 60
L999 L1034
JSR BIT BMI LDA JSR LOA AHD ORA JSR JSR CLC R1S
L1006 SA L1034 FA L966 SA ft$EF II$E0 L8?1 L1006
; UNLISTEN
;LISTEN
;LISTEN SA fUNLISTEN ;(3000 EXIT
;-----------------_._--------*'=$~68E
60
AS 90 10 FE'. A0 51 20 2F F1
4C C1 F5
L1090 kTS , ;F'RINT 'SAVING [FILE NAME) , , L108? LOA MSGFLG ;PRINT II? BPL L1090 ;NO LOY 11$51 ; 'SAVIN'" JSR $F12F JMP L1022 ,SEND FILENAME .END .LIB I(SER9
Error handler
Entry point: $F6FB - (I) $F6FE - (2) $F701 - (3) $F704 - (4) $F707 - (5) $F70A - (6) $F70D - (7) $F710 - (8) $F713 - (9)
too many files file opeh file not open file not found device not present not input file not output file missing filename bad device
Function: To flag an error and print it if output is enabled, Input parameters: None Output parameters: CARRY set, error number in ,A Registers used: ,A,
.x,
,Y
Error message: 110 error #(number) - if bit 6 of MSG FLG is set Description: At each entry point, the ,A register is loaded with the value in brackets, The routine CLRCH is called and ifbit 6 ofMSGFLG (output enable) is clear, CARR Y is set and ,A holds the error number upon exit. If bit 6 is set, the message '110 error #' is printed to the screen and the number is converted to ASCII and printed, CARRY is set and ,A is reloaded with the error number.
68
The Commodore 64 Kernal and Hardware Revealed
LOC F69B fbFB F6FB F6FB F6FB F6F-B F6FB F6FFo F6FB F6FO F6FE Fl00 F-701 F703 1'704 F706 F/0l F709 F70A F70C F 100
F/0F Fl10 Fl12 F713 F71::; F7I6 F7I9 FllB FllO FlIF F722 Fn3 F724 F126 FJ29 Fl2A Fi'2B Fnc
CODE
LINE !!=$F6FB
;****************************************
;. ERROR HANDLER. ;* PRINTS KERNAL ERROR MESSAUE IF FoIT 6 ;. OF MSUFLO IS SET. RETURNS WITH ERROR IN .A AND CARRY SET.
,* "
I··············!!!!···!!·!!!!.!!!! •. !!...!! .•. !! ..•
AY 2C AY 2C A9 2C A9 2C A9 2C A9 2C A9 2C A9 2C A9 48 20 A0 24
I
01
L1097
02
L1011
03
L1 00Y
04
LYSY
0S
L1 026
06
L971
07
L965
08
L974
0Y
L104Y
CC FF 00 90 ~0 0A 20 2F Fl 68 48 09 30 20 02 FF 68 38 60
L1018
LOA 1tt01 .eYT t2C LOA IIt02 .en $2C LOA 11.03 .I?YT t2C LOA 11.04 .BYT $2C LOA ".0S .I?YT $2C LOA 11$06 .I?YT $2C LOA 11.07 .I?·n t2C LOA 11.08 .BYT $2C LOA 1I$0Y F'HA JSR tFFce LOY "''00 1:'.1 T MSUFLG BVC L1018 JSR $F12F F'LA F'HA ORA 11$30 JSR tFF02 F'LA SEC RTS
;f 00
MANY FILES
,FILE OF-'EN ,fILE
~IOT
OPEN
,FILE NOT FOUND ;UEVICE NOT PRESENT ;N(H INPUT FILE ,NOT OUTPUT FILE ,MISSIN~
FILE NAME
,BAD DEVICE II ,ERROR II ON STACK ,RESTORE I/O ,F'F 0003 009F 00AB 0298 00F9 00P'.0 00e7
0260 00A7 0010 0091 009F 00E.'.1 009F 0093
70
The Commodore 64 Kernal and Hardware Revealed
3.4 RS232 serial communications
The CBM 64 is able to communicate with peripheral devices, known as an RS232 110 port. The name RS232 simply refers to an industry standard form of serial communication for computing devices. A serial 110 port can consist of as few as three lines, an output or transmit line, an input or receive line and a common ground line. The data is transmitted or received as a stream of pulses; a single byte becomes a string of eight pulses. Although a serial port can have just three lines, other lines are frequently used to transfer control information. The 64 is able to receive and generate such control signals to implement a full 'X line' interface as well as the simple '3 line' interface. Whichever implementation is used all the lines are connected to 1/0 port B ofCIA#2 (user port). The RS232 routines inside the 64 also use two other lines; P A2 on port A and FLAG which is connected to the NMI line. Normally an RS232 interface card will be used to connect between the user port and a standard RS232 connector. The card will also provide buffering and a higher drive voltage. For communications using the simple 3 line mode an interface card can easily be constructed using a couple of buffer I driver I Cs. The RS232 line normally transmits data using a 12 volt signal, however, and providing cables are kept short it will work with a 5 volt signal. The standard RS232 connector is shown in Fig. 3.4. The function and pin assignment of each of these lines is as follows:
CIA line #
RS232 CIA pin # pin #
Abv
'EIA
GND FLAG PB.0 PBI PB2 PB3 PB4 PB5 PB6 PB7 PA2 GND
A I B 3 C 3 4 D E 2.0 F 18 H 8 Not assigned K 5 L 6 2 M 7 N
GND SIN SIN RTS DTR RI DCD
AA BB BB CA CD CE CF
CTS DSR SOUT GND
CB CC BA AB
In/ Out
In In Out Out In In In In Out
Modes
Function
1,2 1,2 1,2 2 2 3 2
Protective ground Received data Connected to FLAG Request to send Data terminal ready Ring indicator Received line signal
2 2 1,2 2,3
Clear to send Data set ready Transmitted data Signal ground
Modes: I Three line interface (note RTS and DTR are both held high during this mode) 2 X line interface 3 User only, not implemented in the CBM 64 code
Serial Communications 2
3
4
5
6
7
8
9
71
19 11 12
••••••••••••
. -_ ........ . ABC PIN
0
E F H J
TYPE
K L M N
RS232 FUNCTION
A B C 0
E F H
FLAG POO PSI PB2 PB3 PB4
1
Sin
-
RTS OTR
-
RI
-
OCO CTS
I
i
i I
J PB6 PB7
-
L
-
OSR
M
PA2
N
GND
-
Sout GNO
K
, Y INC T1
82
F7B5 F7Bi' F7fM
;START ADDRESS IN HEADER
STAH
F79E
91 B2
;BLOCK TYPE IN HEADER
F943 F94:1 F948 F94B F94E F94F 1'951 F953 F'I54 F956 1'957 F959 1'95B F95C F95E
TAPE READ lRQ ROUTINI:. AE 07 DC A0 FF 98 EO ~)6 DC E.C 07 DC D0 F2 86 ~1 AA Be 06 DC 8C '17 DC A9 19 80 '>F DC AD 0D DC 80 1-\3 ,98 £3 B1 86 Bl 4A 66 Bl ~')
4P1
66 B1 145 B0 18 69 3C C5 !H
1"966
4A A6 9C F0 ~..5 J,C 60 I'A
F969 F969 F96B 1'960 F96F f-971
A3 :H> lB A2 00 69 30 65 ~,0
F97;~
t:5 £'.1
f97:) F977 F978 F97H F97C F97E 1'"980 F98:2 F984
B0 lC
F96~
1'962 F?64
F98~
1'"988 F98B F98P..
F980 1'"98F F991 1'"993 F993 F993 1'"997 F997 F'I99 F99A F99C F99E F91-10 F9A2 F9A4
1130
f%)
A6
, L1132
E8
69 26 ,~3
B~
C5 £'.0 69 63 C5 90 4C
B1 17 2C P.ol'>
B1 ~3
113 FH
A5 !!,4 F0 10 85 A8 D0 19 E6 A9
L1134 L1136
, L1139
e,\') 02
C6 38 E9 E5 65
A9 13 Bl
92 85 '>'2 A5 A4 It
9 '.II
L1137 LllJ8
LDX Dl1BH LO·t It~FF TYA SSC D1TSL CPX D1TBH SHE L1130 STX TEMF' TAX STY DITBL STY 011BH LDA 1t$19 STp, 01CRS LDA OllCR STA $021-13 TYA SBC TEI~P STX TEMP LSR A ROR TEf1P LSR A ROR TEMF' LOA CMF'O CLC AOC 1I$3C CMP TEMP BCS Li1"1 LOX DPSW SEQ L1132 JI'1P L1154 LOX Bl'll LD>: AOC AOC eMP BCS INX ADC ADC CMP BCS AOC AOC CMP BCC JPil" LOA BEQ STA £'.NE
PCNrR Li134 10$00 11$30
CMPO fEI1P L1139 ~$26
CI1PO TEf1P L1137 ~!!-2C
CMF'O THIP Li136 L11't5
SNSW1 Lll"1 RER Lit41
;GET TlI'lE SlNCE LAST IR~I ;COI1PUTE COlJNJ"ER 01FF ; TIf'lER HIGH ROLLOVER? ;YES, RECOi1PU rE ;RE-LOAO Tll11::R B
;CALCULArE HIGH ;1'I0 1)E 2 BITS f'ROM ; HlGH TO TENP ;CALC ilIN PULSE I)ALLJE ;PULSE lI:.SS THAN 111N? ;YES, NOISE ;NO, LAST BIT? ;NO, CONrINUE ;YES, rlNISH BYTE ;9 BITS kEAD? ;YES, Goro ENOING ;SET !.{IT VAL TO Z£RO ;ADD UP TO HHL.F WH"I BE TI~EI::N : SHIRl PULSE ANI) SYNC FULSE ;SHORT? ;YE5 ~SET SIr VAL TO 1 ;MOVE TO IUDOLE 01' HIGH ;1?
:YES ;MOVE TO LONGLONG ;LOHGLCJNG? ;6REATER THAN, ERROR ;YES
;Nor SYNCRONISED?
ERROR ;YE5, I'" LAG RE.R ;ALWA'(S
;1'10,
lNC REZ Bes Ll139
;COUIH REZ UP 01-1 ZEROS ;ALWAYS
DEC REZ SEC SSC "$13 SBC TEMP ADC svxr STA SlJxr LOA rIRT EOR ,*$~H
;COUNl RE,Z OOWN ON ONE.5 ;CALC ACTUAL VI-IL FOR COMPARE SUBJ"RACT INPUT VAL ADD OIFF TO TEi'W STORt::: USED TO AOJUST SOFT SER')O FLlP DIF'OU:: FLAG
102 LOC F9Af> F91-l8 F9HA F9AC F9AC F9AE F9P.o0 F9B3 F9B5 F9B7 F9B~1
F9BC F9BE F9C0 F9C3 F9C3 F9C7 F9C9 F9C9 F9Ce. F9CE. F909 F9l)2 F9D5 F9D5 F9D7 1'·9D9 F9DB 1'900 F9DE F9E.El F9E2 r9E4 F9E6 F9E8 F9E9 F9EB F9EO F9EI' F9F1 F9F3 F9F:5 F'9F7 F9F'l F9F8 F9FA F9FC F9FE FA00 FA02 FI-lf)" FA06 FA08 FA0A FA~D
FA10 FA10 FA1e FA10 FA12 FA14 FA16 FA18 FA1H FA1C FA1F I'AIF
The Commodore 64 Kernal and Hardware Revealed CODE LlNE 85 A4 F0 '2B 86 l)7 A5 Ff> AD 29 l)0 AD 00 A9 8::)
L1141
f)1 ~5
1'14 02 16
00 1'14
80 A4 A5 A3 H) 30
,
P..4 22 A3 02
Ll133
w2
3~
BF
STA FlkT BEQ 11143 STX l)A1A
L1144
92 Fe 07 30 103 C6 e.0 2C E6 B0 A9 00 85 9-' 1::4 l)7 D0 0F 8A DG A0 A5 AY .H) BD C9 10 90 B9 85 96 Be BS
L1143
L11~0
;NO BYTE START? ;YES, RETURN ; THlER A IRQ'D?
LOA BEQ lOA AND SHE LOA SHE LOA STA STA LOA BPL 8MI
H$0~
;NO, EXlT ;SEf DIPOLE FLAG FOR FIRST HAl.F
FIRT $02A4 PCHTR 11148 L1134
; ;
LLlX JSR LDA BNE JAP
#$A6 L1126 I"RTY L1136 $FEBC
, 1'12 1'16 :C'i! E2 F8 A5 9e. De B9 4C P..C FE
SNSW1 11150 $02A3 #$91 L1133 -'92A4 L1150
;SECOND HALF OF DIPOLE ;FIRSl HALF SO SlORE ~IAL
;YES
;WHERE rN BYTE STILL DOING DATA PROCESS PAR rry
;SETUP FOR LONGLONG ;EVEN PARITY? .:HO, SET ERROR ;RESTORE REGS ANI> RTl
: AS
..
8A 45 9B 85 'ill:? H5 B4 1'0 02 C6 H3 30 CS 46 07 66 BF A2 DA
L1142 L1149
,
L1!48
20 E" "'- 1'8
4C BC FE
,
LOA SV>:T BEQ L1149 BMI L1142 DEC CI'IPO .BYT $2C INC CMF'O LDA 1t~60 STA SlJxr CPX DATA BNE L1148 TXA BHE L1136 LDA REZ BIH L114! CNP 1t$10 BCC L114! STA SYNO BCS L1141 TXA EOR STA LOA BEQ DEC BPll LSR ROR LDX JSR JI'IP
PRTY
;ADJUST SOFT SERVO? ;1'40
;YES, MORE. BASE n"'l£ ;,(ES, LESS BASE r II~E ;SKIP NEXl ;CLEAR DlFF FLAG ;CONSEC. Llf:E VALS IN l)lPOLE,' ;NO, PROCESS INFu ;YES, CHECK VALS ; ONES, ERROR ;HOW PlANY ZEROS? ; TOO I'IAW! ; 16? ;NO, CONTrNUE ; )'ES, FLAG SYNO ;~)LWA,(S
;MOVE READ l)ATA TO .A ;CALC PARITY
PRT'(
SNSW1 L11::'0 PCNrR L1144 DATA I'tYCH U$DA L1126 $FEBC
;REfo)L DATA? ;1'10, FORGET :DEC BIT COUNT ; NI::G , TII1I:: FOR r'ARIl Y ;SHIFT BIT FROf1 OATH , INTO BYTE. STOkE ;SE fliP FOR NEXT DIPOLE ;RESTORE REGS AND RTI
;LONGLOND HANDLER
AS 96 F0 04
,
L114!:!
L1140
LDA BEQ LOA SEQ LOA
SYNO L1149 SNSWl L1151 PCNTR BM! L11::)! JMP L113'l
, L1151
;ENI> 01: BYrE? ;YES ;1010, TREAT AS LONG
LSR TEMP
;ADJUST TIME OUT FOk
AS ~."
FYi 07 A5 A3 30 03
/tC 97 F9 46 B1
;GOT BLOCK SYNC? ;1010
;HAD REAL BYrE? ;1'10
The Cassette Units 103 LOC FA21 ~A23
FA2'1 FA26 FA28 FA29 FA2A FA20 FA2F FA31 FA33 FA35 FA37 FA39 FAJB FA30 FA3F FA42 FA ..... FA44 FA46 FA48 FA4A FA4C FA4E FAS0 FA53 FA55 FA57 FA59 FASB FA5D FA6f) FA60 FA63 FA65 FA67 fA6A FA6C FA6E FA70 FA70 FA70 FA70
CODE
UNE
A9 93 38 £5 B1 65 B0 0A
AA 29 1::6 HS 00 AS F0 85 A9
E2 F8 9C B4 11 96
26 Ae 00 8S 96 A9 81 80 00 DC 85
AS 85 1'0 A9 85 A9 80 AS
~.4
96 69 ~.4
01 00 DC
L1153
85 ~.I) 1'13 A8
20 97 FB 85 9C A2 OA 20 E2 F8 AS BE. F0 0'·/,85 1-17
LOA STA BEQ LOA STA LOA STA LOA STA l.OA ORA STA JI'IP
SYNO OLFF L1153 11$00 SNSWl Q$01 OlleR i'lYCH OCHAR RER REZ PRP $FEBC
J5R STA LOX JSR LOA BEQ 5TA
L1079 OPSW II$OA L1126 FSI?LK L1135 SHCNL
TEMP CI'IPO A ;SET TIPiE OUT FOR LAST BIT
;sn BIT THkOW AWAY I'LAG ;BYTE SYNCRONISEO? ;YES. S~:lP TO PASS CHAR ;rHROW our DATA UNTIL SYNC ;NO SYNC ;FLAG DATA AS ERROR ;KILL 16 SYNC FLAG ; SETUP FOR THIER B IRQ ;FLAG WI:: HAVE H'I'TI: SYNC
L115S , Lll::'4
.
;SAVE SYNO S·I ATUS ;NO BLOCt( SYNC ;TURN OFF BYTE S'(Ne SWlTCH :OISABLE TIPlER B IRQ ;PASS CHAR TO BYTE RourINE ~COI'l8INE
ERROR '}ALS
AND SAVE IN PRP ;6ET LAST BYTE
;
;FINlSH ~.nE. CLI< FLAGS :8E.T BIT THROW AWA'( FLAG : HI! r FO!': NEXT 01POLE ;CHEC.: FOk LAST VAL
;****~*****************~*********~*******
;*' BYTE HANDLER OF CASSETrE READ. RF..R IS SET IF THE BYTE IS IN ;*
E.RROR. REZ tS SH IF rHE INTERRUPT PROGRAM IS REAOrNG ZEROS. ROFLG TELLS IJS WHAT \·IE ARE DOING. BIT 7 SAYS TO IGNORE. BYTES UNTiL. REZ IS SET, gIT 6 SAYS ro LOAD THE BnE. OTHERWISE ;* ROFLG IS A cou"n DOWN AFTE.R SYNC. IF ;* VERCK IS SE.T WE DO A COMPARE INSTEAD ;* OF A STOIA LSR LDA BCC LDA LOX STA STX LDA LDA STA LDA EOR 5TA
OCHAR A
11$00
U184 1J$f!.0 Ii"
D1TBL DlTBH DllCR #'$19 DieRS $91 *1>08 $91
;BIT TO WRIlE HITD CARRY ;ASSUME CARRY CLEAR (SHORT) ;CORRECf ;SET LONG ;SET AND STORE f Ii'IE ;LO BYTE ;H.£ BY fE ; CLEAR IRQ ;FORCE LOAf) & START TII1ER ; TOGGLE IORnE B£T
The~ Cassette
LOC Fe,cs FBC7 FBC8 FBC9 FBCB FP..CD FBCO FBCD FBCIJ Fe,CD FBCF FE',Dl FBD3 Fe,l)!:) FBD8 Fe,OA FP..DC FBDE FP.HJ
LL1DE
LINE
29 08
Arm
38
L1l81 p",~
30 3C
, WRTN
A5 A8 00 12 AS'
1\~
A:2 0l 20 1:',1 H;
D0
~~F
E6 AS H3 P..~ 10 29 4C :17
Fe
Ft',E:5 FP,E7
AS A9 D0 09 20 AO FB
F[~EA
D0 10
FBEC HH,:E FP,F0 r iJ,F0 FBF3 FE',FS FBF7 FBF9 FBF[', FBFD FP,FF FC01 FC03 FC'05 FC0l FC~9
~C0E.
FC10 FC12 FC14 FC16 FC16 FC19 FC1A FCIC FCIE FC29 FC22 FC24 FC26 FC28 FC2A FC2C FC21:. FC.30 FC33 FCJ5 FC37 FCJ9 I'C31:.'. FC;)D
, L1l9l
£6 A9 D0
19
,
2'0 A6 FB O~ 14 AS A4 ',9 01 85 A4 F0 0F A5 P..D 1.9 01 85 BD 2':( 01 45 9B 85 9B 4C Be FE
FC0C FC0C
SEC ROR PRP BMI L1183
;LEAVE JUST WRITE e,n ;FLAG PRP FOR END OF BLOU; ;ALl~AYS
; TAPE t.RI TE J.RQ ENTRY
FBE3 FP,E3
1t~08
Rrs
61:1
6·'"
Units 107
U180
L11B3
LOA BNE LJ)A l.DX JSR BNE INC LDA BPL JI'IP
L1178 Ll183 RER PRP L1l83 U194
;END UF (?,LOCK? ;NO, CONTINUE :'(£S, FINISH OFF
LDA BNE JSR BillE tNC BNE
!'fEZ
;CHECK FOR A ONE e,lT
L11S0 L118::; L1183 REZ Ll183
L1179
H> 10'3
20 58 A5 FfJ 142 86 C6
Q' , I
E.0 00 09 85 D0
02
FB
, L1186
AS 12 00
D7 PI!)
A6 e,E 9'" 78 Ll184 Ul8? SHCNL UlBi' Ll'179 SHCNH LllB7 Ii$M
;WRITING LEADING ZEROS , FOR SYNC DONE NO YES, DONE NO YES,
WITH
LOt~
SYNC?
CLEAN UP COUN'fERS WiTH SYNC,' VECTOR FOR DATA
L1l95 SHCNH FSBLh U1 '18
ZERO SHCNH DONE? ,(ES, S'(STEM RESTORE
The Cassette Units UJC
CUDE
~C8D
20 BE Fg A2 0'~ 86 1015
FC8F
86 86
~G91
D0 83
FC8S FC8B
FC93 FC93 FC94 FC95 FC98 FC9A FC9D FCA0 FCA2 FCA5 FCA8 r CAE'. FCAO FCB0 FC3J FCB6 FCBi' FCB8 FCB8 FC3B FCBD FeBD regD FCE'-D FCBD FCBD i>CBD FCBO FCBO
BD 93
FCC0
80 14 03
FCC3 FCC.,
r,D 9 /,
I'D
80 1:5
~)3
~CC9
60
~'CCA
FeCC FCCE FCD0 FCDl FCD1
08 78
AD 11 1)0 09 10 81) 11 1)0 20 CA FC A9 7F 8D 00 DC 20 DO FD HD A0 02 Fi3 09 80 15 03 AD 91' 02 HO 14
FE SF D7 02 D6
C8
STA INX BNE LDA STA LDA STA elI RTS
00
, CNTROL INY LDA BMI CMP BNE INY LDA STA ENE CONTR1 CMP BNE INY LDA STA INY
B1 FE 85 42 C8 B1 FB 85 47 C8 B1 FB 85 4A C8 B1 FB 85 4D D0 BD
86 51 84 513 Ae 00 A5 5D 85 4E A9 7F 8D 00 AD e1 C9 7F DO 06 68 68 68 a8Bl "4C 58 (l8B4 08B4 B1 50 08B6 Fe 3E 0BB8 C9 01 0BBA FO 2B OSBC 855A 08BE E6 513 0eC13 De 132 0SC2 E6 51 USC4 B1 513 08C6 AA e8C7 BD 131 08CA 91 4E 08CC E6 4E 0eCE BD 013 08D1 91 4E 08D3 E6 513 08DS De 132
$eeOO,X
;
RESTRE #$00 $D418 #$1B $D011
;NC SID VOLUME .; RESTORE SCREEN ;START IRQ ; FINISHED ;GET CONTROL NUMBER
($FB),Y EXIT #$01 CONTR1 ($F1l),
TEMPO NEXT #$02 EXIT
; END OF MUSIC .; IS VALUE 1? ;NO
Y
;GET TEMPO ; TRY AGAIN .; I S CONTROL 2? ;NO EXIT PROG
($FB),Y V1PT+2
;GET HI BYTE FOR THE WAVEFORM ; POINTERS OF THE ; FOUR VOICES
LDA ($FB) ,'r'
;
STA INY LDA STA INY LDA STA BNE
STX 3rT' MUSIC1 LDY LDA STA LDA STA LDA CMP BNE PLA PLA PLA JMP MUSIC
DC DC
138
.'
0A 0A
MUSIC9 LDA BEQ CMP BEQ STA MUSIC2 INC ENE INC MUSIC3 LDA TAX LDA STA INC LDA STA INC ENE
ZERO PAGE
V2PT+2 ($FE), Y It3PT+2 ($FB) , 'r' V4PT+2 NE;-
8A2. $ A'~21 qAI ~ C/E24J Irj>AC/> D'fIQ
A2
AI
6PA4 ;tPA5
II PBl 12 PB2 13 PB3 14- PB415PB5 16PB6
1Ff
D¢
liD
1>6
:»1
!2-DI
:nlt
13D2
.D5 DA
I{ND
I+\lss
])3
Connection of a 2764 EPROM to expansion port lines.
+5V
RES
~
D33C/>
])3
~
In
D42CJ
])4-
(0
D52g
])5
D62'f
D6
D~2.6
¢2.25
I"TPST
C5124
18 CBI IQCB2
CS113 R/W 12-
20VBC
IRQ:!.I
NMI
OR
Connection of 6522 via I/O expansion connections. Fig. 5.14. (contd.)
IRQ.
Chapter Six
Interrupts and Their Use
Interrupts are the signals used by peripheral devices, such as the CIA chips, to signal to the processor that they require servicing. This IRQ signal will then cause the processor to halt its current operation temporarily in order to service the interrupt generating device. Having completed this servicing the processor returns to the interrupted program. 6.1 Interrupt requests (IRQ)
The major implementation of IRQs in the Commodore 64's operating system is to scan and receive key presses from the keyboard. This IRQ runs on Timer A of CIA # 1. The timer value is set up so that the keyboard is scanned every 1I 60th of a second. IRQ interrupts can be disabled by setting bit 2 ofthe processor status register or by the use of the command SEI. To re-enable IRQ, reset bit 2 or use the CLI command. The SEI command is used by the disk operating system to prevent timing errors when accessing the disk. The only other standard use of IRQs in the operating system of the Commodore 64 is in the tape 110 routines. Rather than just disabling IRQs, the tape system uses IRQs for reading from or writing to the tape. The tape system uses both Timer A and Timer B on CIA# 1 for reading and writing. For more information on the tape routines, see Chapter 4. 6.2. Interrupt generating devices 6.2.1 The CIA chips CIA#l Register 14 ($DC0D) Bit
7 6 5 4 3 2 1
0
Enable / disable (write), occurred (read) Not used Not used FLAG 1 line (cassette read) Serial data register TOO clock alarm Timer B Timer A
184
The Commodore 64 Kernal and Hardware Revealed
When reading bit 7 is used to determine whether an enabled IRQ on this chip occurred (if more than one device is connected to the IRQ line) i.e. if this bit was not set when the IRQ routine was caused, it must have been either the VIC chip or the expansion port. If bit 7 is set, bits .0-4 will tell what caused the IRQ. It should be noted that when using IRQs, it is advisable to keep a separate record of the IRQs that are enabled, since their respective bits may be set but not necessarily enabled. When writing, bit 7 is used to tell the CIA whether the lower bits are for disabling or enabling. If bit 7 is set, any other bits set are to enable an IRQ. Ifbit 7 is not set, any other bits set are to disable an IRQ.
1. Cassette read FLAG 1 line This line is used by the cassette read routines and creates an IRQ when it is enabled. The tape flags an IRQ on this line when the pulse on the tape goes from high to low. An example of the use of this IRQ is shown in Chapter 4 (fast tape operation). 2. Serial data register (SDR) The SOR is a serial input/ output device of the 6526 CIA chip. When IRQ is enabled on this register, the IRQ will be caused either when the full byte has been read in (input) or when it has been sent out (output). When the IRQ occurs, either a new value to send must be put into the SDR or the byte contained in the SDR will be read and the SOR left to input the next byte. The SDR uses 2 lines on the user port. These lines are SPI and CNTI, which together are used to send / receive data. When sending, each bit is set on S P and the CNT line is used to clock the bit using Timer A. An example use of the SOR can be found in Chapter 5.
3. TOD clock alarm When the TOO clock alarm IRQ has been enabled (after setting TOO and the alarm) an IRQ occurs when the value in TOO becomes equal to the value set in the alarm. An example of how to use the TOO clock can be found in Chapter 5. 41. Timer B Timer B can run in three different modes; as a straight timer, a count down on pulses from the CNT line of the user port, and a count down on Timer A running out. These three methods are outlined in Chapter 5. An IRQ will occur on Timer B in any of the three modes of operation when the value in Timer B clocks past zero. 5. Timer A Timer A has only one mode of operation; as a straight timer. An IRQ on Timer A will occur when the value in Timer A clocks past zero. Note that with Timers A and B, the timer always decreases until it clocks past zero. Therefore, to time something, the timer should be set to the period and when it runs out the time is up. With CIA IRQs, the IRQ is cleared by reading register 14.
6.2.2 The VIC chip The VIC chip is also connected to the IRQ line and VIC chip IRQs are controlled by registers 25 and 26 on the VIC chip.
Interrupts and Their Use
185
VIC register 25 ($0019) (Interrupt flag register) Bit
7 6-4 3 2 1
o
Set on any enabled VIC IRQ occurring Not used Light pen (I=occurred) Sprite to sprite collision (I=occurred) Sprite to background collision (I=occurred) Raster compare (l=occurred)
VIC register 26 ($001 A) (Interrupt enable mask) Bit
7-4 3 2 1
o
Not used Light pen (l=enabled) Sprite to sprite collision (I=enabled) Sprite to background collision (l=enabled) Raster compare (l=enabled)
To enable IRQ, register 26 should be read and the bit to enable set and then written back to register 26. When the IRQ occurs, reading register 25 will tell you which VIC IRQ has occurred. To clear the IRQ, the corresponding bit to clear is written to register 25. 1. Light pen The light pen IRQ occurs when the raster scan reaches the position ofthe light pen and the light pen values can then be read from registers 19 and 20. 2. Sprite to sprite collision Sprite to sprite collision IRQ occurs when any bit in the sprite to sprite collision register (30 - $D01 E) is set. 3. Sprite to background collision Sprite to background collision IRQ occurs when any bit in the sprite to background collision register (31 - $D0 I F) is set. 41. Raster compare Raster compare IRQ occurs when the raster position being displayed becomes equal to the compare value written to registers 17 ($D0 I I high bit) and 18 ($D012).
6.2.3 The expansion port IRQ can be caused by any 110 device connected to the Commodore 64 via the expansion port. There are two 'spare' areas for such 110 devices; they can either be addressed at $DE00 or $DF00. See Chapter 5 for an example of adding a 6522 VIA chip to the Commodore 64 via the expansion port.
186
The Commodore 64 Kernal and Hardware Revealed
6.3 Non maskable interrupts (NM!)
NMIs are so named because they cannot be disabled by the SEI command. Normally the NMI routine is not called regularly like the IRQ routine. This is because NMI is only caused by 2 devices: a) RS232 (user port FLAG sent low) b) RESTORE key There are five other ways of causing an NMI on the 64 that are not implemented in the software. These are Timers A and B, internal shift register, expansion port, and time of day clock on CIA#2. All NMIs except the RESTORE key and the expansion port are controlled by register 14 ($0000) on CIA#2. This register is used as a dual purpose write (enable/ disable NMI) and read (to determine the source of NMI). CIA#2 Register 14 ($00.00) Bit
7 6 5 4
3 2 1 .0
Enable/disable (write), occurred (read) Not used Not used User port FLAG line RS232 data received) Shift register TOO clock alarm Timer B Timer A
When reading bit 7 is used to determine whether an enabled NMI on this chip has occurred (if more than one CIA chip is connected to the NMI line) i.e. if this bit was not set when the NMI routine was caused, the NMI must have been either the RESTORE key or expansion port. If bit 7 is set, bits 0-4 will tell what caused the NMI. It should be noted that when using NMIs, it is advisable to keep a separate record of the NMIs that are enabled as their respective bits could be set but not enabled. When writing, bit 7 is used to tell the CIA whether the lower bits are for disabling or enabling. If bit 7 is set, any other bits set are to enable an NMI. If bit 7 is not set, any other bits set are to disable an NMI.
6.4 Devices that cause NMI
1 User port lFLAG line This line is the one used by the RS232 routines and causes an NMI when it is enabled. The method of flagging an NMI on this line is to set the line to +5 V and then to 0V. This method is outlined in Program 19 which uses 10 lines on the user port to transfer a block of memory from one CBM 64 to another (8 data lines and 2 lines to flag the NMI on the other 64). When initialised, the NMI
Interrupts and Their Use
caElO ceeEl Cae2 C0ElS c007 C0eA eeoc ce0F cell ce14 C016 C019 CElIB C01E celF C01F C0IF C01F C01F C020 C021 C022 ce23 C024 C926 ce29 C02B CEl2E C031 C033 C93S C03S C93B C03B C93D C049 ce43 C046 C049 C04B ce4D C04E
A91F 8Dlse3 A9ce SD1903 A9AE SD3203 A9Ce SD3303 A999 8D0DDD A904 SD70CO 60
LDA STA LDA STA LDA STA
#(NMI $9318 #)NI'II $0319 tt