math.z80 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. Sqrt:
  2. ld a,(hl)
  3. cp 3Ah
  4. jr z,floatsqrt
  5. inc hl
  6. cp $AE
  7. push af
  8. jr nz,+_
  9. ld (parsePtr),hl
  10. _:
  11. call ParseFullArg
  12. ld hl,sqrtHL
  13. ld (next_page_call_address),hl
  14. ld h,b
  15. ld l,c
  16. call next_page_call
  17. ld (ThetaPrimeVar),hl
  18. ld c,a
  19. ld b,0
  20. pop af
  21. ret nz
  22. ;if HL<=BC, increment BC
  23. sbc hl,bc
  24. ret c
  25. ret z
  26. inc bc
  27. ret
  28. floatsqrt:
  29. ld hl,sqrtSingle
  30. jr float_stepin_1
  31. sinefloat:
  32. pop af
  33. ld hl,sinSingle
  34. jr float_stepin_1
  35. _:
  36. ld hl,cosSingle
  37. float_stepin_1:
  38. push hl ;the call to make
  39. call ParseNextFullArg
  40. float_stepin_1_:
  41. push bc ;the second argument
  42. call floatstackpush
  43. ld b,h
  44. ld c,l
  45. pop de
  46. pop hl
  47. ld (next_page_call_address),hl
  48. ex de,hl
  49. jp next_page_call
  50. Cos:
  51. ld a,(hl)
  52. cp 3Ah
  53. jr z,-_
  54. ld a,64
  55. .db $FE
  56. Sin:
  57. xor a
  58. push af
  59. ld a,(hl)
  60. cp 3Ah
  61. jr z,sinefloat
  62. call ParseFullArg
  63. pop af
  64. add a,c
  65. Sine:
  66. ld d,a
  67. rlca
  68. rlca
  69. sbc a,a
  70. xor d
  71. and 63
  72. ld b,0
  73. #if (sinTable&255)<192
  74. #if (sintable&255)==0
  75. ld h,sinTable>>8
  76. ld l,a
  77. #else
  78. ld hl,sinTable
  79. add a,l
  80. ld l,a
  81. #endif
  82. #else
  83. ld c,a
  84. ld hl,sinTable
  85. add hl,bc
  86. #endif
  87. ld c,(hl)
  88. rlc d
  89. jr c,NegateBC
  90. ret
  91. _:
  92. ld hl,absSingle
  93. jp float_stepin_1
  94. absToken:
  95. ld a,(hl)
  96. cp 3Ah
  97. jr z,-_
  98. call ParseFullArg
  99. bit 7,b
  100. ret z
  101. jr negateBC
  102. Negative:
  103. ld a,(hl)
  104. cp 3Ah
  105. jr z,+_
  106. call ParseFullArg
  107. negateBC:
  108. xor a \ sub c \ ld c,a
  109. sbc a,a \ sub b \ ld b,a
  110. ret
  111. _:
  112. ld hl,negSingle
  113. jp float_stepin_1
  114. logToken:
  115. ld a,(hl)
  116. cp 3Ah
  117. jp nz,ErrBadToken
  118. call ParseNextFullArg
  119. cp 2Bh
  120. ld hl,logSingle
  121. jp z,float_stepin_2_
  122. ld hl,log10Single
  123. push hl
  124. jr float_stepin_1_
  125. Add:
  126. push bc
  127. ld a,(hl)
  128. cp 3Ah
  129. jr z,+_
  130. call ParseFullArg
  131. pop hl
  132. xor a
  133. add hl,bc
  134. ld b,h
  135. ld c,l
  136. ld h,a
  137. adc a,a
  138. ld l,a
  139. ld (ThetaPrimeVar),hl
  140. ret
  141. _:
  142. ld hl,addSingle
  143. jp float_stepin
  144. SubtractToken:
  145. push bc
  146. ld a,(hl)
  147. cp 3Ah
  148. jr z,+_
  149. call ParseFullArg
  150. pop hl
  151. xor a
  152. sbc hl,bc
  153. ld b,h
  154. ld c,l
  155. ld h,a
  156. ld l,a
  157. ld (ThetaPrimeVar),hl
  158. ret
  159. _:
  160. ld hl,subSingle
  161. jp float_stepin
  162. Cubed:
  163. ld d,b
  164. ld e,c
  165. call DE_Times_BC
  166. ex de,hl
  167. jr mul_stepin
  168. Squared:
  169. ld d,b
  170. ld e,c
  171. jr mul_stepin
  172. Multiply:
  173. push bc
  174. ld a,(hl)
  175. cp 3Ah
  176. jr z,+_
  177. call ParseFullArg
  178. pop de
  179. mul_stepin:
  180. call DE_Times_BC
  181. ld b,h
  182. ld c,l
  183. ld (ThetaPrimeVar),de
  184. ret
  185. _:
  186. ld hl,mulSingle
  187. float_stepin:
  188. push hl
  189. call ParseNextFullArg
  190. push bc
  191. call floatstackpush
  192. ld b,h
  193. ld c,l
  194. pop de
  195. pop hl
  196. ld (next_page_call_address),hl
  197. pop hl
  198. jp next_page_call
  199. floatstackpush:
  200. ld hl,(floatstack_ptr)
  201. ld bc,4
  202. add hl,bc
  203. ld (floatstack_ptr),hl
  204. ld a,l \ cp floatstack_top&255
  205. ret nz
  206. #if (floatstack_top>>8)!=(floatstack_bottom>>8)
  207. ld a,h \ cp floatstack_top>>8
  208. ret nz
  209. #endif
  210. ld hl,floatstack_bottom
  211. ld (floatstack_ptr),hl
  212. ret
  213. Comment:
  214. SkipLine:
  215. bit floatmode,(iy+ModeFlags2)
  216. jp nz,ConvRStr_Float
  217. call EndOfLine
  218. jp ParseArg2
  219. SkipLineDiv:
  220. ld a,(hl)
  221. cp 93h \ jp z,SlowText
  222. cp 83h
  223. jr z,SkipLine
  224. cp 29h
  225. jr nz,Divide
  226. push bc
  227. call ParseNextFullArg
  228. pop hl
  229. ;1366cc+6cc for every 0 bit in the result (up to +90cc
  230. ;+48cc unless HL and BC are both positive
  231. ;min: 1366cc
  232. ;max: 1504cc
  233. ;avg: 1447cc
  234. ld a,h
  235. xor b
  236. push af
  237. xor b
  238. jp p,+_
  239. xor a
  240. sub l
  241. ld l,a
  242. sbc a,a
  243. sub h
  244. ld h,a
  245. _:
  246. xor b
  247. jp p,+_
  248. xor a
  249. sub c
  250. ld c,a
  251. sbc a,a
  252. sub b
  253. ld b,a
  254. _:
  255. call HL_Div_BC
  256. pop af
  257. jp p,+_
  258. xor a
  259. sub l
  260. ld l,a
  261. sbc a,a
  262. sub h
  263. ld h,a
  264. _:
  265. ld b,h
  266. ld c,l
  267. ld (ThetaPrimeVar),de
  268. ret
  269. Divide:
  270. push bc
  271. ld a,(hl)
  272. cp 3Ah
  273. jr z,+_
  274. call ParseFullArg
  275. pop hl
  276. call HL_Div_BC
  277. ld b,h
  278. ld c,l
  279. ld (ThetaPrimeVar),de
  280. ret
  281. _:
  282. ld hl,divSingle
  283. jp float_stepin
  284. PowToken:
  285. ld a,(hl)
  286. cp 3Ah
  287. jp nz,ErrBadTOken
  288. push bc
  289. ld hl,powSingle
  290. jp float_stepin
  291. _:
  292. ld hl,expSingle
  293. jp float_stepin_1
  294. PowerOf2:
  295. ld a,(hl)
  296. cp 3Ah
  297. jr z,-_
  298. call ParseFullArg
  299. ld a,c
  300. ld hl,0
  301. ld d,h
  302. ld e,h
  303. cp 32
  304. jr nc,Po2End
  305. inc l
  306. or a
  307. jr z,Po2End
  308. ld b,a
  309. _:
  310. add hl,hl
  311. rl e
  312. rl d
  313. djnz -_
  314. Po2End:
  315. ld b,h
  316. ld c,l
  317. ret
  318. Logic:
  319. Equals:
  320. ld a,(hl)
  321. cp $3A
  322. jr z,feq
  323. push bc
  324. factorialStepIn:
  325. call ParseFullArg
  326. pop hl
  327. xor a
  328. sbc hl,bc
  329. ld b,a
  330. ld c,a
  331. ret nz
  332. inc c
  333. ret
  334. feq:
  335. call fcmp
  336. ld bc,0
  337. ret nz
  338. inc c
  339. ret
  340. fcmp:
  341. push bc
  342. call ParseNextFullArg
  343. ld hl,cmpSingle
  344. ld (next_page_call_address),hl
  345. pop hl
  346. ld d,b
  347. ld e,c
  348. jp next_page_call
  349. moreLogic:
  350. push af
  351. push bc
  352. call ParseFullArg
  353. pop hl
  354. pop af
  355. ret
  356. notLogic:
  357. call MoreLogic
  358. ld a,b \ cpl \ ld b,a
  359. ld a,c \ cpl \ ld c,a
  360. ret
  361. andLogic:
  362. call MoreLogic
  363. ld a,h \ and b \ ld b,a
  364. ld a,l \ and c \ ld c,a
  365. ret
  366. orLogic:
  367. call MoreLogic
  368. ld a,h \ or b \ ld b,a
  369. ld a,l \ or c \ ld c,a
  370. ret
  371. xorLogic:
  372. call MoreLogic
  373. ld a,h \ xor b \ ld b,a
  374. ld a,l \ xor c \ ld c,a
  375. ret
  376. Less:
  377. ld a,(hl)
  378. cp $3A
  379. jr z,flt
  380. call MoreLogic
  381. xor a
  382. sbc hl,bc
  383. ld b,a
  384. adc a,a
  385. ld c,a
  386. ret
  387. flt:
  388. call fcmp
  389. ld bc,0
  390. ret nc
  391. inc c
  392. ret
  393. Greater:
  394. ld a,(hl)
  395. cp $3A
  396. jr z,fgt
  397. call MoreLogic
  398. xor a
  399. scf
  400. sbc hl,bc
  401. ccf
  402. ld b,a
  403. adc a,a
  404. ld c,a
  405. ret
  406. fgt:
  407. call fcmp
  408. ld bc,0
  409. ret z
  410. ret c
  411. inc c
  412. ret
  413. LessOrEqual:
  414. ld a,(hl)
  415. cp $3A
  416. jr z,fle
  417. call MoreLogic
  418. xor a
  419. scf
  420. sbc hl,bc
  421. ld b,a
  422. adc a,a
  423. ld c,a
  424. ret
  425. fle:
  426. call fcmp
  427. ld bc,1
  428. ret c
  429. ret z
  430. dec c
  431. ret
  432. MoreOrEqual:
  433. ld a,(hl)
  434. cp $3A
  435. jr z,fge
  436. call MoreLogic
  437. xor a
  438. sbc hl,bc
  439. ccf
  440. ld b,a
  441. adc a,a
  442. ld c,a
  443. ret
  444. fge:
  445. call fcmp
  446. ld bc,0
  447. ret c
  448. inc c
  449. ret
  450. NotEqual:
  451. ld a,(hl)
  452. cp $3A
  453. jr z,fne
  454. call MoreLogic
  455. xor a
  456. sbc hl,bc
  457. ld b,a
  458. ld c,a
  459. ret z
  460. inc c
  461. ret
  462. fne:
  463. call fcmp
  464. ld bc,0
  465. ret z
  466. inc c
  467. ret
  468. RandToken:
  469. ld a,(hl)
  470. cp 3Ah
  471. jr z,+_
  472. call prng16
  473. ld b,h
  474. ld c,l
  475. ret
  476. _:
  477. inc hl
  478. ld (parsePtr),hl
  479. ld hl,randSingle
  480. float_stepin_0:
  481. push hl
  482. call floatstackpush
  483. ld b,h
  484. ld c,l
  485. pop hl
  486. ld (next_page_call_address),hl
  487. jp next_page_call
  488. tanToken:
  489. ld a,(hl)
  490. cp 3Ah
  491. jp nz,ErrBadToken
  492. ld hl,tanSingle
  493. jp float_stepin_1
  494. atanToken:
  495. ld a,(hl)
  496. cp 3Ah
  497. jr z,atan_float
  498. call ParseFullArg
  499. push bc
  500. ld bc,128
  501. cp 2Bh
  502. call z,ParseNextFullArg
  503. pop de
  504. ld hl,atanbin
  505. ld (next_page_call_address),hl
  506. jp next_page_call
  507. atan_float:
  508. ld hl,atanSingle
  509. jp float_stepin_1
  510. acosToken:
  511. ld a,(hl)
  512. cp 3Ah
  513. jp nz,ErrBadToken
  514. ld hl,acosSingle
  515. jp float_stepin_1
  516. asinToken:
  517. ld a,(hl)
  518. cp 3Ah
  519. jp nz,ErrBadToken
  520. ld hl,asinSingle
  521. jp float_stepin_1
  522. tanhToken:
  523. ld a,(hl)
  524. cp 3Ah
  525. jp nz,ErrBadToken
  526. ld hl,tanhSingle
  527. jp float_stepin_1
  528. coshToken:
  529. ld a,(hl)
  530. cp 3Ah
  531. jp nz,ErrBadToken
  532. ld hl,coshSingle
  533. jp float_stepin_1
  534. sinhToken:
  535. ld a,(hl)
  536. cp 3Ah
  537. jp nz,ErrBadToken
  538. ld hl,sinhSingle
  539. jp float_stepin_1
  540. atanhToken:
  541. ld a,(hl)
  542. cp 3Ah
  543. jp nz,ErrBadToken
  544. ld hl,atanhSingle
  545. jp float_stepin_1
  546. acoshToken:
  547. ld a,(hl)
  548. cp 3Ah
  549. jp nz,ErrBadToken
  550. ld hl,acoshSingle
  551. jp float_stepin_1
  552. asinhToken:
  553. ld a,(hl)
  554. cp 3Ah
  555. jp nz,ErrBadToken
  556. ld hl,asinhSingle
  557. jp float_stepin_1
  558. pow10Token:
  559. ld a,(hl)
  560. cp 3Ah
  561. jp nz,ErrBadToken
  562. ld hl,pow10Single
  563. jp float_stepin_1
  564. meanToken:
  565. ld a,(hl)
  566. cp 3Ah
  567. jp nz,ErrBadToken
  568. ld hl,ameanSingle
  569. float_stepin_2:
  570. push hl
  571. call ParseNextFullArg
  572. pop hl
  573. float_stepin_2_:
  574. push bc
  575. push hl
  576. call ParseNextFullArg
  577. push bc
  578. call floatstackpush
  579. ld b,h
  580. ld c,l
  581. pop de
  582. pop hl
  583. ld (next_page_call_address),hl
  584. pop hl
  585. jp next_page_call