
;-------------------------------------------------------;
; music player (new) - super mario bros. (3) waterworld ;
;-------------------------------------------------------;

h_total      equ  8 + 64 + 6
h_sync       equ  8
v_total      equ  3 + 20 + 2
v_sync       equ  1

screen_start  equ  $e1d0+78
screen_total  equ  h_total*v_total-1

spacedrow   equ  0
underline   equ  10
rowlines    equ  10
offsetline  equ  1
spacedatr   equ  1
cursormod   equ  1

crt_prm_port  equ  $ef00
crt_cmd_port  equ  $ef01
crt_reset     equ  $00
crt_start     equ  $27
crt_cursor    equ  $80
crt_preset    equ  $e0
crt_ir_flag   equ  $20

dma_adr2_port  equ  $f004
dma_cnt2_port  equ  $f005
dma_adr3_port  equ  $f006
dma_cnt3_port  equ  $f007
dma_mode_port  equ  $f008
dma_reset      equ  $80
dma_start      equ  $a4

pit_cnt1_port  equ  $ec00
pit_cnt2_port  equ  $ec01
pit_cnt3_port  equ  $ec02
pit_ctrl_port  equ  $ec03
pit_init1      equ  $3e
pit_init2      equ  $7e
pit_init3      equ  $be

key_tape_port  equ  $ed02

print_text     equ  $f818
display_reset  equ  $f82d
command_line   equ  $f875

;------------------------------------

begin:

  lxi  h, text
  call print_text
  pchl

text:
db 10,13,'SUPER MARIO BROS - WATERWORLD THEME (60 HZ)',10,13,10,13,0


  lxi  h, crt_cmd_port
  mov  a, m
  mvi  m, crt_reset
  dcr  l
  mvi  m, spacedrow*128 + h_total-1
  mvi  m, (v_sync-1)*64 + v_total-1
  mvi  m, (underline-1)*16 + rowlines-1
  mvi  m, offsetline*128 + spacedatr*64 + cursormod*16 + h_sync/2-1
  inr  l
  mvi  m, crt_start
  mov  a, m
  ani  crt_ir_flag
  jz   .-3

  lxi  h, dma_mode_port
  mvi  m, dma_reset
  mvi  l, 4
  mvi  m, screen_start & $ff
  mvi  m, screen_start >> 8
  inr  l
  mvi  m, screen_total & $ff
  mvi  m, $4000 + screen_total >> 8
  mvi  l, 8
  mvi  m, dma_start

  lxi  h, sound_data  ;
  shld data_pointer   ;
  mvi  a, 30          ;
  sta  wait_counter   ;


vsync:

  lda  crt_cmd_port
  ani  crt_ir_flag
  jz   vsync

  lxi  h, wait_counter
  dcr  m
  jnz  vsync


read_command:

  call get_next_data

  ora  a               ;
  jnz  off_1           ;
  lxi  h, repeat_point ;
  shld data_pointer
  jmp  read_command


off_1:

  lxi  h, pit_ctrl_port

  rlc
  jnc  off_2
  mvi  m, pit_init1


off_2:

  rlc
  jnc  off_3
  mvi  m, pit_init2


off_3:

  rlc
  jnc  set_1
  mvi  m, pit_init3


set_1:

  rlc
  mov  b, a
  jnc  set_2
  lxi  d, note_table_1
  call get_table_value
  lxi  d, pit_cnt1_port
  call set_channel_value


set_2:

  mov  a, b
  rlc
  mov  b, a
  jnc  set_3
  lxi  d, note_table_2
  call get_table_value
  lxi  d, pit_cnt2_port
  call set_channel_value


set_3:

  mov  a, b
  rlc
  jnc  set_timer
  lxi  d, note_table_3
  call get_table_value
  lxi  d, pit_cnt3_port
  call set_channel_value


set_timer:
  
  call get_next_data
  sta  wait_counter


chek_key:

  lda  key_tape_port
  rlc
  rlc
  jnc  end_program
  rlc
  jc   vsync  
  

end_program:
  
  lxi  h, pit_ctrl_port
  mvi  m, pit_init1
  mvi  m, pit_init2
  mvi  m, pit_init3
  call display_reset
  jmp  command_line


set_channel_value:
  
  mov  a, m
  stax d
  inx  h
  mov  a, m
  stax d
  ret
  

get_table_value:

  call get_next_data
  mov  l, a
  mvi  h, 0
  dad  h
  dad  d
  ret


