[Coladam] Timers in SmartBasic (CODE ENCLOSED)

Richard F. Drushel, Ph.D. drushel at apk.net
Sun Aug 19 18:05:05 EDT 2007


[Payton Byrd] spake unto the ether:

> Can anyone point me to a reference online or share some code that allows
> the tracking of time by second in SmartBasic?

	Hi Payton, appended is a code listing (in plaintext).  You still
have to assemble it and turn it into a set of DATA values to POKE into
memory.  I don't have a running ADAM system handy to write the wrapper
program for this assembly...but it should not be too much trouble,
assuming you have access to a Z80 assembler or at least opcode tables and
a hex calculator.

	View in a non-proportional font, or nothing will line up in
columns.

	If any programmer types see bugs, please jump in and comment.
This code was extracted and rewritten in ideal linear fashion from its
present state in SmartBASIC 1.x.

	*Dr. D.*


****************************** begin code listing *************************



;***************************************************************************
;  NMI-driven software clock for Colceo SmartBASIC                         *
;  by Dr. Richard F. Drushel  2007.08.19                                   *
;  modified from SmartBASIC 1.x code (1991.11.19, 1994.05.08, 1997.11.09)  *
;***************************************************************************

;*** WARNING ***
;
; You must disassemble the first few instructions of the NMI routine at
; 066H in your own version of SmartBASIC, in order to see how many code
; bytes are "damaged" by poking in a new JP instruction.  Make sure you
; include these at the end of the CLK routine before the JP A_069H.


;*** NOTE ***
;
; This code was "unrolled" from its original state in SmartBASIC 1.x
; (since SB1.x was created as a series of patches to the existing
; SmartBASIC 1.0 binary, and only much later reverse-engineered into
; an assembly listing).  It has been desk-checked but not run through
; an asssembler.  I hope that there are no errors.


;*************************************************************************** 
; EOS global data equates -- public symbols defined by Coleco
; note:   code assumes that they are contiguous in memory as given!

EOS_YEAR          EQU   0FDE0H      ;year byte (0-255, 0=1983)
EOS_MONTH         EQU   0FDE1H      ;month byte (1-12)
EOS_DAY           EQU   0FDE2H      ;day byte (1-31)

;***************************************************************************
; NMI routine
;

.ORG    066H        ;NMI vector address

_NMI_VECTOR_066H:
      JP    _CLK        ;if using SmartBASIC 1.0 with LOMEM 27407, then
                        ;_CLK will equal 27424; i.e., JP 27424
A_069H:
                        ;resume rest of SmartBASIC NMI routine
                        ;remember, AF is on the stack!

;***************************************************************************

.ORG    27407       ;SmartBASIC 1.0 start of user memory

;***************************************************************************
; software clock data areas
; note:  code assumes that they are contiguous in memory as given!

_CLK_MONTH_TABLE:
      DB    32,29,32,31,32,31,32,32,31,32,31,32     ;days in the month +1
_CLK_WEEKDAY:
      DB    1           ;weekday (1=Sunday)
_CLK_HOUR:
      DB    0           ;hour
_CLK_MINUTE:
      DB    0           ;minute
_CLK_SECOND:
      DB    0           ;second
_CLK_60TH:
      DB    0           ;sixtieth of second

;***************************************************************************
; software clock NMI routine

_CLK:
      PUSH  AF                      ;used by stock SB1.0 NMI routine
      PUSH  HL
      LD    HL,_CLK_60TH            ;point to current NMI tick
      LD    A,(HL)                  ;get it
      INC   A                       ;one more tick
      CP    60                      ;did we overflow?
      JR    NZ,CLK_SAVE_AND_EXIT    ;no, so save new and exit
      XOR   A                       ;yes, so back to zero
      LD    (HL),A                  ;reset 60th
      DEC   HL                      ;back up to _CLK_SECOND
      LD    A,(HL)                  ;get it
      INC   A                       ;one more second
      CP    60                      ;did we overflow?
      JR    NZ,CLK_SAVE_AND_EXIT    ;no, so save new value and exit
      XOR   A                       ;yes, so back to zero
      LD    (HL),A                  ;reset seconds
      DEC   HL                      ;back up to _CLK_MINUTE
      LD    A,(HL)                  ;get it
      INC   A                       ;one more minute
      CP    60                      ;did we overflow?
      JR    NZ,CLK_SAVE_AND_EXIT    ;no, so save new value and exit
      XOR   A                       ;yes, so back to zero
      LD    (HL),A                  ;reset minutes
      DEC   HL                      ;back up to _CLK_HOUR
      LD    A,(HL)                  ;get it 
      INC   A                       ;one more hour
      CP    24                      ;did we overflow?
      JR    NZ,CLK_SAVE_AND_EXIT    ;no
      XOR   A                       ;yes, so back to zero
      LD    (HL),A                  ;reset hours
      DEC   HL                      ;back up to _CLK_WEEKDAY
      LD    A,(HL)                  ;get it
      INC   A                       ;one more
      CP    8                       ;did we roll over?
      JR    C,CLK_SAVE_WEEKDAY      ;no
      LD    A,1                     ;yes, so reset to Sunday
CLK_SAVE_WEEKDAY:
      LD    (_CLK_WEEKDAY),A        ;save back new day
      LD    HL,_CLK_MONTH_TABLE-1   ;month table base
      PUSH  BC
      LD    A,(EOS_MONTH)           ;get current month
      LD    C,A                     ;into C
      LD    B,0                     ;make an offset
      ADD   HL,BC                   ;go into the table
      LD    B,(HL)                  ;get the number of days in this month
      LD    HL,EOS_DAY              ;point to the current day
      LD    A,(HL)                  ;get it
      INC   A                       ;one more
      CP    B                       ;are we at the max?
      POP   BC
      JR    C,CLK_SAVE_AND_EXIT     ;no, so just save new day and exit
      LD    A,1                     ;yes, so reset to the 1st
      LD    (HL),A                  ;and save it
      DEC   HL                      ;back up to EOS_MONTH
      LD    A,(HL)                  ;get it
      INC   A                       ;one more month
      CP    13                      ;did we roll over?
      JR    C,CLK_SAVE_AND_EXIT     ;no
      LD    A,1                     ;yes, so reset to January
      LD    (HL),A                  ;save it back
      DEC   HL                      ;back up to EOS_YEAR
      INC   (HL)                    ;one more year (don't worry about rollover)
      JR    CLK_EXIT                ;skip to exit
CLK_SAVE_AND_EXIT:
      LD    (HL),A                  ;copy back new value
CLK_EXIT:
      POP   HL

;Except for PUSH AF, include here any instructions that got wiped out by
;patching the 3 bytes at 066H, 067H, and 068H with the JP _CLK instruction!
      
      JP    A_069H      ;resume rest of existing NMI routine
                        ;remember that AF is still on the stack!

;***************************************************************************




******************************* end code listing *************************


-- 
Richard F. Drushel, Ph.D.            | "They fell:  for Heaven to them no hope
Department of Biology                |  imparts / Who hear not for the beating
Case Western Reserve University      |  of their hearts."
Cleveland, Ohio  44106-7080  U.S.A.  |         -- Edgar Allan Poe, "Al-Aaraaf"



More information about the Coladam mailing list