]> cloudbase.mooo.com Git - avrcpm.git/blame - avr/init.asm
* cpm/BIOS.MAC
[avrcpm.git] / avr / init.asm
CommitLineData
6efd6291 1; Hardware initialisation, disk, mmc, timer, DRAM test
9c15f366
L
2;
3; Copyright (C) 2010 Sprite_tm
dc705dc0 4; Copyright (C) 2010-2013 Leo C.
9c15f366
L
5;
6; This file is part of avrcpm.
7;
8; avrcpm is free software: you can redistribute it and/or modify it
9; under the terms of the GNU General Public License as published by
10; the Free Software Foundation, either version 3 of the License, or
11; (at your option) any later version.
12;
13; avrcpm is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16; GNU General Public License for more details.
17;
18; You should have received a copy of the GNU General Public License
19; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
20;
21; $Id$
22;
23
24#define REFR_PRE 8 /* timer prescale factor 1/8 */
25#define REFR_CS 0x02 /* timer clock select for 1/8 */
26#define REFR_CNT F_CPU / REFR_RATE / REFR_PRE
27
28 .cseg
29regval_tab:
30 .db 0,0
31 .db 0xFE,0xFC ; _RAS0 _CAS0
32 .db 0xF8,0xF4 ; _OE _WE
33 .db 255,0 ; _255 _0
34regval_tab_e:
35
36start:
e8384f88
L
37 ldi temp,low(RAMEND) ; top of memory; vim:set ts=8 noet nowrap
38
9c15f366
L
39 out SPL,temp ; init stack pointer
40 ldi temp,high(RAMEND) ; top of memory
41 out SPH,temp ; init stack pointer
42
43; - Load some registers with constant values
44
45 ldiw z,regval_tab*2
46 ldiw y,0
47cp_l: lpm xh,z+
48 st y+,xh
49 cpi zl,low(regval_tab_e*2)
50 brne cp_l
51
52; - Kill wdt
53
54 wdr
55 out MCUSR,_0
56
57 ldi temp,(1<<WDCE) | (1<<WDE)
58 outm8 WDTCSR,temp
59 ldi temp,(1<<WDCE)
60 outm8 WDTCSR,temp
61
62; - Setup Ports
63
825ecc9d
L
64; ldi temp,(1<<PUD) ;disable pullups
65; outm8 P_PUD,temp
66 out PORTD,_255 ;all pins high (enables pullup on input ports)
9c15f366
L
67 out PORTB,_255
68 out PORTC,_255
69 out DDRD,_255 ; all outputs
70 out DDRB,_255
71 out DDRC,_255
72 cbi P_RXD-1,RXD ; RXD pin is input
73
74 outm8 TIMSK1,_0
75 outm8 TIMSK2,_0
76 outm8 TCCR2A,_0
77 outm8 TCCR2B,_0
78
79; - Clear RAM
80
81 ldiw z,SRAM_START
82 ldi temp2,high(ramtop)
98979541 83clr_loop:
9c15f366
L
84 st z+,_0
85 cpi zl,low(ramtop)
86 cpc zh,temp2
98979541
L
87 brne clr_loop
88
89; - Fill unused RAM (stack)
90
91 ldi temp2,high(RAMEND+1)
92 ldi temp,SRAMFILL_VAL
93fill_loop:
94 st z+,temp
95 cpi zl,low(RAMEND+1)
96 cpc zh,temp2
97 brne fill_loop
9c15f366 98
9c15f366
L
99; Init clock/timer system
100
101; Init timer 1 as 1 ms system clock tick.
102
103 ldi temp, low (F_CPU/1000)
104 ldi temp2,high(F_CPU/1000)
105 outm8 OCR1BH,temp2
106 outm8 OCR1BL,temp
107 ldi temp,(1<<ICNC1)|(1<<CS10) ;Noise cancel, fall. edge, Normal Mode, clk/1
108 outm8 TCCR1B,temp
109 inm8 temp,TIMSK1
110 ori temp,(1<<OCIE1B) ;Enable 1ms int.
111 outm8 TIMSK1,temp
112
113; - Init serial port
114
115 rcall uart_init
116
825ecc9d 117;Init timer2. Refresh-call should happen every (8ms/512) cycles.
9c15f366
L
118
119 ldi temp,REFR_CNT*2 ; 2 cycles per int
120 outm8 OCR2A,temp
121 inm8 temp,TCCR2A
122 ori temp,(1<<WGM21) ;CTC mode
123 outm8 TCCR2A,temp
124 inm8 temp,TCCR2B
125 ori temp,REFR_CS ;clk/REFR_PRE
126 outm8 TCCR2B,temp
127 inm8 temp,TIMSK2
128 ori temp, (1<<OCIE2A)
129 outm8 TIMSK2,temp
130
131 sei
132
dc705dc0 133#if I2C_SUPPORT
d8fa6a36
L
134 rcall i2c_init ; Init I2C master
135 rcall rtc_get
136#endif
137
9c15f366
L
138
139.if BOOTWAIT
140 ldi temp,10
b741422e 141 rcall delay_ms
9c15f366
L
142
143.endif
144
145 rcall printstr
146 .db 13,13,"CPM on an AVR, v"
147 db_version VMAJOR, VMINOR
5c8bb361 148 printstring " r" SVN_REVSTR " " TESTSTR
9c15f366
L
149
150.if MEMTEST
151 printnewline
152 printstring "Testing RAM: fill..."
153
154;Fill RAM
155 ldiw x,0
156ramtestw:
157 mov temp,xh
158 eor temp,xl
4675c141 159 rcall dram_write_pp
9c15f366
L
160 brcc ramtestw
161 printstring "wait..."
162
163 ldi temp2,8
164ramtestwl:
165 ldi temp,255
b741422e 166 rcall delay_ms
9c15f366
L
167 dec temp2
168 brne ramtestwl
169
170 printstring "reread..."
171
172;re-read RAM
173 ldiw x,0
fa9059af 174 clr temp3 ;Error counter
9c15f366 175ramtestr:
bca3519f 176 rcall dram_read
fa9059af
L
177
178; ori temp,0x04 ;simulate error
179; andi temp,0xF7
180
9c15f366
L
181 mov temp2,xh
182 eor temp2,xl
183 cp temp,temp2
184 breq ramtestrok
fa9059af
L
185 tst temp3
186 brne ramtestr1
187 printnewline
188 printstring "Addr xx yy "
189ramtestr1:
190 printnewline
825ecc9d 191 mov zl,temp
fa9059af
L
192 movw temp,x
193 rcall printhexw
194 rcall printspace
9c15f366
L
195 mov temp,xh
196 eor temp,xl
fa9059af 197 mov temp2,temp
9c15f366 198 rcall printhex
fa9059af 199 rcall printspace
825ecc9d 200 mov temp,zl
fa9059af
L
201 rcall printhex
202 rcall printspace
203 mov temp,temp2
825ecc9d 204 eor temp,zl
fa9059af
L
205 and temp,temp2
206 rcall printxbits
207 rcall printspace
208 mov temp,temp2
825ecc9d 209 eor temp,zl
fa9059af
L
210 com temp2
211 and temp,temp2
212 rcall printxbits
213
214 inc temp3
215 cpi temp3,16 ;
216 brsh ramtestrex
9c15f366
L
217ramtestrok:
218 adiw xl,1
219 brcc ramtestr
fa9059af
L
220ramtestrex:
221 tst temp3 ;any errors?
222 breq ramtestend
223
224 printstring " System halted!"
225halted_loop:
226 rjmp halted_loop ;keep AVR in an endless loop
227
e8384f88
L
228printxbits:
229 push temp2
230 push temp3
231 mov temp2,temp
232 ldi temp3,8
233prntxb0:
234 ldi temp,'-'
235 lsl temp2
236 brcc prntxb1
237 ldi temp,'X'
238prntxb1:
239 rcall uartPutc
240 dec temp3
241 brne prntxb0
242 pop temp3
243 pop temp2
244 ret
245
fa9059af 246ramtestend:
9c15f366
L
247
248.endif
249
250.if MEMFILL
251 ldiw x,0
02d57479 252 ldi temp,MEMFILL_VAL
9c15f366 253ramfillw:
4675c141 254 rcall dram_write_pp
9c15f366
L
255 brcc ramfillw
256.endif
257
258
259;----------------------------------------------------------------------------
260
261boot_again:
262 printnewline
dd7aea8c 263 printstring "Initing mmc..."
64219415 264 printnewline
b741422e 265 lcall mgr_init_partitions
9c15f366
L
266
267 cbr temp,0x80
268 brne boot_ipl2
269 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
64219415 270 printnewline
9c15f366
L
271 ldi temp2,18
272boot_iplwl:
273 ldi temp,255
274 rcall delay_ms
275 dec temp2
276 brne boot_iplwl
277 rjmp boot_again
278
279
280boot_ipl2:
b741422e 281 lcall mgr_prnt_parttbl
9c15f366
L
282 printnewline
283 printstring "Partinit done."
dd7aea8c 284
2ccaac16
L
285; Init (de)blocking buffer
286
287 lcall dsk_inval_hostbuf
678fc0b0 288
4675c141 289; Read first sector of first CP/M partition (ipl)
9c15f366 290
e8384f88
L
291 ldiw y,fsys_vars
292
92202636 293; Disk 0
e8384f88 294 std y+o_seekdsk,_0
92202636 295; Track 0
e8384f88
L
296 std y+o_seektrk, _0
297 std y+o_seektrk+1,_0
92202636 298; Sector 0
e8384f88 299 std y+o_seeksec,_0
92202636 300
dd7aea8c
L
301; Destination
302 ldiw x,IPLADDR
e8384f88
L
303 std y+o_dmaadr+0,xl
304 std y+o_dmaadr+1,xh
dd7aea8c 305
2ccaac16
L
306 ldi temp,1<<READ_FUNC
307 lcall dskDoIt
9c15f366 308
dd7aea8c 309; lift off
b741422e 310 ljmp z80_init
9c15f366
L
311
312
e8384f88 313; vim:set ts=8 noet nowrap
fa9059af 314