home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / aforth-1.4.lha / AForth-1.4 / progs / datafile.f < prev    next >
Encoding:
Text File  |  1994-03-31  |  1.9 KB  |  62 lines

  1. ( example code for loading and saving data files within AFORTH)
  2. ( Copyright © Stratagem4, 1994)
  3.  
  4. CREATE data 40 ALLOT    ( create a 40 element array)
  5.  
  6. : savedata                ( save contents of 'data' to file)
  7.                         ( syntax is savedata <name>")
  8.     40 S->D    0 BUFFER    ( get a buffer of 40 bytes in public memory)
  9.     DUPD D0= IF            ( if we have no buffer then)
  10.         CR ." Could not allocate buffer...sorry!" CR
  11.         ABORT            ( drop everything)
  12.     ELSE                ( otherwise save the data)
  13.         DUPD            ( duplicate buffer address)
  14.         data            ( fetch pfa of 'data')
  15.         4 ROLL 4 ROLL    ( swap top two addresses)
  16.         20 MOVE            ( copy contents of 'data' to buffer)
  17.         ASCII " WORD    ( read next word as filename)
  18.         SAVE-BUFFERS    ( save data)
  19.         DROP            ( here we drop the success flag, but you should)
  20.                         ( test it)
  21.         EMPTY-BUFFERS    ( clean up)
  22.     THEN ;
  23.  
  24. : loaddata                ( load file and palce in 'data')
  25.                         ( syntax is loaddata <name>")
  26.     ASCII " WORD BLOCK    ( load filename)
  27.     DUPD D0= IF            ( if failed)
  28.         ABORT            ( drop everything)
  29.     ELSE                ( else)
  30.         DUPD data 20 MOVE    ( move data from buffer to 'data')
  31.         EMPTY-BUFFERS    ( free memory)
  32.     THEN ;
  33.  
  34. : testdata                ( fill the array with some known values)
  35.     data                ( fetch address of 'data')
  36.     20 0 DO                ( loop through 'data')
  37.         DUPD I ROT ROT !    ( store a value in each hole)
  38.         2D+                ( increment address)
  39.     LOOP
  40.     DROPD ;                ( drop address)
  41.  
  42. : cleardata                ( flush the array)
  43.     data 40 0 FILL ;    ( fill array with zero's)
  44.  
  45. : printdata                ( show contents of array)
  46.     data                ( fetch address of data)
  47.     20 0 DO                ( loop through array)
  48.         DUPD @ .        ( fetch and print number)
  49.         2D+                ( increment address)
  50.     LOOP
  51.     DROPD ;                ( drop address)
  52.  
  53. ( and now the actual test begins)
  54. ( *** NOTE *** this is an auto booting file!!!!)
  55. CR testdata CR
  56. ." test data: " CR printdata CR
  57. savedata ram:datafile"
  58. cleardata
  59. ." contents after saving and clearing: " CR printdata CR
  60. loaddata ram:datafile"
  61. ." contents after loading: " CR printdata CR CR
  62.