]> git.sur5r.net Git - cc65/blob - libsrc/lynx/eeprom46.s
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / libsrc / lynx / eeprom46.s
1 ;***************
2 ; EEPROM-routines
3 ; for 93C46 (128 bytes as 64 16-bit words)
4 ;
5 ; created : 11.05.95
6 ; last modified :
7
8 ; 16.02.96      leaner (thanks to Harry)
9 ; 12.03.96      test for busy after write and erase (well, Harry ;)) )
10 ; 22.08.97      ported to ra65 for use with cc65
11 ; 02.12.97      added xref for the new ra65
12 ;
13 ;
14 ; (c) 1995..97 Bastian Schick
15 ; CS    = A7 (18)
16 ; CLK   = A1 (11)
17 ; DI/DO = AUDIN (32)
18 ;
19 ;And now how to contact the EEPROM :
20 ;
21 ;CARD
22 ;PORT               ----\/----      93C46(SMD too)
23 ;(18)  A7   --------| CS     |- +5V
24 ;(11)  A1   --------| CLK    |- NC
25 ;               +---| DI     |- NC
26 ;(32) AUDIN ----+---| DO     |- GND
27 ;                   ----------
28         .export         _lynx_eeread_93c46
29         .export         _lynx_eewrite_93c46
30         .import         popax
31         .importzp       ptr1
32
33         .include        "lynx.inc"
34
35 ; -------------------
36 ; EEPROM command list
37 EE_C_WRITE      =    $40
38 EE_C_READ       =    $80
39 EE_C_ERASE      =    $C0
40 EE_C_EWEN       =    $30
41 EE_C_EWDS       =    $00
42
43 ; ------------------------------------------------------------------------
44 ; unsigned __fastcall__ lynx_eeread_93c46(unsigned char cell);
45 ; /* Read a 16 bit word from the given address */
46 ;
47 _lynx_eeread_93c46:
48         and #$3f
49         ora #EE_C_READ
50         jsr EE_Send9Bit
51         jsr EE_Read16Bit
52         lda ptr1
53         ldx ptr1+1
54         rts
55
56 ;***************
57 ; reads EEPROM-word to ptr1
58 ; A,Y destroyed
59 EE_Read16Bit:
60         lda #$a
61         sta IODIR       ; set AUDIN to Input
62         clc
63         stz ptr1
64         stz ptr1+1
65         ldy #15
66 EEloop1:
67 ; CLK = 1
68         stz RCART0
69         stz RCART0
70 ; CLK = 0
71         stz RCART0
72         stz RCART0
73
74         lda IODAT
75         and #$10        ; mask bit
76         adc #$f0        ; C=1 if A=$10
77         rol ptr1
78         rol ptr1+1      ; shifts 0 to Carry
79         dey
80         bpl EEloop1
81
82         ldx #$1a
83         stx IODIR       ; set AUDIN for output
84 ;EE_SET_CS_LOW
85
86         ldx #3
87         stx SYSCTL1
88         dex
89         stx SYSCTL1
90
91         rts
92
93 ;***************
94 ; write word to EEPROM
95 ; void __fastcall__ lynx_eewrite_93c46(unsigned int addr, unsigned int val);
96 _lynx_eewrite_93c46:
97         sta ptr1
98         stx ptr1+1
99         lda #EE_C_EWEN
100         jsr EE_Send9Bit
101         jsr popax
102         and #$3f
103         ora #EE_C_WRITE
104         jsr EE_Send9Bit
105         jsr EE_Send16Bit        ; Sends ptr1 that contains val
106
107 EE_wait:
108 ; EE_SET_CS_HIGH
109
110         ldx #63
111 EEloop:
112         stz RCART0
113         stz RCART0
114         dex
115         bpl EEloop
116
117         lda #$0A
118         sta IODIR       ; AUDIN to input
119         lda #$10
120 EE_wait1:
121         bit IODAT       ; 'til ready :D0-read is /D0-written
122         beq EE_wait1
123         lda #$1a        ; AUDIN to output
124         sta IODIR
125         lda #EE_C_EWDS
126 ;       bra EE_Send9Bit ; fall into
127 ;***************
128 ; send A via I2C
129 ; A,Y destroyed
130 ;***************
131 EE_Send9Bit:
132 ; EE_SET_CS_LOW
133         ldy #3
134         sty SYSCTL1
135         dey
136         sty SYSCTL1
137 ; EE_SET_CS_HIGH
138
139         ldy #63
140 EEloop2:
141         stz RCART0
142         stz RCART0
143         dey
144         bpl EEloop2
145
146         ldy #8
147         sec             ; start bit
148         ror A
149         ror A
150         ror A
151         ror A           ; bit 8 at pos. 4
152 EEloop3:
153         tax
154         and #$10
155         ora #$b
156         sta IODAT
157 ; CLK = 1
158         stz RCART0
159         stz RCART0
160 ; CLK = 0
161         stz RCART0
162         stz RCART0
163         txa
164         rol A
165         dey
166         bpl EEloop3
167         rts
168 ;***************
169 ; send ptr1 to EEPROM
170 EE_Send16Bit:
171         lda ptr1+1
172
173         ror A
174         ror ptr1
175         ror A
176         ror ptr1
177         ror A
178         ror ptr1
179
180         ldy #15
181 EEloop5:
182         tax
183         and #$10
184         ora #$b
185         sta IODAT
186 ; CLK = 1
187         stz RCART0
188         stz RCART0
189 ; CLK = 0
190         stz RCART0
191         stz RCART0
192         txa
193         rol ptr1
194         rol A
195         dey
196         bpl EEloop5
197
198 ; EE_SET_CS_LOW
199         ldx #3
200         stx SYSCTL1
201         dex
202         stx SYSCTL1
203         rts
204