time.a65 8.5 KB

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