-
Notifications
You must be signed in to change notification settings - Fork 2
/
sfr_rw.c
311 lines (256 loc) · 9.89 KB
/
sfr_rw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*-------------------------------------------------------------------------
sfr_rw.c - reading/writing mcs-51 sfr
Copyright (C) 2007 Frieder Ferlemann <Frieder.Ferlemann AT web.de>
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
In other words, you are welcome to use, share and improve this program.
You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding!
As a special exception, you may use this file as part of a free software
library for the XO of the One Laptop per Child project without restriction.
Specifically, if other files instantiate
templates or use macros or inline functions from this file, or you compile
this file and link it with other files to produce an executable, this
file does not by itself cause the resulting executable to be covered by
the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
-------------------------------------------------------------------------*/
#include <stdbool.h>
#include "compiler.h"
//! some mcs51 have unified memory
/*! can execute code from xdata and code memory.
This seems not to be the case for the kb3700
(although the data sheet section 4.12 says
"the 8051 uses MOVX and MOVC instructions to read [..] XRAM [..]"
Maybe there is a bit to turn this on?)
*/
#define UNIFIED_MEMORY (0)
//! needs to be addressable as both code and xdata memory
static volatile unsigned char __xdata xdata_code_snippet[4];
//! routine to allow indexed read access to mcs-51 Special Function Registers
/*! The instruction set of the MCS-51 normally does not allow indexed
access to SFR.
If there is unified memory access for code and xdata
we can patch a code snippet and jump there.
Otherwise there is a LARGE table.
\note the kb3700 also has xdata addressable SFR (above 0xfc00).
\note These are not meant here.
*/
unsigned char read_mcs51_sfr(unsigned char address) __naked
{
address; /* avoid warning about unused variable */
#if defined( SDCC ) && UNIFIED_MEMORY
__asm
ar2 = 0x02
push ar2
mov r2, dpl
mov a,#0x85 ; opcode for mov
mov dptr, #_xdata_code_snippet
movx @dptr, a
mov a,r2 ; src (address)
inc dptr
movx @dptr, a
mov a,#dpl ; dest (dpl), # is intended
inc dptr
movx @dptr, a
mov a,#0x22 ; opcode for ret
inc dptr
movx @dptr, a
pop ar2
ljmp _xdata_code_snippet
__endasm;
#elif defined( SDCC ) && !UNIFIED_MEMORY
__asm
mov b, #0x00
mov a, dpl
xrl a, #0x80
add a, acc
mov _B_1, c
add a, acc
mov _B_0, c ; now ACC and B hold SFR number (-0x80) multiplied by 4
;
add a, #jumptable
push acc
mov a,b
addc a, #(jumptable >> 8)
push acc
;
ret ; "return" to the address pushed onto the stack
jumptable:
.db 0x85, 0x80, 0x82, 0x22 ; the equivalent to: mov dpl,0x80 ret
.db 0x85, 0x81, 0x82, 0x22 ; the equivalent to: mov dpl,0x81 ret
.db 0x85, 0x82, 0x82, 0x22
.db 0x85, 0x83, 0x82, 0x22
.db 0x85, 0x84, 0x82, 0x22
.db 0x85, 0x85, 0x82, 0x22
.db 0x85, 0x86, 0x82, 0x22
.db 0x85, 0x87, 0x82, 0x22
.db 0x85, 0x88, 0x82, 0x22
.db 0x85, 0x89, 0x82, 0x22
.db 0x85, 0x8a, 0x82, 0x22
.db 0x85, 0x8b, 0x82, 0x22
.db 0x85, 0x8c, 0x82, 0x22
.db 0x85, 0x8d, 0x82, 0x22
.db 0x85, 0x8e, 0x82, 0x22
.db 0x85, 0x8f, 0x82, 0x22
.db 0x85, 0x90, 0x82, 0x22
.db 0x85, 0x91, 0x82, 0x22
.db 0x85, 0x92, 0x82, 0x22
.db 0x85, 0x93, 0x82, 0x22
.db 0x85, 0x94, 0x82, 0x22
.db 0x85, 0x95, 0x82, 0x22
.db 0x85, 0x96, 0x82, 0x22
.db 0x85, 0x97, 0x82, 0x22
.db 0x85, 0x98, 0x82, 0x22
.db 0x85, 0x99, 0x82, 0x22
.db 0x85, 0x9a, 0x82, 0x22
.db 0x85, 0x9b, 0x82, 0x22
.db 0x85, 0x9c, 0x82, 0x22
.db 0x85, 0x9d, 0x82, 0x22
.db 0x85, 0x9e, 0x82, 0x22
.db 0x85, 0x9f, 0x82, 0x22
.db 0x85, 0xa0, 0x82, 0x22
.db 0x85, 0xa1, 0x82, 0x22
.db 0x85, 0xa2, 0x82, 0x22
.db 0x85, 0xa3, 0x82, 0x22
.db 0x85, 0xa4, 0x82, 0x22
.db 0x85, 0xa5, 0x82, 0x22
.db 0x85, 0xa6, 0x82, 0x22
.db 0x85, 0xa7, 0x82, 0x22
.db 0x85, 0xa8, 0x82, 0x22
.db 0x85, 0xa9, 0x82, 0x22
.db 0x85, 0xaa, 0x82, 0x22
.db 0x85, 0xab, 0x82, 0x22
.db 0x85, 0xac, 0x82, 0x22
.db 0x85, 0xad, 0x82, 0x22
.db 0x85, 0xae, 0x82, 0x22
.db 0x85, 0xaf, 0x82, 0x22
.db 0x85, 0xb0, 0x82, 0x22
.db 0x85, 0xb1, 0x82, 0x22
.db 0x85, 0xb2, 0x82, 0x22
.db 0x85, 0xb3, 0x82, 0x22
.db 0x85, 0xb4, 0x82, 0x22
.db 0x85, 0xb5, 0x82, 0x22
.db 0x85, 0xb6, 0x82, 0x22
.db 0x85, 0xb7, 0x82, 0x22
.db 0x85, 0xb8, 0x82, 0x22
.db 0x85, 0xb9, 0x82, 0x22
.db 0x85, 0xba, 0x82, 0x22
.db 0x85, 0xbb, 0x82, 0x22
.db 0x85, 0xbc, 0x82, 0x22
.db 0x85, 0xbd, 0x82, 0x22
.db 0x85, 0xbe, 0x82, 0x22
.db 0x85, 0xbf, 0x82, 0x22
.db 0x85, 0xc0, 0x82, 0x22
.db 0x85, 0xc1, 0x82, 0x22
.db 0x85, 0xc2, 0x82, 0x22
.db 0x85, 0xc3, 0x82, 0x22
.db 0x85, 0xc4, 0x82, 0x22
.db 0x85, 0xc5, 0x82, 0x22
.db 0x85, 0xc6, 0x82, 0x22
.db 0x85, 0xc7, 0x82, 0x22
.db 0x85, 0xc8, 0x82, 0x22
.db 0x85, 0xc9, 0x82, 0x22
.db 0x85, 0xca, 0x82, 0x22
.db 0x85, 0xcb, 0x82, 0x22
.db 0x85, 0xcc, 0x82, 0x22
.db 0x85, 0xcd, 0x82, 0x22
.db 0x85, 0xce, 0x82, 0x22
.db 0x85, 0xcf, 0x82, 0x22
.db 0x85, 0xd0, 0x82, 0x22
.db 0x85, 0xd1, 0x82, 0x22
.db 0x85, 0xd2, 0x82, 0x22
.db 0x85, 0xd3, 0x82, 0x22
.db 0x85, 0xd4, 0x82, 0x22
.db 0x85, 0xd5, 0x82, 0x22
.db 0x85, 0xd6, 0x82, 0x22
.db 0x85, 0xd7, 0x82, 0x22
.db 0x85, 0xd8, 0x82, 0x22
.db 0x85, 0xd9, 0x82, 0x22
.db 0x85, 0xda, 0x82, 0x22
.db 0x85, 0xdb, 0x82, 0x22
.db 0x85, 0xdc, 0x82, 0x22
.db 0x85, 0xdd, 0x82, 0x22
.db 0x85, 0xde, 0x82, 0x22
.db 0x85, 0xdf, 0x82, 0x22
.db 0x85, 0xe0, 0x82, 0x22
.db 0x85, 0xe1, 0x82, 0x22
.db 0x85, 0xe2, 0x82, 0x22
.db 0x85, 0xe3, 0x82, 0x22
.db 0x85, 0xe4, 0x82, 0x22
.db 0x85, 0xe5, 0x82, 0x22
.db 0x85, 0xe6, 0x82, 0x22
.db 0x85, 0xe7, 0x82, 0x22
.db 0x85, 0xe8, 0x82, 0x22
.db 0x85, 0xe9, 0x82, 0x22
.db 0x85, 0xea, 0x82, 0x22
.db 0x85, 0xeb, 0x82, 0x22
.db 0x85, 0xec, 0x82, 0x22
.db 0x85, 0xed, 0x82, 0x22
.db 0x85, 0xee, 0x82, 0x22
.db 0x85, 0xef, 0x82, 0x22
.db 0x85, 0xf0, 0x82, 0x22
.db 0x85, 0xf1, 0x82, 0x22
.db 0x85, 0xf2, 0x82, 0x22
.db 0x85, 0xf3, 0x82, 0x22
.db 0x85, 0xf4, 0x82, 0x22
.db 0x85, 0xf5, 0x82, 0x22
.db 0x85, 0xf6, 0x82, 0x22
.db 0x85, 0xf7, 0x82, 0x22
.db 0x85, 0xf8, 0x82, 0x22
.db 0x85, 0xf9, 0x82, 0x22
.db 0x85, 0xfa, 0x82, 0x22
.db 0x85, 0xfb, 0x82, 0x22
.db 0x85, 0xfc, 0x82, 0x22
.db 0x85, 0xfd, 0x82, 0x22
.db 0x85, 0xfe, 0x82, 0x22
.db 0x85, 0xff, 0x82, 0x22
__endasm;
#else
return 0xff;
#endif
}
//! routine to allow indexed write access to mcs-51 Special Function Registers
/*! The instruction set of the MCS-51 normally does not allow indexed
access to SFR.
Because the kb3700 uses unified memory access for code and xdata
we can patch a code snippet and jump there.
\note the kb3700 also has xdata addressable SFR (above 0xfc00).
\note These are not meant here.
*/
void write_mcs51_sfr(unsigned char address, unsigned char value) __naked
{
address; /* avoid warning about unused variable */
value; /* avoid warning about unused variable */
#if defined( SDCC )
__asm
ar2 = 0x02
push ar2
mov r2, dpl
mov a,#0x85
mov dptr, #_xdata_code_snippet
movx @dptr, a
mov a, _write_mcs51_sfr_PARM_2
inc dptr
movx @dptr, a
mov a,r2
inc dptr
movx @dptr, a
mov a,#0x22
inc dptr
movx @dptr, a
pop ar2
ljmp _xdata_code_snippet
__endasm;
#endif
}