OSDN Git Service

V3.7
[fast-forth/master.git] / MSP430-FORTH / SD_TEST.f
1 \ -*- coding: utf-8 -*-
2
3 \ first, we test for downloading driver only if UART TERMINAL target
4 CODE ABORT_SD_TEST
5 SUB #2,PSP
6 MOV TOS,0(PSP)
7 MOV &VERSION,TOS
8 SUB #307,TOS        \ FastForth V3.7
9 COLON
10 'CR' EMIT            \ return to column 1 without 'LF'
11 ABORT" FastForth version = 3.7 please!"
12 PWR_STATE           \ remove ABORT_SD_TEST definition before resuming
13 ;
14
15 ABORT_SD_TEST
16
17 ; -----------
18 ; SD_TEST.f
19 ; -----------
20 \
21 \ to see kernel options, download FastForthSpecs.f
22 \ FastForth kernel options: MSP430ASSEMBLER, CONDCOMP, SD_CARD_READ_WRITE
23 \
24 \ TARGET SELECTION ( = the name of \INC\target.pat file without the extension)
25 \ MSP_EXP430FR5739  MSP_EXP430FR5969    MSP_EXP430FR5994    MSP_EXP430FR6989
26 \ MSP_EXP430FR4133  MSP_EXP430FR2433    MSP_EXP430FR2355    CHIPSTICK_FR2433
27 \ LP_MSP430FR2476
28 \
29 \ from scite editor : copy your target selection in (shift+F8) parameter 1:
30 \
31 \ OR
32 \
33 \ drag and drop this file onto SendSourceFileToTarget.bat
34 \ then select your TARGET when asked.
35 \
36 \
37 \
38 \
39 \ how to test SD_CARD driver on your launchpad:
40 \
41 \
42 \ remove the jumpers RX, TX of programming port (don't remove GND, TST, RST and VCC)
43 \ wire PL2303TA/HXD: GND <-> GND, RX <-- TX, TX --> RX
44 \ connect it to your PC on a free USB port
45 \ connect the PL2303TA/HXD cable to your PC on another free USB port
46 \ configure TERATERM as indicated in forthMSP430FR.asm
47 \
48 \
49 \ if you have a MSP-EXP430FR5994 launchpad, program it with MSP_EXP430FR5994_xbauds_SD_CARD.txt
50 \ to do, drag and drop this file onto prog.bat
51 \ nothing else to do!
52 \
53 \
54 \ else edit forthMSP430FR.asm with scite editor
55 \   uncomment your target, copy it
56 \   paste it into (SHIFT+F8) param1
57 \   set DTC .equ 1
58 \       FREQUENCY   .equ 16
59 \       THREADS     .equ 16
60 \       TERMINALBAUDRATE    .equ what_you_want
61 \         
62 \   uncomment:  CONDCOMP
63 \               MSP430ASSEMBLER
64 \               SD_CARD_LOADER
65 \               SD_CARD_READ_WRITE
66
67 \   compile for your target (CTRL+0)
68 \
69 \   program your target via TI interface (CTRL+1)
70 \
71 \   then wire your SD_Card module as described in your MSP430-FORTH\target.pat file
72 \
73 \
74 \
75 \ format FAT16 or FAT32 a SD_CARD memory (max 64GB) with "FRxxxx" in the disk name
76 \ drag and drop \MSP430_COND\MISC folder on the root of this SD_CARD memory (FastForth doesn't do yet)
77 \ put it in your target SD slot
78 \ if no reset, type COLD from the console input (teraterm) to reset FAST FORTH
79 \
80 \ with MSP430FR5xxx or MSP430FR6xxx targets, you can first set RTC:
81 \ by downloading RTC.f with SendSourceFileToTarget.bat
82 \ then terminal input asks you to type (with spaces) (DMY), then (HMS),
83 \ So, subsequent copied files will be dated:
84 \
85 \ with CopySourceFileToTarget_SD_Card.bat (or better, from scite editor, menu tools):
86 \
87 \   copy TESTASM.4TH        to \MISC\TESTASM.4TH    (add path \MISC in the window opened by TERATERM)
88 \   copy TSTWORDS.4TH       to \TSTWORDS.4TH
89 \   copy CORETEST.4TH       to \CORETEST.4TH
90 \   copy SD_TOOLS.f         to \SD_TOOLS.4TH
91 \   copy SD_TEST.f          to \SD_TEST.4TH
92 \   copy PROG100k.f         to \PROG100k.4TH
93 \   copy RTC.f              to \RTC.4TH             ( doesn't work with if FR2xxx or FR4xxx)
94
95 PWR_STATE
96
97 [DEFINED] {SD_TEST} [IF]  {SD_TEST} [THEN] \ remove it if defined out of kernel 
98
99 MARKER {SD_TEST}
100
101 [UNDEFINED] EXIT [IF]
102 \ https://forth-standard.org/standard/core/EXIT
103 \ EXIT     --      exit a colon definition; CALL #EXIT performs ASMtoFORTH (10 cycles)
104 \                                           JMP #EXIT performs EXIT
105 CODE EXIT
106 MOV @RSP+,IP    \ 2 pop previous IP (or next PC) from return stack
107 MOV @IP+,PC     \ 4 = NEXT
108                 \ 6 (ITC-2)
109 ENDCODE
110 [THEN]
111
112 [UNDEFINED] SWAP [IF]
113 \ https://forth-standard.org/standard/core/SWAP
114 \ SWAP     x1 x2 -- x2 x1    swap top two items
115 CODE SWAP
116 MOV @PSP,W      \ 2
117 MOV TOS,0(PSP)  \ 3
118 MOV W,TOS       \ 1
119 MOV @IP+,PC     \ 4
120 ENDCODE
121 [THEN]
122
123 [UNDEFINED] >BODY [IF]
124 \ https://forth-standard.org/standard/core/toBODY
125 \ >BODY     -- addr      leave BODY of a CREATEd word\ also leave default ACTION-OF primary DEFERred word
126 CODE >BODY
127 ADD #4,TOS
128 MOV @IP+,PC
129 ENDCODE
130 [THEN]
131
132 [UNDEFINED] 0= [IF]
133 \ https://forth-standard.org/standard/core/ZeroEqual
134 \ 0=     n/u -- flag    return true if TOS=0
135 CODE 0=
136 SUB #1,TOS      \ borrow (clear cy) if TOS was 0
137 SUBC TOS,TOS    \ TOS=-1 if borrow was set
138 MOV @IP+,PC
139 ENDCODE
140 [THEN]
141
142 [UNDEFINED] IF [IF]     \ define IF and THEN
143 \ https://forth-standard.org/standard/core/IF
144 \ IF       -- IFadr    initialize conditional forward branch
145 CODE IF       \ immediate
146 SUB #2,PSP              \
147 MOV TOS,0(PSP)          \
148 MOV &DP,TOS             \ -- HERE
149 ADD #4,&DP            \           compile one word, reserve one word
150 MOV #QFBRAN,0(TOS)      \ -- HERE   compile QFBRAN
151 ADD #2,TOS              \ -- HERE+2=IFadr
152 MOV @IP+,PC
153 ENDCODE IMMEDIATE
154
155 \ https://forth-standard.org/standard/core/THEN
156 \ THEN     IFadr --                resolve forward branch
157 CODE THEN               \ immediate
158 MOV &DP,0(TOS)          \ -- IFadr
159 MOV @PSP+,TOS           \ --
160 MOV @IP+,PC
161 ENDCODE IMMEDIATE
162 [THEN]
163
164 [UNDEFINED] ELSE [IF]
165 \ https://forth-standard.org/standard/core/ELSE
166 \ ELSE     IFadr -- ELSEadr        resolve forward IF branch, leave ELSEadr on stack
167 CODE ELSE     \ immediate
168 ADD #4,&DP              \ make room to compile two words
169 MOV &DP,W               \ W=HERE+4
170 MOV #BRAN,-4(W)
171 MOV W,0(TOS)            \ HERE+4 ==> [IFadr]
172 SUB #2,W                \ HERE+2
173 MOV W,TOS               \ -- ELSEadr
174 MOV @IP+,PC
175 ENDCODE IMMEDIATE
176 [THEN]
177
178 [UNDEFINED] BEGIN [IF]  \ define BEGIN UNTIL AGAIN WHILE REPEAT
179 \ https://forth-standard.org/standard/core/BEGIN
180 \ BEGIN    -- BEGINadr             initialize backward branch
181 CODE BEGIN
182     MOV #HEREXEC,PC
183 ENDCODE IMMEDIATE
184
185 \ https://forth-standard.org/standard/core/UNTIL
186 \ UNTIL    BEGINadr --             resolve conditional backward branch
187 CODE UNTIL              \ immediate
188     MOV #QFBRAN,X
189 BW1 ADD #4,&DP          \ compile two words
190     MOV &DP,W           \ W = HERE
191     MOV X,-4(W)         \ compile Bran or QFBRAN at HERE
192     MOV TOS,-2(W)       \ compile bakcward adr at HERE+2
193     MOV @PSP+,TOS
194     MOV @IP+,PC
195 ENDCODE IMMEDIATE
196
197 \ https://forth-standard.org/standard/core/AGAIN
198 \ AGAIN    BEGINadr --             resolve uncondionnal backward branch
199 CODE AGAIN     \ immediate
200 MOV #BRAN,X
201 GOTO BW1
202 ENDCODE IMMEDIATE
203
204 \ https://forth-standard.org/standard/core/WHILE
205 \ WHILE    BEGINadr -- WHILEadr BEGINadr
206 : WHILE     \ immediate
207 POSTPONE IF SWAP
208 ; IMMEDIATE
209
210 \ https://forth-standard.org/standard/core/REPEAT
211 \ REPEAT   WHILEadr BEGINadr --     resolve WHILE loop
212 : REPEAT
213 POSTPONE AGAIN POSTPONE THEN
214 ; IMMEDIATE
215 [THEN]
216
217 [UNDEFINED] DO [IF]     \ define DO LOOP +LOOP
218 \ https://forth-standard.org/standard/core/DO
219 \ DO       -- DOadr   L: -- 0
220 CODE DO                 \ immediate
221 SUB #2,PSP              \
222 MOV TOS,0(PSP)          \
223 ADD #2,&DP              \   make room to compile xdo
224 MOV &DP,TOS             \ -- HERE+2
225 MOV #XDO,-2(TOS)        \   compile xdo
226 ADD #2,&LEAVEPTR        \ -- HERE+2     LEAVEPTR+2
227 MOV &LEAVEPTR,W         \
228 MOV #0,0(W)             \ -- HERE+2     L-- 0
229 MOV @IP+,PC
230 ENDCODE IMMEDIATE
231
232 \ https://forth-standard.org/standard/core/LOOP
233 \ LOOP    DOadr --         L-- an an-1 .. a1 0
234 CODE LOOP               \ immediate
235     MOV #XLOOP,X
236 BW1 ADD #4,&DP          \ make room to compile two words
237     MOV &DP,W
238     MOV X,-4(W)         \ xloop --> HERE
239     MOV TOS,-2(W)       \ DOadr --> HERE+2
240 BEGIN                   \ resolve all "leave" adr
241     MOV &LEAVEPTR,TOS   \ -- Adr of top LeaveStack cell
242     SUB #2,&LEAVEPTR    \ --
243     MOV @TOS,TOS        \ -- first LeaveStack value
244     CMP #0,TOS          \ -- = value left by DO ?
245 0<> WHILE
246     MOV W,0(TOS)        \ move adr after loop as UNLOOP adr
247 REPEAT
248     MOV @PSP+,TOS
249     MOV @IP+,PC
250 ENDCODE IMMEDIATE
251
252 \ https://forth-standard.org/standard/core/PlusLOOP
253 \ +LOOP   adrs --   L-- an an-1 .. a1 0
254 CODE +LOOP              \ immediate
255 MOV #XPLOOP,X
256 GOTO BW1
257 ENDCODE IMMEDIATE
258 [THEN]
259
260 [UNDEFINED] I [IF]
261 \ https://forth-standard.org/standard/core/I
262 \ I        -- n   R: sys1 sys2 -- sys1 sys2
263 \                  get the innermost loop index
264 CODE I
265 SUB #2,PSP              \ 1 make room in TOS
266 MOV TOS,0(PSP)          \ 3
267 MOV @RSP,TOS            \ 2 index = loopctr - fudge
268 SUB 2(RSP),TOS          \ 3
269 MOV @IP+,PC             \ 4 13~
270 ENDCODE
271 [THEN]
272
273 [UNDEFINED] + [IF]
274 \ https://forth-standard.org/standard/core/Plus
275 \ +       n1/u1 n2/u2 -- n3/u3     add n1+n2
276 CODE +
277 ADD @PSP+,TOS
278 MOV @IP+,PC
279 ENDCODE
280 [THEN]
281
282 [UNDEFINED] - [IF]
283 \ https://forth-standard.org/standard/core/Minus
284 \ -      n1/u1 n2/u2 -- n3/u3     n3 = n1-n2
285 CODE -
286 SUB @PSP+,TOS   \ 2  -- n2-n1 ( = -n3)
287 XOR #-1,TOS     \ 1
288 ADD #1,TOS      \ 1  -- n3 = -(n2-n1) = n1-n2
289 MOV @IP+,PC
290 ENDCODE
291 [THEN]
292
293 [UNDEFINED] MAX [IF]   \ define MAX and MIN
294     CODE MAX    \    n1 n2 -- n3       signed maximum
295         CMP @PSP,TOS    \ n2-n1
296         S< ?GOTO FW1    \ n2<n1
297 BW1     ADD #2,PSP
298         MOV @IP+,PC
299     ENDCODE
300
301     CODE MIN    \    n1 n2 -- n3       signed minimum
302         CMP @PSP,TOS    \ n2-n1
303         S< ?GOTO BW1    \ n2<n1
304 FW1     MOV @PSP+,TOS
305         MOV @IP+,PC
306     ENDCODE
307 [THEN]
308
309 [UNDEFINED] C@ [IF]
310 \ https://forth-standard.org/standard/core/CFetch
311 \ C@     c-addr -- char   fetch char from memory
312 CODE C@
313 MOV.B @TOS,TOS
314 MOV @IP+,PC
315 ENDCODE
316 [THEN]
317
318 [UNDEFINED] SPACE [IF]
319 \ https://forth-standard.org/standard/core/SPACE
320 \ SPACE   --               output a space
321 : SPACE
322 $20 EMIT ;
323 [THEN]
324
325 [UNDEFINED] SPACES [IF]
326 \ https://forth-standard.org/standard/core/SPACES
327 \ SPACES   n --            output n spaces
328 CODE SPACES
329 CMP #0,TOS
330 0<> IF
331     PUSH IP
332     BEGIN
333         LO2HI
334         $20 EMIT
335         HI2LO
336         SUB #2,IP 
337         SUB #1,TOS
338     0= UNTIL
339     MOV @RSP+,IP
340 THEN
341 MOV @PSP+,TOS           \ --         drop n
342 NEXT              
343 ENDCODE
344 [THEN]
345
346 [UNDEFINED] DUP [IF]    \ define DUP and DUP?
347 \ https://forth-standard.org/standard/core/DUP
348 \ DUP      x -- x x      duplicate top of stack
349 CODE DUP
350 BW1 SUB #2,PSP      \ 2  push old TOS..
351     MOV TOS,0(PSP)  \ 3  ..onto stack
352     MOV @IP+,PC     \ 4
353 ENDCODE
354
355 \ https://forth-standard.org/standard/core/qDUP
356 \ ?DUP     x -- 0 | x x    DUP if nonzero
357 CODE ?DUP
358 CMP #0,TOS      \ 2  test for TOS nonzero
359 0<> ?GOTO BW1    \ 2
360 MOV @IP+,PC     \ 4
361 ENDCODE
362 [THEN]
363
364 [UNDEFINED] OVER [IF]
365 \ https://forth-standard.org/standard/core/OVER
366 \ OVER    x1 x2 -- x1 x2 x1
367 CODE OVER
368 MOV TOS,-2(PSP)     \ 3 -- x1 (x2) x2
369 MOV @PSP,TOS        \ 2 -- x1 (x2) x1
370 SUB #2,PSP          \ 1 -- x1 x2 x1
371 MOV @IP+,PC
372 ENDCODE
373 [THEN]
374
375 [UNDEFINED] >R [IF]
376 \ https://forth-standard.org/standard/core/toR
377 \ >R    x --   R: -- x   push to return stack
378 CODE >R
379 PUSH TOS
380 MOV @PSP+,TOS
381 MOV @IP+,PC
382 ENDCODE
383 [THEN]
384
385 [UNDEFINED] R> [IF]
386 \ https://forth-standard.org/standard/core/Rfrom
387 \ R>    -- x    R: x --   pop from return stack ; CALL #RFROM performs DOVAR
388 CODE R>
389 SUB #2,PSP      \ 1
390 MOV TOS,0(PSP)  \ 3
391 MOV @RSP+,TOS   \ 2
392 MOV @IP+,PC     \ 4
393 ENDCODE
394 [THEN]
395
396 [UNDEFINED] CONSTANT [IF]
397 \ https://forth-standard.org/standard/core/CONSTANT
398 \ CONSTANT <name>     n --                      define a Forth CONSTANT 
399 : CONSTANT 
400 CREATE
401 HI2LO
402 MOV TOS,-2(W)           \   PFA = n
403 MOV @PSP+,TOS
404 MOV @RSP+,IP
405 MOV @IP+,PC
406 ENDCODE
407 [THEN]
408
409 [UNDEFINED] STATE [IF]
410 \ https://forth-standard.org/standard/core/STATE
411 \ STATE   -- a-addr       holds compiler state
412 STATEADR CONSTANT STATE
413 [THEN]
414
415 [UNDEFINED] IS [IF]     \ define DEFER! and IS
416 \ https://forth-standard.org/standard/core/DEFERStore
417 \ Set the word xt1 to execute xt2. An ambiguous condition exists if xt1 is not for a word defined by DEFER.
418 CODE DEFER!             \ xt2 xt1 --
419 MOV @PSP+,2(TOS)        \ -- xt1=CFA_DEFER          xt2 --> [CFA_DEFER+2]
420 MOV @PSP+,TOS           \ --
421 MOV @IP+,PC
422 ENDCODE
423
424 \ https://forth-standard.org/standard/core/IS
425 \ IS <name>        xt --
426 \ used as is :
427 \ DEFER DISPLAY                         create a "do nothing" definition (2 CELLS)
428 \ inline command : ' U. IS DISPLAY      U. becomes the runtime of the word DISPLAY
429 \ or in a definition : ... ['] U. IS DISPLAY ...
430 \ KEY, EMIT, CR, ACCEPT and WARM are examples of DEFERred words
431 : IS
432 STATE @
433 IF  POSTPONE ['] POSTPONE DEFER! 
434 ELSE ' DEFER! 
435 THEN
436 ; IMMEDIATE
437 [THEN]
438
439 [UNDEFINED] U.R [IF]        \ defined in {UTILITY}
440 : U.R                       \ u n --           display u unsigned in n width (n >= 2)
441 >R  <# 0 # #S #>  
442 R> OVER - 0 MAX SPACES TYPE
443 ;
444 [THEN]
445
446 [UNDEFINED] DUMP [IF]       \ defined in {UTILITY}
447 \ https://forth-standard.org/standard/tools/DUMP
448 CODE DUMP                   \ adr n  --   dump memory
449 PUSH IP
450 PUSH &BASEADR               \ save current base
451 MOV #$10,&BASEADR           \ HEX base
452 ADD @PSP,TOS                \ -- ORG END
453 LO2HI
454   SWAP                      \ -- END ORG
455   DO  CR                    \ generate line
456     I 4 U.R SPACE           \ generate address
457       I 8 + I
458       DO I C@ 3 U.R LOOP
459       SPACE
460       I $10 + I 8 +
461       DO I C@ 3 U.R LOOP  
462       SPACE SPACE
463       I $10 + I             \ display 16 chars
464       DO I C@ $7E MIN $20 MAX EMIT LOOP
465   $10 +LOOP
466   R> BASEADR !              \ restore current base
467 ;
468 [THEN]
469
470 [UNDEFINED] HERE [IF]
471 CODE HERE
472 MOV #BEGIN,PC
473 ENDCODE
474 [THEN]
475
476
477 \ SD_EMIT  c --    output char c to a SD_CARD file opened as write
478 CODE SD_EMIT
479 CMP #512,&BufferPtr     \ 512 bytes by sector
480 U>= IF                  \ if file buffer is full
481     MOV #WRITE,X        \ CALL #Write_File
482     CALL 2(X)           \ BufferPtr = 0
483 THEN
484 MOV &BufferPtr,Y        \ 3 
485 MOV.B TOS,SD_BUF(Y)     \ 3
486 ADD #1,&BufferPtr       \ 4
487 MOV @PSP+,TOS           \ 2
488 MOV @IP+,PC
489 ENDCODE
490
491 : SD_TEST
492 PWR_HERE    \ remove all volatile programs from MAIN memory
493 CR
494 ." 0 Set date and time" CR
495 ." 1 Load {TOOLS} words" CR
496 ." 2 Load {SD_TOOLS} words" CR
497 ." 3 Load {CORE_COMP} words" CR
498 ." 4 Load ANS core tests" CR
499 ." 5 Load a 100k program " CR
500 ." 6 Read only this source file" CR
501 ." 7 append a dump of FORTH to YOURFILE.TXT" CR
502 ." 8 delete YOURFILE.TXT" CR
503 ." 9 Load TST_WORDS" CR
504 ." your choice : "
505 KEY
506 48 - ?DUP
507 0= IF
508     ." LOAD RTC.4TH" CR
509     LOAD" RTC.4TH"
510 ELSE 1 - ?DUP
511     0= IF
512         ." LOAD UTILITY.4TH" CR
513         LOAD" UTILITY.4TH"
514     ELSE 1 - ?DUP
515         0= IF
516             ." LOAD SD_TOOLS.4TH" CR
517             LOAD" SD_TOOLS.4TH"
518         ELSE 1 - ?DUP
519             0= IF
520                 ." LOAD CORE_ANS.4TH" CR
521                 LOAD" CORE_ANS.4TH"
522             ELSE 1 - ?DUP
523                 0= IF
524                     ." LOAD CORETEST.4TH" CR
525                     LOAD" CORETEST.4TH"
526                     PWR_STATE
527                 ELSE 1 - ?DUP
528                     0= IF
529                         ." LOAD PROG100K.4TH" CR
530                         NOECHO
531                         LOAD" PROG100K.4TH"
532                     ELSE 1 - ?DUP
533                         0= IF
534                             ." READ PROG100K.4TH" CR
535                             READ" PROG100K.4TH"
536                             BEGIN
537                                 READ    \ sequentially read 512 bytes
538                             UNTIL       \ prog10k.4TH is closed
539                         ELSE 1 - ?DUP
540                             0= IF
541                                 ." WRITE YOURFILE.TXT" CR
542                                 WRITE" YOURFILE.TXT"
543                                 ['] SD_EMIT IS EMIT
544 \                                ." va te faire voir"
545                                 MAIN_ORG HERE OVER - DUMP
546                                 ['] EMIT >BODY IS EMIT
547                                 CLOSE
548                             ELSE 1 - ?DUP
549                                 0= IF
550                                     ." DEL YOURFILE.TXT" CR
551                                     DEL" YOURFILE.TXT"
552                                 ELSE 1 - ?DUP
553                                     0= IF
554                                         ." LOAD TSTWORDS.4TH" CR
555                                         LOAD" TSTWORDS.4TH"
556                                     ELSE
557                                         ." abort" ABORT" "
558                                     THEN                                        
559                                 THEN
560                             THEN
561                         THEN
562                     THEN
563                 THEN
564             THEN
565         THEN
566     THEN
567 THEN
568 ;
569
570
571
572 RST_HERE
573
574 [THEN]
575
576 ECHO SD_TEST