experimental.z80 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. ;All routines must be 768 bytes or less
  2. ;They get copied to moduleExec
  3. #include "grammer2.5.inc"
  4. mymodule:
  5. .db "Gram" ;magic number
  6. .dw MODULE_VERSION ;minimum version
  7. .dw (mod_start-2-$)/2 ;num elements
  8. .dw func11-mod_start ; ADDSTR
  9. .dw func12-mod_start ; ARRAY
  10. .dw func1-mod_start ; DEL
  11. .dw func9-mod_start ; FLOAT
  12. .dw func4-mod_start ; GET
  13. .dw func0-mod_start ; INITFS
  14. .dw func2-mod_start ; LIST
  15. .dw func3-mod_start ; MAT
  16. .dw func6-mod_start ; NAME
  17. .dw func5-mod_start ; SET
  18. .dw func10-mod_start ; STR
  19. .dw func8-mod_start ; UINT16
  20. .dw func7-mod_start ; UINT8
  21. ; File storage format needs
  22. ; - Find
  23. ; - Create
  24. ; - Delete
  25. ; - Resize
  26. mod_start:
  27. func0: .db "INITFS",$29 \ .dw func1-$-2
  28. ; This routine initializes the FS
  29. ;
  30. ; INITFS ptr,size
  31. ;
  32. call ParseFullArg
  33. ld (FS_begin),bc
  34. push bc
  35. call ParseNextFullArg
  36. pop hl
  37. add hl,bc
  38. ld (FS_end),hl
  39. ret
  40. func1: .db "DEL",$29 \ .dw func12-$-2
  41. ; This routine deletes a var in the FS
  42. ;
  43. ; DEL "name"
  44. ;
  45. call ParseFullArg
  46. ld h,b
  47. ld l,c
  48. call GetGrammerText
  49. ld h,d
  50. ld l,e
  51. add hl,bc
  52. ld (hl),0
  53. ex de,hl
  54. call FS_findvar
  55. jp FS_delvar
  56. func12: .db "ARRAY",$29 \ .dw func2-$-2
  57. ; This routine creates an array in the FS
  58. ;
  59. ; ARRAY "name",TYPE,[elem0,elem1,elem2,...]
  60. ;
  61. func12_start:
  62. call ParseFullArg
  63. ld h,b
  64. ld l,c
  65. call GetGrammerText
  66. ld h,d
  67. ld l,e
  68. add hl,bc
  69. ld (hl),0
  70. ex de,hl
  71. call FS_createvar_max
  72. ;HL points to the data
  73. ;BC is the size of the entry
  74. push hl
  75. push hl
  76. push bc
  77. ; Get the variable type
  78. call ParseNextFullArg
  79. ld e,c
  80. ;Now write the bytes: FS_array, type
  81. ;Make sure there is enough room!
  82. pop bc
  83. pop hl
  84. ld a,b
  85. or c
  86. jp z,ErrMem
  87. ld (hl),FS_array
  88. cpi
  89. jp po,ErrMem
  90. ld (hl),e
  91. cpi
  92. push hl
  93. ld hl,(parsePtr)
  94. ld a,(hl)
  95. pop hl
  96. jr array_loop_end
  97. _:
  98. push hl
  99. push bc
  100. push de
  101. call ParseNextFullArg
  102. pop hl ;L is type
  103. ld h,a ;Next byte to be parsed
  104. ld a,l ;A is type
  105. ld d,b
  106. ld e,c
  107. pop bc ;size remaining
  108. ex (sp),hl ;points to where to write
  109. call moduleExec+array_write-func12_start
  110. pop de
  111. ld a,d
  112. array_loop_end:
  113. cp $2B
  114. jr z,-_
  115. ;Now calculate the size of the array
  116. pop de ;points to the start
  117. or a
  118. sbc hl,de
  119. ld b,h
  120. ld c,l
  121. ex de,hl
  122. call FS_resize
  123. ld b,h
  124. ld c,l
  125. ret
  126. array_write:
  127. ;A is the type
  128. ;HL is where to write
  129. ;DE is the value to write
  130. ;BC is number of bytes available
  131. or a
  132. jr z,array_write_uint8
  133. dec a
  134. jr z,array_write_uint16
  135. dec a
  136. jp nz,ErrMem
  137. array_write_float:
  138. ;make sure BC>=4
  139. ld a,c
  140. sub 4
  141. ld a,b
  142. sbc a,0
  143. jp c,ErrMem
  144. ex de,hl
  145. ldi
  146. ldi
  147. ldi
  148. ldi
  149. ex de,hl
  150. ret
  151. array_write_uint8:
  152. ;make sure BC>=1
  153. ld a,b
  154. or c
  155. jp z,ErrMem
  156. ld (hl),e
  157. cpi
  158. ret
  159. array_write_uint16:
  160. ;make sure BC>=2
  161. ld a,c
  162. sub 2
  163. ld a,b
  164. sbc a,0
  165. jp c,ErrMem
  166. ld (hl),e
  167. cpi
  168. ld (hl),d
  169. cpi
  170. ret
  171. func2: .db "LIST",$29 \ .dw func3-$-2
  172. ; This routine creates a list in the FS
  173. ;
  174. ; LIST "name",elem0,elem1,elem2,...
  175. ;
  176. call ParseFullArg
  177. ld h,b
  178. ld l,c
  179. call GetGrammerText
  180. ld h,d
  181. ld l,e
  182. add hl,bc
  183. ld (hl),0
  184. ex de,hl
  185. call FS_createvar_max
  186. ;HL points to the data
  187. ;DE points to the entry
  188. ld bc,0
  189. jp FS_resize
  190. func3: .db "MAT",$29 \ .dw func10-$-2
  191. ; This routine creates a list in the FS
  192. ;
  193. ; MAT "name",[elem0,elem1,elem2,...],[...
  194. ;
  195. call ParseFullArg
  196. ld h,b
  197. ld l,c
  198. call GetGrammerText
  199. ld h,d
  200. ld l,e
  201. add hl,bc
  202. ld (hl),0
  203. ex de,hl
  204. call FS_createvar_max
  205. ;HL points to the data
  206. ;BC is the size of the entry
  207. ld bc,0
  208. jp FS_resize
  209. func10: .db "STR",$29 \ .dw func11-$-2
  210. ; This routine creates a list in the FS
  211. ;
  212. ; STR "name",ptr
  213. ;
  214. call ParseFullArg
  215. ld h,b
  216. ld l,c
  217. call GetGrammerText
  218. ld h,d
  219. ld l,e
  220. add hl,bc
  221. ld (hl),0
  222. ex de,hl
  223. call FS_createvar_max
  224. ;HL points to the data
  225. ;BC is the size available
  226. push hl
  227. push bc
  228. call ParseNextFullArg
  229. push bc
  230. ld h,b
  231. ld l,c
  232. call GetGrammerStr
  233. pop de ;string to copy
  234. pop hl ;available space
  235. scf
  236. inc bc
  237. sbc hl,bc
  238. dec bc
  239. jp c,ErrMem
  240. pop hl ;Where to copy the string to
  241. ld (hl),FS_STR
  242. push hl
  243. inc hl
  244. ex de,hl
  245. ld a,b
  246. or c
  247. jr z,+_
  248. push bc
  249. ldir
  250. pop bc
  251. _:
  252. xor a
  253. ld (de),a
  254. inc bc
  255. inc bc
  256. pop hl
  257. call FS_resize
  258. ld b,h
  259. ld c,l
  260. ld l,(hl)
  261. ld h,0
  262. ld (ThetaPrimeVar),hl
  263. ret
  264. func11: .db "ADDSTR",$29 \ .dw func4-$-2
  265. ; This routine creates a list in the FS
  266. ;
  267. ; ADDSTR ptr,ptr
  268. ;
  269. call ParseFullArg
  270. push bc
  271. push bc
  272. call ParseNextFullArg
  273. ;BC points to the second string
  274. ld a,(bc)
  275. cp FS_STR
  276. jp nz,ErrMem ;actually a syntax issue
  277. ld h,b
  278. ld l,c
  279. dec hl
  280. ld b,(hl)
  281. dec hl
  282. ld c,(hl)
  283. inc hl
  284. inc hl
  285. ex (sp),hl
  286. ;HL points to the first var
  287. ;BC is the size of the second string
  288. ld a,(hl)
  289. cp FS_STR
  290. jp nz,ErrMem ;actually a syntax issue
  291. dec hl
  292. ld d,(hl)
  293. dec hl
  294. ld e,(hl)
  295. inc hl
  296. inc hl
  297. push bc ;size of str
  298. push de
  299. ex de,hl
  300. add hl,bc
  301. ld b,h
  302. ld c,l
  303. dec bc ;Don't want to duplicate the type byte and ending null-byte
  304. dec bc
  305. ex de,hl
  306. ;BC is the new size
  307. call FS_resize
  308. pop de
  309. pop bc
  310. add hl,de
  311. pop de
  312. ex de,hl
  313. dec de ;overwrite the ending null byte
  314. inc hl ;don't write the type byte
  315. dec bc ;not writing first byte
  316. dec bc ;skip writing last byte
  317. ld a,b
  318. or c
  319. jr z,+_
  320. ldir
  321. _:
  322. ;Finish by writing a 0 to the end
  323. ;Q: "But Zeda, why not just use the end byte of the second string?"
  324. ;A: Trying concatenating a string to itself :P
  325. xor a
  326. ld (de),a
  327. pop bc
  328. ret
  329. func4: .db "GET",$29 \ .dw func5-$-2
  330. ; This routine reads an element from a var
  331. ;
  332. ; GET ptr,index[,index2[,index3[,...
  333. ;
  334. call ParseFullArg
  335. ld h,b
  336. ld l,c
  337. ;HL points to the data
  338. ret
  339. func5: .db "SET",$29 \ .dw func6-$-2
  340. ; This routine sets the value of an element in a var
  341. ;
  342. ; SET ptr,value,index[,index2[,index3[,...
  343. ;
  344. call ParseFullArg
  345. ld h,b
  346. ld l,c
  347. ;HL points to the data
  348. ret
  349. func6: .db "NAME",$29 \ .dw func7-$-2
  350. ; This routine gets a pointer to a var by its name
  351. ;
  352. ; NAME "name"
  353. ;
  354. call ParseFullArg
  355. ld h,b
  356. ld l,c
  357. call GetGrammerText
  358. ld h,d
  359. ld l,e
  360. add hl,bc
  361. ld (hl),0
  362. ex de,hl
  363. call FS_findvar
  364. ld b,h
  365. ld c,l
  366. ld l,(hl)
  367. ld h,0
  368. ld (ThetaPrimeVar),hl
  369. inc bc
  370. ret
  371. func7: .db "UINT8",0 \ .dw func8-$-2
  372. ld bc,0
  373. ret
  374. func8: .db "UINT16",0 \ .dw func9-$-2
  375. ld bc,1
  376. ret
  377. func9: .db "FLOAT",0 \ .dw mod_end-$-2
  378. ld bc,2
  379. ret
  380. mod_end:
  381. .echo $-mymodule," bytes"