;;
;; MAIN.ASM
;;

; TAB size = 3
; Prefer spaces over TABs

; assemble with:  TASM -80 -b -s main.asm YOURFILEHERE.P

; the .SYM file can be loaded into EO for debugging.
; the .LST file is useful for reference.

; if you want the program to autorun, leave these as-is.
; if you don't, comment out the 1st line and uncomment the second.
;
;#define AUTORUN line1
#define AUTORUN dfile


; the ordering of this file's includes is critical - don't change it.
;
#include "sysvars.asm"
#include "line0.asm"


; you can't use this if you use TASM's .MODULE feature.
;
#include "charcode.asm"


   ;
   ; do your stuff here. for example:
   ;


START:
        call FAST
        ld ix,data_start                ; Pointer to the sound data
        ld bc,(data_end-data_start)/3   ; Number of notes

checkbreak:
        call BREAK                      ; $0f46
        jr c,loadnote

        call SLOW                       ; Return to BASIC
        ret

loadnote:
        push bc                        ;
        ld h,(ix+000h)                 ; H = pitch
        inc ix	                       ;
        ld e,(ix+000h)                 ; DE = duration (in H-1 cycles)
        inc ix                         ;
        ld d,(ix+000h)                 ;
        inc ix                         ;

        ld a,003h                      ; If (DE > 03ff) || (DE == 0000)
        cp d                           ;
        jr c,soundloop_end             ; Skip sound loop

        ld a,d	
        or e
        jr nz,soundloop_start

soundloop_end:
        pop bc                         ;
        dec bc                         ; BC--
        ld a,b                         ; If BC == 0 Then
        or c                           ;
        jr nz,checkbreak               ;	

        call SLOW                      ; Return to BASIC
        ret

; This loop produces a waveform like this:
 
; _____ ____  ___   __                    __   ___  ____ __________ __
; |   |_|  |__| |___||____|__________|____||___| |__|  |_|        |_|

; |     |     |     |     |     |     |     |     |     |     |     |
; |    |    |    |    |    |    |    |    |    |    |    |    |    |

; Two counters: H with period H' and L with H' - 1.

; Major period:  2*H*(H-1) * loop duration

; Frequency: 18 kHz/(2*H*(H-1))

soundloop_start:
        ld a,h                         ; Update "ld h,nn" and "ld l,nn"
        ld (hvalue+1),a                ; instructions below
        dec a                          ;
        ld (lvalue+1),a                ; H = H', L = H' - 1

;        ld a,0b8h                     ; Initial latch value

soundloop:

        ld b,004h                      ; Pause                                  7
pause1:
        nop                            ;                                        4
        djnz pause1                    ;                                        63      13/8

        dec h                          ; H--                                    4

        jr nz,hrz                      ; If H == 0 Then                         12

;        ld (02038h),a                 ; Change output state
        out (0ffh),a

;        xor 004h	
hvalue:  ld h,07fh                     ; Restore H to original value
hrz:
        ld b,004h                      ; Pause                                  7
pause2:
        nop                            ;
        djnz pause2                    ;                                        63

        dec l                          ; L--                                    4
        jr nz,soundloop                ; If L == 0 Then                         12
;        ld (02038h),a                 ; Change output state                    ===
        in a,(0feh)
;        xor 004h                      ;                                        172 T states
lvalue: ld l,07eh                      ; Restore L to original value

        dec de                         ; DE--

        ld c,a                         ;
        ld a,d                         ; If DE == 0 	
        or e                           ;
        ld a,c                         ;
        jr nz,soundloop                ;

        jr soundloop_end               ; End loop


data_start:
	defb 07fh		;	7f 	 
	defb 065h		;	65 	e 
	defb 000h		;	00 	. 

	defb 05fh		;	5f 	_ 
	defb 04bh		;	4b 	K 
	defb 000h		;	00 	. 

	defb 055h		;	55 	U 
	defb 043h		;	43 	C 
	defb 000h		;	00 	. 

	defb 04bh		;	4b 	K 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 0aah		;	aa 	. 
	defb 000h		;	00 	. 
	
	defb 066h		;	66 	f 
	defb 065h		;	65 	e 
	defb 001h		;	01 	. 
	
	defb 07fh		;	7f 	 
	defb 065h		;	65 	e 
	defb 000h		;	00 	. 
	
	defb 05fh		;	5f 	_ 
	defb 04bh		;	4b 	K 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 043h		;	43 	C 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 029h		;	29 	) 
	defb 001h		;	01 	. 
	
	defb 07fh		;	7f 	 
	defb 065h		;	65 	e 
	defb 000h		;	00 	. 
	
	defb 05fh		;	5f 	_ 
	defb 04bh		;	4b 	K 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 043h		;	43 	C 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 0aah		;	aa 	. 
	defb 000h		;	00 	. 
	
	defb 066h		;	66 	f 
	defb 065h		;	65 	e 
	defb 001h		;	01 	. 
	
	defb 066h		;	66 	f 
	defb 051h		;	51 	Q 
	defb 000h		;	00 	. 
	
	defb 05fh		;	5f 	_ 
	defb 0aah		;	aa 	. 
	defb 000h		;	00 	. 
	
	defb 066h		;	66 	f 
	defb 051h		;	51 	Q 
	defb 000h		;	00 	. 
	
	defb 07fh		;	7f 	 
	defb 0bch		;	bc 	. 
	defb 001h		;	01 	. 
	
	defb 03fh		;	3f 	? 
	defb 02ch		;	2c 	, 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 02eh		;	2e 	. 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 034h		;	34 	4 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 086h		;	86 	. 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 096h		;	96 	. 
	defb 000h		;	00 	. 
	
	defb 05fh		;	5f 	_ 
	defb 042h		;	42 	B 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 0aah		;	aa 	. 
	defb 000h		;	00 	. 
	
	defb 07fh		;	7f 	 
	defb 058h		;	58 	X 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 02eh		;	2e 	. 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 034h		;	34 	4 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 086h		;	86 	. 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 086h		;	86 	. 
	defb 000h		;	00 	. 
	
	defb 07fh		;	7f 	 
	defb 058h		;	58 	X 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 02eh		;	2e 	. 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 034h		;	34 	4 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 043h		;	43 	C 
	defb 086h		;	86 	. 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 03bh		;	3b 	; 
	defb 000h		;	00 	. 
	
	defb 04bh		;	4b 	K 
	defb 096h		;	96 	. 
	defb 000h		;	00 	. 

	defb 05fh		;	5f 	_ 
	defb 042h		;	42 	B 
	defb 000h		;	00 	. 
	
	defb 055h		;	55 	U 
	defb 0aah		;	aa 	. 
	defb 000h		;	00 	. 
	
	defb 066h		;	66 	f 
	defb 047h		;	47 	G 
	defb 000h		;	00 	. 
	
	defb 05fh		;	5f 	_ 
	defb 042h		;	42 	B 
	defb 000h		;	00 	. 
	
	defb 066h		;	66 	f 
	defb 047h		;	47 	G 
	defb 000h		;	00 	. 
	
	defb 07fh		;	7f 	 
	defb 058h		;	58 	X 
	defb 000h		;	00 	. 
	
	defb 07fh		;	7f 	 
	defb 0feh		;	fe 	. 
	defb 000h		;	00 	. 
data_end:



   ;
   ; and when (if) you're done...
   ;


   ; return to basic - resumes program execution at next line
   ;
;   jp    NEXTLINE

        defb $76

; the ordering of this file's includes is critical - don't change it.
;
#include "line1.asm"