get_next_data:

  lhld data_pointer
  mov  a, m           
  inx  h              
  shld data_pointer
  ret

;---------------------------

note_table_1:
dw 7216, 6811, 6430, 6070, 5409, 5107, 4821, 4551, 4057, 3616, 3414, 3223, 3043, 2713, 

note_table_2:
dw 6070, 5409, 4821, 4551, 4057, 3830, 3616, 3414, 3223, 3043, 2873, 2713, 2561, 2284, 2020, 

note_table_3:
dw 27213, 18173, 15287, 14431, 13622, 12860, 12140, 10819, 10213, 9642, 9103, 8113, 7660, 7232, 6827, 6446, 6086, 5425, 5123, 

sound_data:
db 
repeat_point:
db 56, 3, 0, 16, 24, 2, 1, 16, 24, 1, 2, 16, 24, 0, 3, 16, 24, 1, 4, 16, 24, 2, 5, 16, 24, 3, 6, 8, 216, 3, 6, 8, 216, 3, 6, 8, 192, 8, 28, 4, 6, 10, 8, 224, 8, 28, 5, 6, 10, 32, 8, 3, 8, 128, 8, 60, 7, 11, 4, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 224, 8, 28, 6, 10, 3, 8, 32, 8, 4, 10, 8, 32, 8, 4, 13, 8, 224, 8, 28, 7, 11, 4, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 224, 8, 4, 7, 8, 40, 3, 8, 12, 4, 10, 8, 40, 6, 8, 12, 7, 14, 8, 40, 9, 8, 28, 7, 11, 4, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 224, 8, 28, 6, 10, 3, 8, 32, 8, 4, 10, 8, 32, 8, 28, 8, 12, 13, 8, 32, 8, 28, 7, 11, 4, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 224, 8, 4, 7, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 40, 3, 8, 28, 5, 9, 6, 8, 32, 8, 4, 10, 8, 32, 8, 4, 13, 8, 224, 8, 28, 4, 8, 5, 8, 32, 8, 4, 9, 8, 32, 8, 4, 12, 8, 224, 8, 28, 5, 9, 6, 8, 32, 8, 4, 10, 8, 32, 8, 4, 13, 8, 224, 8, 4, 3, 8, 40, 3, 8, 12, 4, 10, 8, 40, 6, 8, 12, 7, 13, 8, 40, 8, 8, 28, 5, 9, 6, 8, 32, 8, 4, 10, 8, 32, 8, 4, 13, 8, 224, 8, 28, 0, 3, 3, 8, 32, 8, 4, 10, 8, 32, 8, 28, 8, 12, 13, 8, 32, 8, 28, 7, 11, 4, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 224, 8, 4, 1, 8, 32, 8, 4, 10, 8, 32, 8, 4, 14, 8, 40, 3, 8, 28, 13, 13, 4, 8, 32, 8, 4, 10, 8, 32, 8, 4, 17, 8, 224, 8, 28, 12, 13, 3, 8, 32, 8, 4, 10, 8, 32, 8, 4, 16, 8, 224, 8, 28, 11, 13, 2, 8, 32, 8, 4, 10, 8, 32, 8, 4, 15, 8, 224, 8, 12, 13, 5, 8, 32, 8, 12, 14, 10, 8, 96, 8, 4, 17, 8, 40, 13, 8, 28, 12, 12, 6, 8, 32, 8, 4, 11, 8, 32, 8, 4, 18, 8, 224, 8, 28, 11, 12, 5, 8, 32, 8, 4, 11, 8, 32, 8, 4, 18, 8, 224, 8, 28, 10, 12, 4, 8, 32, 8, 4, 11, 8, 32, 8, 4, 18, 8, 224, 8, 12, 12, 3, 8, 32, 8, 12, 13, 10, 8, 96, 8, 4, 18, 8, 40, 12, 8, 28, 1, 11, 0, 8, 32, 8, 4, 10, 8, 32, 8, 4, 17, 8, 224, 8, 28, 5, 4, 1, 8, 32, 8, 28, 7, 6, 10, 8, 32, 8, 28, 9, 12, 10, 8, 32, 8, 156, 9, 11, 8, 8, 252, 9, 11, 8, 8, 252, 9, 11, 8, 24, 60, 5, 6, 3, 8, 60, 4, 7, 4, 40, 192, 8, 0  ; eof

;---------------------------

data_pointer:
dw 0

wait_counter:
db 0
