blob: 89bbf9db94a05a0f3717840d3e3b7ae76c0caf63 [file] [log] [blame]
kthacker6de67752006-04-17 15:02:26 +00001 ;; The following code is written and is copyrighted by
2 ;; Groepaz/Hitmen
3
4 ;; Small changes by Adam Dunkels (renamed ss232 -> rs232)
5
6;----------------------------------------------------------------------------------------------
7; silver surfer polling mode driver for cc65
8; - work from here to create a full featured driver with interupts.
9; gpz fixed 20020828: fatal bug fixed in _rs232_params
10;----------------------------------------------------------------------------------------------
11
12rs16550base = $de08
13
14fifo_rxd = rs16550base+$00 ;8 (r)
15fifo_txd = rs16550base+$00 ;8 (w)
16
17fifo_dll = rs16550base+$00 ;8 (r/w)
18fifo_dlm = rs16550base+$01 ;9 (r/w)
19
20fifo_ier = rs16550base+$01 ;9
21
22fifo_fcr = rs16550base+$02 ;a (w)
23fifo_iir = rs16550base+$02 ;a (r)
24fifo_lcr = rs16550base+$03 ;b
25fifo_mcr = rs16550base+$04 ;c
26fifo_lsr = rs16550base+$05 ;d
27fifo_msr = rs16550base+$06 ;e (r)
28fifo_scratch = rs16550base+$07 ;f (r/w)
29
30
31
32 .export _rs232_init
33 .export _rs232_done
34 .export _rs232_params
35 .export _rs232_put
36 .export _rs232_get
37
38 .importzp ptr1, ptr2
39 .import popa, popax
40
41;----------------------------------------------------------------------------------------------
42; Error codes. Beware: The codes must match the codes in the C header file
43
44ErrNotInitialized = $01
45ErrBaudTooFast = $02
46ErrBaudNotAvail = $03
47ErrNoData = $04
48ErrOverflow = $05
49
50;----------------------------------------------------------------------------------------------
51;unsigned char __fastcall__ rs232_init (char hacked);
52;/* Initialize the serial port, install the interrupt handler. The parameter
53; * has no effect for now and should be set to 0.
54; */
55;----------------------------------------------------------------------------------------------
56
57 .code
58
59_rs232_init:
60 ; enable ssurfer-port
61 lda $de01
62 ora #$01
63 sta $de01
64
65 ; disable nmi's from ssurfer
66 lda #%00000000
67 sta fifo_ier
68
69 ; activate dtr
70 lda #%00000001
71 sta fifo_mcr
72
73 lda #$00 ; ok
74 tax
75 rts
76
77;----------------------------------------------------------------------------------------------
78;unsigned char __fastcall__ rs232_done (void);
79;/* Close the port, deinstall the interrupt hander. You MUST call this function
80; * before terminating the program, otherwise the machine may crash later. If
81; * in doubt, install an exit handler using atexit(). The function will do
82; * nothing, if it was already called.
83; */
84;----------------------------------------------------------------------------------------------
85
86_rs232_done:
87 ; disable nmi's from ssurfer
88 lda #%00000000
89 sta fifo_ier
90
91 ; deactivate dtr
92 sta fifo_mcr
93
94 ; disable ssurfer-port
95 lda $de01
96 and #$fe
97 sta $de01
98
99 lda #$00 ; ok
100 tax
101 rts
102
103;----------------------------------------------------------------------------------------------
104;unsigned char __fastcall__ rs232_params (unsigned char params, unsigned char parity);
105;/* Set the port parameters. Use a combination of the #defined values above. */
106;----------------------------------------------------------------------------------------------
107
108 .data
109
110_rs232_baudrates:
111
112 .word (7372800 / ( 50 * 16))
113 .word (7372800 / ( 110 * 16))
114 .word (7372800 / ( 269 * 8))
115 .word (7372800 / ( 300 * 16))
116 .word (7372800 / ( 600 * 16))
117 .word (7372800 / ( 1200 * 16))
118 .word (7372800 / ( 2400 * 16))
119 .word (7372800 / ( 4800 * 16))
120 .word (7372800 / ( 9600 * 16))
121 .word (7372800 / ( 19200 * 16))
122 .word (7372800 / ( 38400 * 16))
123 .word (7372800 / ( 57600 * 16))
124 .word (7372800 / ( 115200 * 16))
125 .word (7372800 / ( 230400 * 16))
126
127 .bss
128
129_rs232_tmp1:
130 .res 1
131
132 .code
133
134_rs232_params:
135
136 sta _rs232_tmp1 ; save parity
137
138 ; reset fifo
139 lda #%10000111
140 sta fifo_fcr
141
142 ; that delay thing really needed ?!
143 ; (original datasheet mentions a delay here)
144 ; ldy #$00
145 ; dey
146 ; bny *-1
147
148 ; set dlab
149 lda #%10000011 ; we assmume 8n1
150 sta fifo_lcr
151
152 jsr popa
153 tay ; save param
154
155 ; set baudrate
156 clc
157 lsr a
158 lsr a
159 lsr a
160 lsr a
161 asl a
162 tax
163 lda _rs232_baudrates,x
164 sta fifo_dll
165 lda _rs232_baudrates+1,x
166 sta fifo_dlm
167
168 tya ; param
169 and #$0f
170 ora _rs232_tmp1 ; parity
171
172 ; reset dlab
173 sta fifo_lcr
174
175 lda #$00 ; ok
176 tax
177 rts
178
179;----------------------------------------------------------------------------------------------
180; check if byte available, returns AKKU=0 if none
181
182ss_getlsr:
183 lda fifo_lsr
184 and #$01
185 rts
186
187;----------------------------------------------------------------------------------------------
188;unsigned char __fastcall__ rs232_get (char* b);
189;/* Get a character from the serial port. If no characters are available, the
190; * function will return RS_ERR_NO_DATA, so this is not a fatal error.
191; */
192;----------------------------------------------------------------------------------------------
193; get byte (non blocking, returns byte in A or CARRY=1 - error)
194
195_rs232_get:
196 sta ptr1
197 stx ptr1+1
198
199 jsr ss_getlsr ; check if byte available
200; bne sk32 ; yes
201 bne sk33 ; yes
202
203 ; activate rts
204 lda #%00000011
205 sta fifo_mcr
206sk32:
207
208 ; deactivate rts
209; lda #%00000001
210; sta fifo_mcr
211
212 jsr ss_getlsr ; check if byte available
213 bne sk33 ; yes
214
215 ; deactivate rts
216 lda #%00000001
217 sta fifo_mcr
218
219 lda #ErrNoData ; no data
220 ldx #0
221 rts
222sk33:
223 ; deactivate rts
224 lda #%00000001
225 sta fifo_mcr
226
227 ; get byte
228 ldy #$00
229 lda fifo_rxd
230 sta (ptr1),y
231
232 lda #0 ; ok
233 tax
234 rts
235
236;----------------------------------------------------------------------------------------------
237;unsigned char __fastcall__ rs232_put (char b);
238;/* Send a character via the serial port. There is a transmit buffer, but
239; * transmitting is not done via interrupt. The function returns
240; * RS_ERR_OVERFLOW if there is no space left in the transmit buffer.
241; */
242;----------------------------------------------------------------------------------------------
243
244_rs232_put:
245 tax
246 ; transmit buf ready?
247 lda fifo_lsr
248 and #%00100000
249 bne @sk1
250@sk2:
251 lda #ErrOverflow ; overflow
252 ldx #$00
253 rts
254@sk1:
255 ; reciever ready?
256 lda fifo_msr
257 and #%00010000
258 beq @sk2
259
260 stx fifo_txd
261
262 lda #$00 ; ok
263 tax
264 rts
265
266;----------------------------------------------------------------------------------------------
267;unsigned char __fastcall__ rs232_pause (void);
268;/* Assert flow control and disable interrupts. */
269;----------------------------------------------------------------------------------------------
270
271_rs232_pause:
272 ; activate rts
273 lda #%00000011
274 sta fifo_mcr
275
276 lda #$00 ; ok
277 tax
278 rts
279
280;----------------------------------------------------------------------------------------------
281;unsigned char __fastcall__ rs232_unpause (void);
282;/* Re-enable interrupts and release flow control */
283;----------------------------------------------------------------------------------------------
284
285_rs232_unpause:
286 ; deactivate rts
287 lda #%00000001
288 sta fifo_mcr
289
290 lda #$00 ; ok
291 tax
292 rts
293
294;----------------------------------------------------------------------------------------------
295;unsigned char __fastcall__ rs232_status (unsigned char* status,
296; unsigned char* errors);
297;/* Return the serial port status. */
298;----------------------------------------------------------------------------------------------
299
300_rs232_status:
301 sta ptr2
302 stx ptr2+1
303 jsr popax
304 sta ptr1
305 stx ptr1+1
306
307 ldy #$00
308
309 ; Get status
310 lda fifo_iir
311 and #%00000001
312 sta _rs232_tmp1
313 lda fifo_msr
314 lsr a
315 and #%01010000
316 ora _rs232_tmp1
317 sta _rs232_tmp1
318 lda fifo_lsr
319 and #%00101110
320 ora _rs232_tmp1
321 sta (ptr1),y
322
323 ; Get errors
324 lda #$00 ; ok
325 sta (ptr2),y
326
327 lda #$00 ; ok
328 tax
329 rts