time.a65 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. #include "memmap.i65"
  2. #include "dma.i65"
  3. timebox_data
  4. ; string offset, selection width, bcdtime offset, 1s limit, 10s limit
  5. .byt 0, 4, 9
  6. .byt 5, 2, 8
  7. .byt 8, 2, 6
  8. .byt 11, 2, 4
  9. .byt 14, 2, 2
  10. .byt 17, 2, 0
  11. time_month
  12. .byt 3, 1
  13. .byt 2, 8
  14. .byt 3, 1
  15. .byt 3, 0
  16. .byt 3, 1
  17. .byt 3, 0
  18. .byt 3, 1
  19. .byt 3, 1
  20. .byt 3, 0
  21. .byt 3, 1
  22. .byt 3, 0
  23. .byt 3, 1
  24. time_init:
  25. php
  26. sep #$20 : .as
  27. ; save text area
  28. jsr backup_screen
  29. ; draw window + current time setting
  30. lda #^text_clkset
  31. sta window_tbank
  32. ldx #!text_clkset
  33. stx window_taddr
  34. lda @time_win_x
  35. sta window_x
  36. lda @time_win_w
  37. sta window_w
  38. lda @time_win_y
  39. sta window_y
  40. lda @time_win_h
  41. sta window_h
  42. jsr draw_window
  43. jsr gettime
  44. stz time_sel
  45. stz time_exit
  46. lda #^text_buttonB
  47. sta print_bank
  48. ldx #!text_buttonB
  49. stx print_src
  50. lda @time_win_x
  51. inc
  52. inc
  53. sta print_x
  54. lda @time_win_y
  55. clc
  56. adc #$5
  57. sta print_y
  58. lda #$01
  59. sta print_pal
  60. jsr hiprint
  61. time_update
  62. jsr rendertime
  63. lda #^stringbuf
  64. sta print_bank
  65. ldx #!stringbuf
  66. stx print_src
  67. lda @time_win_x
  68. clc
  69. adc #$04
  70. sta print_x
  71. lda @time_win_y
  72. clc
  73. adc #$03
  74. sta print_y
  75. stz print_pal
  76. jsr hiprint
  77. lda time_sel
  78. asl
  79. clc
  80. adc time_sel
  81. xba
  82. lda #$00
  83. xba
  84. tax
  85. lda !timebox_data, x
  86. clc
  87. adc #$04
  88. adc @time_win_x
  89. sta bar_xl
  90. lda @time_win_y
  91. adc #$02
  92. sta bar_yl
  93. inx
  94. lda !timebox_data, x
  95. sta bar_wl
  96. inx
  97. lda !timebox_data, x
  98. sta time_ptr
  99. timeloop1
  100. lda isr_done
  101. lsr
  102. bcc timeloop1
  103. stz isr_done
  104. jsr read_pad
  105. lda #$01
  106. and pad1trig+1
  107. bne tkey_right
  108. lda #$02
  109. and pad1trig+1
  110. bne tkey_left
  111. lda #$04
  112. and pad1trig+1
  113. bne tkey_down
  114. lda #$08
  115. and pad1trig+1
  116. bne tkey_up
  117. lda #$80
  118. and pad1trig+1
  119. bne tkey_b
  120. ; do stuff
  121. lda time_exit
  122. beq timeloop1
  123. ; set clock
  124. jsr settime
  125. ; restore text area
  126. jsr restore_screen
  127. plp
  128. rtl
  129. tkey_b
  130. inc time_exit
  131. jmp time_update
  132. tkey_left
  133. jsr time_key_left
  134. jmp time_update
  135. tkey_right
  136. jsr time_key_right
  137. jmp time_update
  138. tkey_up
  139. jsr time_key_up
  140. jmp time_update
  141. tkey_down
  142. jsr time_key_down
  143. jmp time_update
  144. time_key_left
  145. lda time_sel
  146. beq +
  147. dec time_sel
  148. +
  149. rts
  150. time_key_right
  151. lda time_sel
  152. cmp #5
  153. beq +
  154. inc time_sel
  155. +
  156. rts
  157. time_key_up
  158. lda time_ptr
  159. cmp #4
  160. beq time_inc_hour_jmp
  161. cmp #6
  162. beq time_inc_day_jmp
  163. cmp #8
  164. beq time_inc_mon_jmp
  165. cmp #9
  166. beq time_inc_year_jmp
  167. xba
  168. lda #$00
  169. xba
  170. tax
  171. lda !time_s1, x
  172. inc
  173. cmp #$0a
  174. bmi time_inc_minsec1_normal
  175. stz !time_s1, x
  176. lda !time_s10, x
  177. inc
  178. cmp #$06
  179. bmi time_inc_minsec10_normal
  180. stz !time_s10, x
  181. bra +
  182. time_inc_minsec10_normal
  183. sta !time_s10, x
  184. bra +
  185. time_inc_minsec1_normal
  186. sta !time_s1, x
  187. +
  188. rts
  189. time_inc_hour_jmp
  190. jsr time_inc_hour
  191. rts
  192. time_inc_day_jmp
  193. jsr time_inc_day
  194. rts
  195. time_inc_mon_jmp
  196. jsr time_inc_mon
  197. rts
  198. time_inc_year_jmp
  199. jsr time_inc_year
  200. lda time_mon
  201. jsr time_adjust_mon
  202. rts
  203. time_inc_hour
  204. lda time_h10
  205. cmp #$02
  206. bne time_inc_hour_normal
  207. lda time_h1
  208. inc
  209. cmp #$04
  210. bmi +
  211. stz time_h1
  212. stz time_h10
  213. rts
  214. +
  215. sta time_h1
  216. rts
  217. time_inc_hour_normal
  218. lda time_h1
  219. inc
  220. cmp #$0a
  221. bmi +
  222. stz time_h1
  223. inc time_h10
  224. rts
  225. +
  226. sta time_h1
  227. rts
  228. time_inc_day
  229. lda time_mon
  230. dec
  231. asl
  232. xba
  233. lda #$00
  234. xba
  235. tax
  236. lda !time_month, x
  237. cmp time_d10
  238. bne time_inc_day_normal
  239. inx
  240. jsr is_leapyear_feb
  241. lda !time_month, x
  242. dec
  243. adc #$00
  244. cmp time_d1
  245. bpl +
  246. stz time_d1
  247. stz time_d10
  248. +
  249. inc time_d1
  250. rts
  251. time_inc_day_normal
  252. lda time_d1
  253. inc
  254. cmp #$0a
  255. bmi +
  256. stz time_d1
  257. inc time_d10
  258. rts
  259. +
  260. sta time_d1
  261. rts
  262. time_inc_mon
  263. lda time_mon
  264. inc
  265. cmp #13
  266. bmi +
  267. lda #$01
  268. +
  269. jsr time_adjust_mon
  270. rts
  271. time_adjust_mon
  272. sta time_mon
  273. dec
  274. asl
  275. xba
  276. lda #$00
  277. xba
  278. tax
  279. lda time_d10
  280. cmp !time_month, x
  281. bcs time_mon_adjust
  282. rts
  283. time_mon_adjust
  284. php
  285. inx
  286. lda !time_month, x
  287. pha
  288. jsr is_leapyear_feb ; c=1 -> a leapyear february
  289. pla
  290. adc #$00
  291. plp
  292. bne time_mon_doadjust
  293. cmp time_d1
  294. bcs +
  295. time_mon_doadjust
  296. sta time_d1
  297. dex
  298. lda !time_month, x
  299. sta time_d10
  300. +
  301. rts
  302. time_inc_year
  303. lda time_y1
  304. inc
  305. cmp #$0a
  306. bmi time_inc_y1_normal
  307. stz time_y1
  308. lda time_y10
  309. inc
  310. cmp #$0a
  311. bmi time_inc_y10_normal
  312. stz time_y10
  313. inc time_y100
  314. rts
  315. time_inc_y10_normal
  316. sta time_y10
  317. rts
  318. time_inc_y1_normal
  319. sta time_y1
  320. rts
  321. time_key_down
  322. lda time_ptr
  323. cmp #4
  324. beq time_dec_hour_jmp
  325. cmp #6
  326. beq time_dec_day_jmp
  327. cmp #8
  328. beq time_dec_mon_jmp
  329. cmp #9
  330. beq time_dec_year_jmp
  331. xba
  332. lda #$00
  333. xba
  334. tax
  335. lda !time_s1, x
  336. dec
  337. cmp #$ff
  338. bne time_dec_minsec1_normal
  339. lda #$09
  340. sta !time_s1, x
  341. lda !time_s10, x
  342. dec
  343. cmp #$ff
  344. bne time_dec_minsec10_normal
  345. lda #$05
  346. sta !time_s10, x
  347. bra +
  348. time_dec_minsec10_normal
  349. sta !time_s10, x
  350. bra +
  351. time_dec_minsec1_normal
  352. sta !time_s1, x
  353. +
  354. rts
  355. time_dec_hour_jmp
  356. jsr time_dec_hour
  357. rts
  358. time_dec_day_jmp
  359. jsr time_dec_day
  360. rts
  361. time_dec_mon_jmp
  362. jsr time_dec_mon
  363. rts
  364. time_dec_year_jmp
  365. jsr time_dec_year
  366. lda time_mon
  367. jsr time_adjust_mon
  368. rts
  369. time_dec_hour
  370. lda time_h1
  371. dec
  372. cmp #$ff
  373. bne time_dec_h1_normal
  374. lda time_h10
  375. bne time_dec_h10_normal
  376. lda #$02
  377. sta time_h10
  378. lda #$03
  379. sta time_h1
  380. rts
  381. time_dec_h10_normal
  382. lda #$09
  383. sta time_h1
  384. dec time_h10
  385. rts
  386. time_dec_h1_normal
  387. sta time_h1
  388. rts
  389. time_dec_day
  390. lda time_d10
  391. beq +
  392. lda time_d1
  393. dec
  394. cmp #$ff
  395. bne time_dec_d1_normal
  396. bra time_dec_cont
  397. + lda time_d1
  398. dec
  399. bne time_dec_d1_normal
  400. time_dec_cont
  401. lda time_d10
  402. bne time_dec_d10_normal
  403. lda time_mon
  404. dec
  405. asl
  406. ldx #$0000
  407. tax
  408. lda !time_month, x
  409. sta time_d10
  410. inx
  411. lda !time_month, x
  412. pha
  413. jsr is_leapyear_feb
  414. pla
  415. adc #$00
  416. sta time_d1
  417. rts
  418. time_dec_d10_normal
  419. lda #$09
  420. sta time_d1
  421. dec time_d10
  422. rts
  423. time_dec_d1_normal
  424. sta time_d1
  425. rts
  426. time_dec_mon
  427. lda time_mon
  428. dec
  429. bne +
  430. lda #12
  431. +
  432. jsr time_adjust_mon
  433. rts
  434. time_dec_year
  435. lda time_y1
  436. dec
  437. cmp #$ff
  438. bne time_dec_y1_normal
  439. lda #$09
  440. sta time_y1
  441. lda time_y10
  442. dec
  443. cmp #$ff
  444. bne time_dec_y10_normal
  445. lda #$09
  446. sta time_y10
  447. dec time_y100
  448. rts
  449. time_dec_y10_normal
  450. sta time_y10
  451. rts
  452. time_dec_y1_normal
  453. sta time_y1
  454. rts
  455. rts
  456. gettime
  457. lda #$0d
  458. sta $2801
  459. lda $2800
  460. lda $2800
  461. sta time_s1
  462. lda $2800
  463. sta time_s10
  464. lda $2800
  465. sta time_m1
  466. lda $2800
  467. sta time_m10
  468. lda $2800
  469. sta time_h1
  470. lda $2800
  471. sta time_h10
  472. lda $2800
  473. sta time_d1
  474. lda $2800
  475. sta time_d10
  476. lda $2800
  477. sta time_mon
  478. lda $2800
  479. sta time_y1
  480. lda $2800
  481. sta time_y10
  482. lda $2800
  483. sta time_y100
  484. rts
  485. rendertime
  486. ldx #!stringbuf
  487. lda time_y100
  488. and #$0f
  489. cmp #$0a
  490. bpl y_skip1
  491. clc
  492. adc #$30
  493. sta !1,x
  494. lda #$31
  495. sta !0,x
  496. bra y_skip2
  497. y_skip1
  498. clc
  499. adc #$26
  500. sta !1,x
  501. lda #$32
  502. sta !0,x
  503. y_skip2
  504. inx
  505. inx
  506. lda time_y10
  507. and #$0f
  508. clc
  509. adc #$30
  510. sta !0,x
  511. inx
  512. lda time_y1
  513. and #$0f
  514. clc
  515. adc #$30
  516. sta !0,x
  517. inx
  518. lda #$2d
  519. sta !0,x
  520. inx
  521. lda time_mon
  522. and #$0f
  523. cmp #$0a
  524. bpl mon_skip1
  525. clc
  526. adc #$30
  527. sta !1,x
  528. lda #$30
  529. sta !0,x
  530. bra mon_skip2
  531. mon_skip1
  532. clc
  533. adc #$26
  534. sta !1,x
  535. lda #$31
  536. sta !0,x
  537. mon_skip2
  538. inx
  539. inx
  540. lda #$2d
  541. sta !0,x
  542. inx
  543. lda time_d10
  544. clc
  545. adc #$30
  546. sta !0,x
  547. inx
  548. lda time_d1
  549. clc
  550. adc #$30
  551. sta !0,x
  552. inx
  553. lda #$20
  554. sta !0,x
  555. inx
  556. lda time_h10
  557. clc
  558. adc #$30
  559. sta !0,x
  560. inx
  561. lda time_h1
  562. clc
  563. adc #$30
  564. sta !0,x
  565. inx
  566. lda #$3a
  567. sta !0,x
  568. inx
  569. lda time_m10
  570. clc
  571. adc #$30
  572. sta !0,x
  573. inx
  574. lda time_m1
  575. clc
  576. adc #$30
  577. sta !0,x
  578. inx
  579. lda #$3a
  580. sta !0,x
  581. inx
  582. lda time_s10
  583. clc
  584. adc #$30
  585. sta !0,x
  586. inx
  587. lda time_s1
  588. clc
  589. adc #$30
  590. sta !0,x
  591. stz !1,x
  592. rts
  593. is_leapyear_feb
  594. lda time_mon
  595. cmp #$02
  596. bne is_leapyear_feb_no
  597. lda time_y10
  598. asl
  599. sta time_tmp
  600. asl
  601. asl
  602. clc
  603. adc time_tmp
  604. clc
  605. adc time_y1
  606. sta time_tmp
  607. and #$03
  608. beq is_leapyear_4th ; every 4th year
  609. is_leapyear_feb_no
  610. clc
  611. rts
  612. is_leapyear_4th ; leapyear candidate
  613. lda time_tmp ; year=00?
  614. beq + ; then check century
  615. sec ; else it's always a leapyear
  616. rts
  617. +
  618. lda time_y100 ; century divisible by 4?
  619. clc
  620. adc #10
  621. and #$03
  622. beq is_leapyear_400th
  623. clc
  624. rts
  625. is_leapyear_400th
  626. sec
  627. rts
  628. settime
  629. lda time_y100
  630. sta @AVR_PARAM
  631. lda time_y10
  632. sta @AVR_PARAM+1
  633. lda time_y1
  634. sta @AVR_PARAM+2
  635. lda time_mon
  636. sta @AVR_PARAM+3
  637. lda time_d10
  638. sta @AVR_PARAM+4
  639. lda time_d1
  640. sta @AVR_PARAM+5
  641. lda time_h10
  642. sta @AVR_PARAM+6
  643. lda time_h1
  644. sta @AVR_PARAM+7
  645. lda time_m10
  646. sta @AVR_PARAM+8
  647. lda time_m1
  648. sta @AVR_PARAM+9
  649. lda time_s10
  650. sta @AVR_PARAM+10
  651. lda time_s1
  652. sta @AVR_PARAM+11
  653. lda #$02 ; set clock
  654. sta @AVR_CMD
  655. rts
  656. printtime:
  657. jsr gettime
  658. jsr rendertime
  659. lda listdisp
  660. clc
  661. adc #$0a
  662. clc
  663. adc vidmode
  664. sta print_y
  665. lda #$2b
  666. sta print_x
  667. lda #$40
  668. sta print_count
  669. lda #^stringbuf
  670. sta print_bank
  671. ldx #!stringbuf
  672. stx print_src
  673. stz print_pal
  674. jsr hiprint
  675. rts