]> cloudbase.mooo.com Git - avrcpm.git/blob - avrcpm/avr/init.asm
f38883e824683db1d6a4f07315ec1d8e3b1bedf3
[avrcpm.git] / avrcpm / avr / init.asm
1 ; Various functions: init, (RAM) disk, mmc, timer
2 ;
3 ; Copyright (C) 2010 Sprite_tm
4 ; Copyright (C) 2010 Leo C.
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
29 regval_tab:
30 .db 0,0
31 .db 0xFE,0xFC ; _RAS0 _CAS0
32 .db 0xF8,0xF4 ; _OE _WE
33 .db 255,0 ; _255 _0
34 regval_tab_e:
35
36 start:
37 ldi temp,low(RAMEND) ; top of memory
38 out SPL,temp ; init stack pointer
39 ldi temp,high(RAMEND) ; top of memory
40 out SPH,temp ; init stack pointer
41
42 ; - Load some registers with constant values
43
44 ldiw z,regval_tab*2
45 ldiw y,0
46 cp_l: lpm xh,z+
47 st y+,xh
48 cpi zl,low(regval_tab_e*2)
49 brne cp_l
50
51 ; - Kill wdt
52
53 wdr
54 out MCUSR,_0
55
56 ldi temp,(1<<WDCE) | (1<<WDE)
57 outm8 WDTCSR,temp
58 ldi temp,(1<<WDCE)
59 outm8 WDTCSR,temp
60
61 ; - Setup Ports
62
63 ldi temp,(1<<PUD) ;disable pullups
64 outm8 P_PUD,temp
65 out PORTD,_255 ;all pins high
66 out PORTB,_255
67 out PORTC,_255
68 out DDRD,_255 ; all outputs
69 out DDRB,_255
70 out DDRC,_255
71 cbi P_RXD-1,RXD ; RXD pin is input
72
73 outm8 TIMSK1,_0
74 outm8 TIMSK2,_0
75 outm8 TCCR2A,_0
76 outm8 TCCR2B,_0
77
78 ; - Clear RAM
79
80 ldiw z,SRAM_START
81 ldi temp2,high(ramtop)
82 clr_l:
83 st z+,_0
84 cpi zl,low(ramtop)
85 cpc zh,temp2
86 brne clr_l
87
88
89 ; Init clock/timer system
90
91 ; Init timer 1 as 1 ms system clock tick.
92
93 ldi temp, low (F_CPU/1000)
94 ldi temp2,high(F_CPU/1000)
95 outm8 OCR1BH,temp2
96 outm8 OCR1BL,temp
97 ldi temp,(1<<ICNC1)|(1<<CS10) ;Noise cancel, fall. edge, Normal Mode, clk/1
98 outm8 TCCR1B,temp
99 inm8 temp,TIMSK1
100 ori temp,(1<<OCIE1B) ;Enable 1ms int.
101 outm8 TIMSK1,temp
102
103 ; - Init serial port
104
105 rcall uart_init
106
107 ;Init timer2. Refresh-call should happen every (8ms/512)=312 cycles.
108
109 ldi temp,REFR_CNT*2 ; 2 cycles per int
110 outm8 OCR2A,temp
111 inm8 temp,TCCR2A
112 ori temp,(1<<WGM21) ;CTC mode
113 outm8 TCCR2A,temp
114 inm8 temp,TCCR2B
115 ori temp,REFR_CS ;clk/REFR_PRE
116 outm8 TCCR2B,temp
117 inm8 temp,TIMSK2
118 ori temp, (1<<OCIE2A)
119 outm8 TIMSK2,temp
120
121 sei
122
123
124 .if BOOTWAIT
125 ldi temp,10
126 call delay_ms
127
128 .endif
129
130 rcall printstr
131 .db 13,13,"CPM on an AVR, v"
132 db_version VMAJOR, VMINOR
133
134 .if MEMTEST
135 printnewline
136 printstring "Testing RAM: fill..."
137
138 ;Fill RAM
139 ldiw x,0
140 ramtestw:
141 mov temp,xh
142 eor temp,xl
143 rcall dram_write_pp
144 brcc ramtestw
145 printstring "wait..."
146
147 ldi temp2,8
148 ramtestwl:
149 ldi temp,255
150 call delay_ms
151 dec temp2
152 brne ramtestwl
153
154 printstring "reread..."
155
156 ;re-read RAM
157 ldiw x,0
158 clr temp3
159 ramtestr:
160 mem_read
161 mov temp2,xh
162 eor temp2,xl
163 cp temp,temp2
164 breq ramtestrok
165 push temp
166 cpi temp3,0 ;if first error
167 brne ramtest2
168 inc temp3
169 ldi temp,10 ;newline
170 rcall uartPutc
171 ramtest2:
172 ldi temp,13 ;return
173 rcall uartPutc
174 pop temp
175 rcall printhex
176 ldi temp,'<'
177 rcall uartPutc
178 mov temp,xh
179 eor temp,xl
180 rcall printhex
181 ldi temp,'@'
182 rcall uartPutc
183 movw temp,x
184 rcall printhexw
185 ramtestrok:
186 adiw xl,1
187 brcc ramtestr
188
189 .endif
190
191 .if MEMFILL
192 ldiw x,0
193 ramfillw:
194 ldi temp,MEMFILL_VAL
195 rcall dram_write_pp
196 brcc ramfillw
197 .endif
198
199
200 ;----------------------------------------------------------------------------
201
202 boot_again:
203 printnewline
204 printstring "Initing mmc...",0
205 printnewline
206 call mgr_init_partitions
207
208 cbr temp,0x80
209 brne boot_ipl2
210 printstring "No bootable CP/M disk found! Please change MMC/SD-Card."
211 printnewline
212 ldi temp2,18
213 boot_iplwl:
214 ldi temp,255
215 rcall delay_ms
216 dec temp2
217 brne boot_iplwl
218 rjmp boot_again
219
220
221 boot_ipl2:
222 call mgr_prnt_parttbl
223 printnewline
224 printstring "Partinit done."
225
226 rcall dsk_cboot ;init (de)blocking buffer
227
228 ; Read first sector of first CP/M partition (ipl)
229
230 lds xl,hostparttbl+1
231 lds xh,hostparttbl+2
232 lds yl,hostparttbl+3
233 lds yh,hostparttbl+4
234 rcall mmcReadSect
235
236 ; rcall dsk_cboot ;init (de)blocking buffer
237
238 ;First sector of disk or first CP/M partition is in hostbuf.
239
240 ;Save to Z80 RAM (only 128 bytes because that's retro)
241 ldiw z,hostbuf
242 ldiw x,IPLADDR
243 iplwriteloop:
244 ld temp,z+
245 rcall dram_write_pp
246 cpi zl,low(hostbuf+128)
247 brne iplwriteloop
248 cpi zh,high(hostbuf+128)
249 brne iplwriteloop
250 jmp z80_init
251
252