src/halt.asm3.9 KB · NASM
Raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
include "hardware.inc"
include "hUGE.inc"

SECTION "LCD controller status interrupt", ROM0[$0048]
    ;; HACK!!!!!!!!!!!!!
    ;; there's some sort of bug in the emulator which needs to be fixed,
    ;; which screws up the program counter immediately after it exits a halt.
    ;; this nop protects against that for now.
    nop
    nop
    nop
    reti

SECTION "init", ROM0[$0100]
    jp init

SECTION "romname", ROM0[$0134]
; $0134 - $013E: The title, in upper-case letters, followed by zeroes.
DB "HALT"
DS 7 ; padding
; $013F - $0142: The manufacturer code. Empty for now
DS 4
DS 1
; $0144 - $0145: "New" Licensee Code, a two character name.
DB "NF"

SECTION "Vars", WRAM0

instrument1: ds 6
instrument2: ds 6
instrument3: ds 6
instrument4: ds 6

subpattern1: ds (3*64)
subpattern2: ds (3*64)
subpattern3: ds (3*64)
subpattern4: ds (3*64)

waveforms: ds (16*16)

start_zero:

start_ch1: db
start_ch2: db
start_ch3: db
start_ch4: db

running_ch1: db
running_ch2: db
running_ch3: db
running_ch4: db

end_zero:

SECTION "code", ROM0[$400]

init_note1:
  ld hl, instrument1
  ld a, [hl+]
  ldh [rAUD1SWEEP], a
  ld a, [hl+]
  ldh [rAUD1LEN], a
  ld a, [hl+]
  ldh [rAUD1ENV], a
  ld a, [hl]
  ld [highmask1], a

  xor a
  ld [table_row1], a
  ld [start_ch1], a

  jp play_ch1_note

init_note2:
  ld hl, instrument2
  inc hl
  ld a, [hl+]
  ldh [rAUD2LEN], a
  ld a, [hl+]
  ldh [rAUD2ENV], a
  ld a, [hl]
  ld [highmask2], a

  xor a
  ld [table_row2], a
  ld [start_ch2], a

  jp play_ch2_note

init_note3:
  ld hl, instrument3
  ld a, [hl+]
  ldh [rAUD3LEN], a
  ld a, [hl+]
  ldh [rAUD3LEVEL], a
  ld a, [hl+]
  ld [current_wave], a
  ld a, [hl]
  ld [highmask3], a

  xor a
  ld [table_row3], a
  ld [start_ch3], a

  jp play_ch3_note

init_note4:
  ld a, [channel_note4]
  call get_note_poly
  ld [channel_period4], a

  ld hl, instrument4
  ld a, [hl+]
  ldh [rAUD4ENV], a
  inc hl
  inc hl
  ld a, [hl]
  and %00111111
  ldh [rAUD4LEN], a

  ld a, [channel_period4]
  ld d, a
  ld a, [hl]
  and %10000000
  swap a
  ld [step_width4], a
  or d
  ld [channel_period4], a

  ld a, [hl]
  and %01000000
  or  %10000000
  ld [highmask4], a

  xor a
  ld [table_row4], a
  ld [start_ch4], a

  jp play_ch4_note

run_table1:
  ld bc, subpattern1
  ld hl, table_row1
  ld e, 0

  ld a, b
  or c
  jp nz, do_table
  ret

run_table2:
  ld bc, subpattern2
  ld hl, table_row2
  ld e, 1

  ld a, b
  or c
  jp nz, do_table
  ret

run_table3:
  ld bc, subpattern3
  ld hl, table_row3
  ld e, 2

  ld a, b
  or c
  jp nz, do_table
  ret

run_table4:
  ld bc, subpattern4
  ld hl, table_row4
  ld e, 3

  ld a, b
  or c
  jp nz, do_table
  ret

init:
    ld a, $FF
DEF _addr = _AUD3WAVERAM
    REPT 16
    ldh [_addr], a
DEF _addr = _addr + 1
    ENDR

    ld a, $80
    ldh [rAUDENA], a
    ; Enable all channels in stereo
    ld a, $FF
    ldh [rAUDTERM], a
    ; Set volume
    ld a, $77
    ldh [rAUDVOL], a
    ; silence ch3
    xor a
    ldh [rAUD3LEVEL], a

    ;; Enable the HBlank interrupt on scanline 0
    ldh a, [rSTAT]
    or a, STATF_LYC
    ldh [rSTAT], a
    xor a
    ldh [rLYC], a

    ld a, IEF_LCDC
    ldh [rIE], a
    ei

    ;; Initialize hUGEDriver-related vars
    xor a
    ld [row], a

    ;; Setup waves pointer
    ld hl, waves
    ld a, LOW(waveforms)
    ld [hl+], a
    ld a, HIGH(waveforms)
    ld [hl], a

    ;; Zero some ram
    ld c, end_zero - start_zero
    ld hl, start_zero
    xor a
.fill_loop:
    ld [hl+], a
    dec c
    jr nz, .fill_loop

_halt:
    halt
    nop

    ld a, [start_ch1]
    and $FF
    call nz, init_note1

    ld a, [start_ch2]
    and $FF
    call nz, init_note2

    ld a, [start_ch3]
    and $FF
    call nz, init_note3

    ld a, [start_ch4]
    and $FF
    call nz, init_note4

    ;;;;;;;;;;;;;;;;;;;;;

    ld a, [running_ch1]
    and $FF
    call nz, run_table1

    ld a, [running_ch2]
    and $FF
    call nz, run_table2

    ld a, [running_ch3]
    and $FF
    call nz, run_table3

    ld a, [running_ch4]
    and $FF
    call nz, run_table4

    jr _halt