OSDN Git Service

V4.0
[fast-forth/master.git] / MSP430-FORTH / CHNGBAUD.f
1 \ -*- coding: utf-8 -*-
2 \
3 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
4 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
5 \ MSP_EXP430FR4133  MSP_EXP430FR2433    CHIPSTICK_FR2433    MSP_EXP430FR2355
6 \ LP_MSP430FR2476
7 \
8 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
9 \
10 \ OR
11 \
12 \ drag and drop this file onto SendSourceFileToTarget.bat
13 \ then select your TARGET when asked.
14 \
15 \ COLD            \ uncomment for this TEST which must not disrupt the downloading process
16
17 ; ------------
18 ; CHNGBAUD.f
19 ; ------------
20
21 \ first, we do some tests allowing the download
22     CODE I2CTERM_ABORT
23     SUB #4,PSP
24     MOV TOS,2(PSP)
25     MOV &KERNEL_ADDON,TOS
26     BIT #$3C00,TOS          \ BIT13|BIT12|BIT11|BIT10 test (UART TERMINAL test)
27     0<> IF MOV #0,TOS THEN  \ if TOS <> 0 (UART TERMINAL), set TOS = 0
28     MOV TOS,0(PSP)
29     MOV &VERSION,TOS
30     SUB #400,TOS            \ FastForth V4.0
31     COLON
32     $0D EMIT                \ return to column 1 without CR
33     ABORT" FastForth V4.0 please!"
34     ABORT" <-- Ouch! unexpected I2C_FastForth target!"
35     RST_RET             \ remove ABORT_UARTI2CS definition before resuming
36     ;
37
38 I2CTERM_ABORT
39
40 ; ------------------------------------------------------------------
41 ; first we download the set of definitions we need (from CORE_ANS.f)
42 ; ------------------------------------------------------------------
43
44     [UNDEFINED] DUP [IF]    \ define DUP and DUP?
45 \ https://forth-standard.org/standard/core/DUP
46 \ DUP      x -- x x      duplicate top of stack
47     CODE DUP
48 BW1 SUB #2,PSP      \ 2  push old TOS..
49     MOV TOS,0(PSP)  \ 3  ..onto stack
50     MOV @IP+,PC     \ 4
51     ENDCODE
52
53 \ https://forth-standard.org/standard/core/qDUP
54 \ ?DUP     x -- 0 | x x    DUP if nonzero
55     CODE ?DUP
56     CMP #0,TOS      \ 2  test for TOS nonzero
57     0<> ?GOTO BW1   \ 2
58     MOV @IP+,PC     \ 4
59     ENDCODE
60     [THEN]
61
62     [UNDEFINED] DROP [IF]
63 \ https://forth-standard.org/standard/core/DROP
64 \ DROP     x --          drop top of stack
65     CODE DROP
66     MOV @PSP+,TOS   \ 2
67     MOV @IP+,PC     \ 4
68     ENDCODE
69     [THEN]
70
71     [UNDEFINED] OVER [IF]
72 \ https://forth-standard.org/standard/core/OVER
73 \ OVER    x1 x2 -- x1 x2 x1
74     CODE OVER
75     MOV TOS,-2(PSP)     \ 3 -- x1 (x2) x2
76     MOV @PSP,TOS        \ 2 -- x1 (x2) x1
77     SUB #2,PSP          \ 1 -- x1 x2 x1
78     MOV @IP+,PC
79     ENDCODE
80     [THEN]
81
82     [UNDEFINED] CR [IF]
83 \ https://forth-standard.org/standard/core/CR
84 \ CR      --               send CR+LF to the output device
85
86 \    DEFER CR    \ DEFERed definition, by default executes that of :NONAME
87     CODE CR
88     MOV #NEXT_ADR,PC    \ compile same as DEFER
89     ENDCODE
90
91     :NONAME
92     'CR' EMIT 'LF' EMIT
93     ; IS CR
94     [THEN]
95
96     [UNDEFINED] 1+ [IF]
97 \ https://forth-standard.org/standard/core/OnePlus
98 \ 1+      n1/u1 -- n2/u2       add 1 to TOS
99     CODE 1+
100     ADD #1,TOS
101     MOV @IP+,PC
102     ENDCODE
103     [THEN]
104
105     [UNDEFINED] U/ [IF]
106 \ U/   u1 u2 -- q   unsigned 16/16->q16
107     CODE U/
108     SUB #2,PSP
109     MOV #0,0(PSP)   \ -- u1lo u1hi u2
110     CALL #MUSMOD    \ -- r qlo qhi
111     MOV @PSP,TOS    \ -- r qlo qlo
112     ADD #4,PSP      \ -- qlo
113     MOV @IP+,PC
114     ENDCODE
115     [THEN]
116
117     [UNDEFINED] >R [IF]
118 \ https://forth-standard.org/standard/core/toR
119 \ >R    x --   R: -- x   push to return stack
120     CODE >R
121     PUSH TOS
122     MOV @PSP+,TOS
123     MOV @IP+,PC
124     ENDCODE
125     [THEN]
126
127     [UNDEFINED] R> [IF]
128 \ https://forth-standard.org/standard/core/Rfrom
129 \ R>    -- x    R: x --   pop from return stack ; CALL #RFROM performs DOVAR
130     CODE R>
131     SUB #2,PSP      \ 1
132     MOV TOS,0(PSP)  \ 3
133     MOV @RSP+,TOS   \ 2
134     MOV @IP+,PC     \ 4
135     ENDCODE
136     [THEN]
137
138     [UNDEFINED] = [IF]
139 \ https://forth-standard.org/standard/core/Equal
140 \ =      x1 x2 -- flag         test x1=x2
141     CODE =
142     SUB @PSP+,TOS   \ 2
143     0<> IF          \ 2
144         AND #0,TOS  \ 1 flag Z = 1
145     ELSE            \ 2
146         XOR #-1,TOS \ 1
147     THEN
148     MOV @IP+,PC     \ 4
149     ENDCODE
150     [THEN]
151
152     [UNDEFINED] < [IF]  \ define < and >
153 \ https://forth-standard.org/standard/core/less
154 \ <      n1 n2 -- flag        test n1<n2, signed
155     CODE <
156     SUB @PSP+,TOS   \ 1 TOS=n2-n1
157     S< ?GOTO FW1    \ 2 signed
158     0<> IF          \ 2
159 BW1 MOV #-1,TOS \ 1 flag Z = 0
160     THEN
161     MOV @IP+,PC
162     ENDCODE
163
164 \ https://forth-standard.org/standard/core/more
165 \ >     n1 n2 -- flag         test n1>n2, signed
166     CODE >
167     SUB @PSP+,TOS   \ 2 TOS=n2-n1
168     S< ?GOTO BW1    \ 2 --> +5
169 FW1 AND #0,TOS      \ 1 flag Z = 1
170     MOV @IP+,PC
171     ENDCODE
172     [THEN]
173
174     [UNDEFINED] IF [IF] \ define IF THEN
175 \ https://forth-standard.org/standard/core/IF
176 \ IF       -- IFadr    initialize conditional forward branch
177     CODE IF
178     SUB #2,PSP          \
179     MOV TOS,0(PSP)      \
180     MOV &DP,TOS         \ -- HERE
181     ADD #4,&DP          \           compile one word, reserve one word
182     MOV #QFBRAN,0(TOS)  \ -- HERE   compile QFBRAN
183     ADD #2,TOS          \ -- HERE+2=IFadr
184     MOV @IP+,PC
185     ENDCODE IMMEDIATE
186
187 \ https://forth-standard.org/standard/core/THEN
188 \ THEN     IFadr --                resolve forward branch
189     CODE THEN
190     MOV &DP,0(TOS)          \ -- IFadr
191     MOV @PSP+,TOS           \ --
192     MOV @IP+,PC
193     ENDCODE IMMEDIATE
194     [THEN]
195
196     [UNDEFINED] ELSE [IF]
197 \ https://forth-standard.org/standard/core/ELSE
198 \ ELSE     IFadr -- ELSEadr        resolve forward IF branch, leave ELSEadr on stack
199     CODE ELSE
200     ADD #4,&DP              \ make room to compile two words
201     MOV &DP,W               \ W=HERE+4
202     MOV #BRAN,-4(W)
203     MOV W,0(TOS)            \ HERE+4 ==> [IFadr]
204     SUB #2,W                \ HERE+2
205     MOV W,TOS               \ -- ELSEadr
206     MOV @IP+,PC
207     ENDCODE IMMEDIATE
208     [THEN]
209
210     [UNDEFINED] DO [IF] \ define DO LOOP +LOOP
211 \ https://forth-standard.org/standard/core/DO
212 \ DO       -- DOadr   L: -- 0
213     HDNCODE XDO         \ DO run time
214     MOV #$8000,X        \ 2 compute 8000h-limit = "fudge factor"
215     SUB @PSP+,X         \ 2
216     MOV TOS,Y           \ 1 loop ctr = index+fudge
217     ADD X,Y             \ 1 Y = INDEX
218     PUSHM #2,X          \ 4 PUSHM X,Y, i.e. PUSHM LIMIT, INDEX
219     MOV @PSP+,TOS       \ 2
220     MOV @IP+,PC         \ 4
221     ENDCODE
222
223     CODE DO
224     SUB #2,PSP              \
225     MOV TOS,0(PSP)          \
226     ADD #2,&DP              \   make room to compile xdo
227     MOV &DP,TOS             \ -- HERE+2
228     MOV #XDO,-2(TOS)        \   compile xdo
229     ADD #2,&LEAVEPTR        \ -- HERE+2     LEAVEPTR+2
230     MOV &LEAVEPTR,W         \
231     MOV #0,0(W)             \ -- HERE+2     L-- 0
232     MOV @IP+,PC
233     ENDCODE IMMEDIATE
234
235 \ https://forth-standard.org/standard/core/LOOP
236 \ LOOP    DOadr --         L-- an an-1 .. a1 0
237     HDNCODE XLOOP       \   LOOP run time
238     ADD #1,0(RSP)       \ 4 increment INDEX
239 BW1 BIT #$100,SR        \ 2 is overflow bit set?
240     0= IF               \   branch if no overflow
241         MOV @IP,IP
242         MOV @IP+,PC
243     THEN
244     ADD #4,RSP          \ 1 empties RSP
245     ADD #2,IP           \ 1 overflow = loop done, skip branch ofs
246     MOV @IP+,PC         \ 4 14~ taken or not taken xloop/loop
247     ENDCODE             \
248
249     CODE LOOP
250     MOV #XLOOP,X
251 BW2 ADD #4,&DP              \ make room to compile two words
252     MOV &DP,W
253     MOV X,-4(W)             \ xloop --> HERE
254     MOV TOS,-2(W)           \ DOadr --> HERE+2
255     BEGIN                   \ resolve all "leave" adr
256         MOV &LEAVEPTR,TOS   \ -- Adr of top LeaveStack cell
257         SUB #2,&LEAVEPTR    \ --
258         MOV @TOS,TOS        \ -- first LeaveStack value
259         CMP #0,TOS          \ -- = value left by DO ?
260     0<> WHILE
261         MOV W,0(TOS)        \ move adr after loop as UNLOOP adr
262     REPEAT
263     MOV @PSP+,TOS
264     MOV @IP+,PC
265     ENDCODE IMMEDIATE
266
267 \ https://forth-standard.org/standard/core/PlusLOOP
268 \ +LOOP   adrs --   L-- an an-1 .. a1 0
269     HDNCODE XPLOO   \   +LOOP run time
270     ADD TOS,0(RSP)  \ 4 increment INDEX by TOS value
271     MOV @PSP+,TOS   \ 2 get new TOS, doesn't change flags
272     GOTO BW1        \ 2
273     ENDCODE         \
274
275     CODE +LOOP
276     MOV #XPLOO,X
277     GOTO BW2        \ goto BW1 LOOP
278     ENDCODE IMMEDIATE
279     [THEN]
280
281     [UNDEFINED] CASE [IF]   \ define CASE OF ENDOF ENDCASE
282 \ https://forth-standard.org/standard/core/CASE
283     : CASE 0 ; IMMEDIATE \ -- #of-1
284
285 \ https://forth-standard.org/standard/core/OF
286     : OF \ #of-1 -- orgOF #of
287     1+                      \ count OFs
288     >R                      \ move off the stack in case the control-flow stack is the data stack.
289     POSTPONE OVER POSTPONE = \ copy and test case value
290     POSTPONE IF             \ add orig to control flow stack
291     POSTPONE DROP               \ discards case value if =
292     R>                      \ we can bring count back now
293     ; IMMEDIATE
294
295 \ https://forth-standard.org/standard/core/ENDOF
296     : ENDOF \ orgOF #of -- orgENDOF #of
297     >R                      \ move off the stack in case the control-flow stack is the data stack.
298     POSTPONE ELSE
299     R>                      \ we can bring count back now
300     ; IMMEDIATE
301
302 \ https://forth-standard.org/standard/core/ENDCASE
303     : ENDCASE \ orgENDOF1..orgENDOFn #of --
304     POSTPONE DROP
305     0 DO
306         POSTPONE THEN
307     LOOP
308     ; IMMEDIATE
309     [THEN]
310
311 ; --------------------------
312 ; end of definitions we need
313 ; --------------------------
314
315     [UNDEFINED] S_ [IF]
316     CODE S_             \           Squote alias with blank instead quote separator
317     SUB #2,PSP
318     MOV TOS,0(PSP)
319     MOV #'SP',TOS
320     MOV #S"+10,PC       \           addr S" + 10 --> PC
321     ENDCODE IMMEDIATE
322     [THEN]
323
324     [UNDEFINED] ESC [IF]
325     CODE ESC
326     CMP #0,&STATEADR
327     0= IF MOV @IP+,PC   \ interpret time use is disallowed
328     THEN
329     COLON
330     $1B                 \ -- char escape
331     POSTPONE LITERAL    \ compile-time code : lit $1B
332     POSTPONE EMIT       \ compile-time code : EMIT
333     POSTPONE S_         \ compile-time code : S_ <escape_sequence>
334     POSTPONE TYPE       \ compile-time code : TYPE
335     ; IMMEDIATE
336     [THEN]
337
338     : BAD_MHz
339     $20 DUP EMIT
340             ABORT" only for 1,4,8,16,24 MHz MCLK!"
341     ;
342
343     : OVR_BAUDS
344     $20 DUP EMIT ESC [7m    \ set reverse video
345             ." with MCLK = " FREQ_KHZ @ 1000 U/ .
346             ABORT" MHz? don't dream!"
347     ;
348
349     : CHNGBAUD                  \ only for 1, 4, 8, 16, 24 MHz
350     RST_RET                     \ removes this created word (garbage collector)
351     ECHO
352     ESC [8;42;80t      \ set 42L * 80C terminal display
353     41 0 DO CR LOOP     \ to avoid erasing any line of source, create 42-1 empty lines
354     ESC [H              \ cursor home
355
356     FREQ_KHZ @ DUP >R               \ r-- target MCLCK frequency in MHz
357     ." target MCLK = " 1000 U/ . ." MHz" CR
358     ." choose your baudrate:" CR
359     ."  0 --> 6 MBds" CR        \ >= 24 MHz
360     ."  1 --> 5 MBds" CR        \ >= 20 MHz
361     ."  2 --> 4 MBds" CR        \ >= 16 MHz
362     ."  3 --> 3 MBds" CR        \ >= 12 MHz
363     ."  4 --> 1843200 Bds" CR   \ >= 8 MHz
364     ."  5 --> 921600 Bds" CR    \ >= 4 MHz
365     ."  6 --> 460800 Bds" CR    \ >= 2 MHz
366     ."  7 --> 230400 Bds" CR    \ >= 1 MHz
367     ."  8 --> 115200 Bds" CR    \ >= 500 kHz
368     ."  9 --> 38400 Bds" CR
369     ."  A --> 19200 Bds" CR
370     ."  B --> 9600 Bds" CR
371     ." other --> abort" CR
372     ." your choice: "
373     KEY
374     CASE
375     #48 OF  ." 6 MBds"          \ add this to the current line
376             R> CASE
377                 #24000 OF $4 $0 \ -- TERM_BRW  TERM_MCTLW
378                     ENDOF
379                 24000 <
380                 IF OVR_BAUDS    \ < 24 MHz --> abort
381                 THEN BAD_MHz    \ other MHz --> abort
382             ENDCASE
383         ENDOF
384     #49 OF  ." 5 MBds"
385             R> CASE
386                 #24000 OF $4 $EE00  ENDOF
387                 #20000 OF $4 $0     ENDOF
388                 20000 <
389                 IF OVR_BAUDS    \ < 20 MHz --> abort
390                 THEN BAD_MHz    \ other MHz --> abort
391             ENDCASE
392         ENDOF
393     #50 OF  ." 4 MBds"
394             R> CASE
395                 #24000 OF $6 $0     ENDOF
396                 #20000 OF $5 $0     ENDOF
397                 #16000 OF $4 $0     ENDOF
398                 16000 <
399                 IF OVR_BAUDS    \ < 16 MHz --> abort
400                 THEN BAD_MHz    \ other MHz --> abort
401             ENDCASE
402         ENDOF
403     #51 OF  ." 3 MBds"
404             R> CASE
405                 #24000 OF $8 $0     ENDOF
406                 #20000 OF $6 $D600  ENDOF
407                 #16000 OF $5 $4900  ENDOF
408                 #12000 OF $4 $0     ENDOF
409                 12000 <
410                 IF OVR_BAUDS    \ < 12 MHz --> abort
411                 THEN BAD_MHz    \ other MHz --> abort
412             ENDCASE
413         ENDOF
414     #52 OF  ." 1843200 Bds"
415             R> CASE
416                 #24000 OF $0D $0200 ENDOF
417                 #20000 OF $0A $DF00 ENDOF
418                 #16000 OF $8 $D600  ENDOF
419                 #12000 OF $6 $AA00  ENDOF
420                 #8000  OF $5 $9200  ENDOF
421                 8000 <
422                 IF OVR_BAUDS    \ < 8 MHz --> abort
423                 THEN BAD_MHz    \ other MHz --> abort
424             ENDCASE
425         ENDOF
426     #53 OF  ." 921600 Bds"
427             R> CASE
428                 #24000 OF $1 $00A1  ENDOF
429                 #20000 OF $1 $B751  ENDOF
430                 #16000 OF $11 $4A00 ENDOF
431                 #12000 OF $0D $0200  ENDOF
432                 #8000  OF $8 $D600  ENDOF
433                 #4000  OF $4 $4900  ENDOF
434                 4000 <
435                 IF OVR_BAUDS    \ < 4 MHz --> abort
436                 THEN BAD_MHz    \ other MHz --> abort
437             ENDCASE
438         ENDOF
439     #54 OF  ." 460800 Bds"
440             R> CASE
441                 #24000 OF $3 $0241  ENDOF
442                 #20000 OF $2 $92B1  ENDOF
443                 #16000 OF $2 $BB21  ENDOF
444                 #12000 OF $1 $00A1  ENDOF
445                 #8000  OF $11 $4A00 ENDOF
446                 #4000  OF $8 $D600  ENDOF
447                 #2000  OF $4 $4900  ENDOF
448                 2000 <
449                 IF OVR_BAUDS    \ < 2 MHz --> abort
450                 THEN BAD_MHz    \ other MHz --> abort
451             ENDCASE
452         ENDOF
453     #55 OF  ." 230400 Bds"
454             R> CASE
455                 #24000 OF $6 $2081  ENDOF
456                 #20000 OF $5 $EE61  ENDOF
457                 #16000 OF $4 $5551  ENDOF
458                 #12000 OF $3 $0241  ENDOF
459                 #8000  OF $2 $BB21  ENDOF
460                 #4000  OF $11 $4A00 ENDOF
461                 #2000  OF $8 $D600  ENDOF
462                 #1000  OF $4 $4900  ENDOF
463                 1000 <
464                 IF OVR_BAUDS    \ < 1 MHz --> abort
465                 THEN BAD_MHz    \ other MHz --> abort
466             ENDCASE
467         ENDOF
468     #56 OF  ." 115200 Bds"
469             R> CASE
470                 #24000 OF $0D $4901 ENDOF
471                 #20000 OF $0A $AD01 ENDOF
472                 #16000 OF $8 $F7A1  ENDOF
473                 #12000 OF $6 $2081  ENDOF
474                 #8000  OF $4 $5551  ENDOF
475                 #4000  OF $2 $BB21  ENDOF
476                 #2000  OF $11 $4A00 ENDOF
477                 #1000  OF $8 $D600  ENDOF
478                 #500   OF $4 $4900  ENDOF
479                 500 <
480                 IF OVR_BAUDS    \ < 500 Khz --> abort
481                 THEN BAD_MHz    \ other MHz --> abort
482             ENDCASE
483         ENDOF
484     #57 OF  ." 38400 Bds"
485             R> CASE
486                 #24000  OF $27 $0011    ENDOF
487                 #16000  OF $1A $D601    ENDOF
488                 #8000   OF $0D $4901    ENDOF
489                 #4000   OF $6 $2081     ENDOF
490                 #1000   OF $1 $00A1     ENDOF
491                 BAD_MHz    \ other MHz --> abort
492             ENDCASE
493         ENDOF
494     #65 OF  ." 19200 Bds"
495             R> CASE
496                 #24000  OF $4E $0021    ENDOF
497                 #16000  OF $34 $4911    ENDOF
498                 #8000   OF $1A $D601    ENDOF
499                 #4000   OF $0D $4901    ENDOF
500                 #1000   OF $3 $0241     ENDOF
501                 BAD_MHz    \ other MHz --> abort
502             ENDCASE
503         ENDOF
504     #66 OF  ." 9600 Bds"
505             R> CASE
506                 #24000  OF $9C $0041    ENDOF
507                 #16000  OF $68 $D621    ENDOF
508                 #8000   OF $34 $4911    ENDOF
509                 #4000   OF $1A $D601    ENDOF
510                 #1000   OF $6 $2081     ENDOF
511                 BAD_MHz    \ other MHz --> abort
512             ENDCASE
513         ENDOF
514         ." abort" ABORT" "      \ ABORT" " displays nothing
515     ENDCASE
516     TERMMCTLW_RST !             \ set UCAxMCTLW value in FRAM
517     TERMBRW_RST !               \ set UCAxBRW value in FRAM
518     CR ESC [7m                  \ escape sequence to set reverse video
519     ." Change baudrate in Teraterm, save its setup, then reset target."
520     ESC [0m
521     ;
522
523     CHNGBAUD