]> git.sur5r.net Git - cc65/blob - libsrc/lynx/eeprom.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / lynx / eeprom.s
1 ;****************
2 ; CC65 Lynx Library
3 ;
4 ; Originally by Bastian Schick
5 ; http://www.geocities.com/SiliconValley/Byte/4242/lynx/
6 ;
7 ; Ported to cc65 (http://www.cc65.org) by
8 ; Shawn Jefferson, June 2004
9 ;
10 ; Several changes,
11 ; Ullrich von Bassewitz, 1004-10-14
12 ;
13 ;
14 ;****************
15 ;* EEPROM-routs
16 ;* for 93C46 (1024bit => 64 16-bit words)
17 ;*
18 ;* created : 11.05.95
19 ;* last modified :
20 ;*
21 ;* 16.02.96      leaner (thanks to Harry)
22 ;* 12.03.96      test for busy after write and erase (well, Harry ;)) )
23 ;* 22.08.97      ported to ra65 for use with cc65
24 ;* 02.12.97      added xref for the new ra65
25 ;*
26 ;*
27 ;* (c) 1995..97 Bastian Schick
28 ;* CS    = A7 (18)
29 ;* CLK   = A1 (11)
30 ;* DI/DO = AUDIN (32)
31 ;*
32 ;* And now how to contact the EEPROM :
33 ;*
34 ;* CARD
35 ;* PORT               ----\/----      93C46(SMD too)
36 ;* (18)  A7   --------| CS     |- +5V
37 ;* (11)  A1   --------| CLK    |- NC
38 ;*                +---| DI     |- NC
39 ;* (32) AUDIN ----+---| DO     |- GND
40 ;*                    ----------
41 ;*
42 ;****************
43
44
45         .export         _lynx_eeprom_read
46         .export         _lynx_eeprom_write
47         .export         _lynx_eeprom_erase
48         .import         popa
49         .importzp       ptr1
50
51         .include        "lynx.inc"
52
53
54 ; ------------------------------------------------------------------------
55 ; EEPROM command list
56
57 EE_C_WRITE      =    $40
58 EE_C_READ       =    $80
59 EE_C_ERASE      =    $C0
60 EE_C_EWEN       =    $30
61 EE_C_EWDS       =    $00
62
63
64 ; ------------------------------------------------------------------------
65 ; unsigned __fastcall__ lynx_eeprom_read (unsigned char cell);
66 ; /* Read a 16 bit word from the given address */
67 ;
68
69 _lynx_eeprom_read:
70         and     #$3f
71         ora     #EE_C_READ
72         jsr     EE_Send9Bit
73
74         lda     #$a
75         sta     IODIR            ; set AUDIN to Input
76
77         clc
78         stz     ptr1
79         stz     ptr1+1          ; Clear result
80         ldy     #16-1           ; Initialize bit counter
81 @L1:
82 ; CLK = 1
83         stz     RCART0
84         stz     RCART0
85 ; CLK = 0
86         stz     RCART0
87         stz     RCART0
88
89         lda     IODAT
90         and     #$10             ; mask bit
91         adc     #$f0             ; C=1 if A=$10
92         rol     ptr1
93         rol     ptr1+1           ; shifts 0 to Carry
94         dey
95         bpl     @L1
96
97         ldx     #$1a
98         stx     IODIR            ; set AUDIN for output
99 ;EE_SET_CS_LOW
100
101         ldx     #3
102         stx     SYSCTL1
103         dex
104         stx     SYSCTL1
105
106         lda     ptr1
107         ldy     ptr1+1          ; Load result
108
109         rts
110
111
112 ; ------------------------------------------------------------------------
113 ; unsigned __fastcall__ lynx_eeprom_erase (unsigned char cell);
114 ; /* Clear the word at the given address */
115 ;
116
117 _lynx_eeprom_erase:
118         pha                     ; Save argument
119         lda     #EE_C_EWEN      ; EWEN
120         jsr     EE_Send9Bit
121         pla                     ; Restore cell
122         and     #$3f
123         ora     #EE_C_ERASE     ; clear cell A
124         jsr     EE_Send9Bit
125         bra     EE_wait
126
127
128 ; ------------------------------------------------------------------------
129 ; unsigned __fastcall__ lynx_eeprom_write (unsigned char cell, unsigned val);
130 ; /* Write the word at the given address */
131 ;
132
133 _lynx_eeprom_write:
134         sta     ptr1
135         stx     ptr1+1          ; Save val into ptr1
136         lda     #EE_C_EWEN      ; EWEN
137         jsr     EE_Send9Bit
138         jsr     popa            ; Get cell
139         and     #$3f            ; Make valid range 0..63
140         ora     #EE_C_WRITE     ; WRITE
141         jsr     EE_Send9Bit
142         jsr     EE_Send16Bit    ; Send value in ptr1
143
144 EE_wait:
145 ; EE_SET_CS_HIGH
146
147         ldx     #63
148 EEloop:
149         stz     RCART0
150         stz     RCART0
151         dex
152         bpl     EEloop
153
154         lda     #$0A
155         sta     IODIR           ; AUDIN to input
156         lda     #$10
157 EE_wait1:
158         bit     IODAT           ; 'til ready :D0-read is /D0-written
159         beq     EE_wait1
160         lda     #$1a            ; AUDIN to output
161         sta     IODIR
162
163         lda     #EE_C_EWDS      ; EWDS
164
165 ;       bra     EE_Send9Bit     ; fall into
166
167
168 ; ------------------------------------------------------------------------
169 ; Send 8 bit value in A to eeprom
170
171 EE_Send9Bit:
172 ; EE_SET_CS_LOW
173         ldx     #3
174         stx     SYSCTL1
175         dex
176         stx     SYSCTL1
177 ; EE_SET_CS_HIGH
178
179         ldx     #63
180 EEloop2:
181         stz     RCART0
182         stz     RCART0
183         dex
184         bpl     EEloop2
185
186         ldy     #8
187         sec                     ; start bit
188         ror     A
189         ror     A
190         ror     A
191         ror     A               ; bit 8 at pos. 4
192 EEloop3:
193         tax
194         and     #$10
195         ora     #$b
196         sta     IODAT
197 ; CLK = 1
198         stz     RCART0
199         stz     RCART0
200 ; CLK = 0
201         stz     RCART0
202         stz     RCART0
203         txa
204         rol     A
205         dey
206         bpl     EEloop3
207
208         lda     #$b             ; fnr neue EEPROMs
209         sta     IODAT
210
211         rts
212
213 ; ------------------------------------------------------------------------
214 ; Send 16 bit value in ptr1 to eeprom
215
216 EE_Send16Bit:
217         lda     ptr1+1
218
219         ror     A
220         ror     ptr1
221         ror     A
222         ror     ptr1
223         ror     A
224         ror     ptr1
225
226         ldy     #15
227 EEloop4:
228         tax
229         and     #$10
230         ora     #$b
231         sta     IODAT
232 ; CLK = 1
233         stz     RCART0
234         stz     RCART0
235 ; CLK = 0
236         stz     RCART0
237         stz     RCART0
238         txa
239         rol     ptr1
240         rol     A
241         dey
242         bpl     EEloop4
243
244 ; EE_SET_CS_LOW
245         ldx     #3
246         stx     SYSCTL1
247         dex
248         stx     SYSCTL1
249
250         lda     #$b             ; fnr neue EEPROMs
251         sta     IODAT
252
253         rts
254
255