Commit 5543644

Nick committed on
WIP stack hack
commit 5543644a88581969ccd138a843196fa80e72c4f2 parent 3095d9d
8 changed files +259−74
ModifiedSound/sound.pas +7−1
@@ -132,6 +132,7 @@ procedure SoundUpdate(cycles: integer);
132 procedure SoundSetCycles(n: integer); 132 procedure SoundSetCycles(n: integer);
133 133
134 function SoundBufferTooFull: Boolean; 134 function SoundBufferTooFull: Boolean;
135 function SoundBufferSize: Integer;
135 136
136 var 137 var
137 soundEnable: boolean; 138 soundEnable: boolean;
@@ -155,7 +156,7 @@ implementation
155 uses vars, bass; 156 uses vars, bass;
156 157
157 const 158 const
158 VolumeDecreaseCoeff = 6; // It's really loud otherwise... 159 VolumeDecreaseCoeff = 1; // It's really loud otherwise...
159 playbackFrequency = 44100; 160 playbackFrequency = 44100;
160 sampleCycles: longint = (8192 * 1024) div playbackFrequency; 161 sampleCycles: longint = (8192 * 1024) div playbackFrequency;
161 TooFullThreshold: DWORD = (playbackFrequency div 10)*sizeof(single); //0.1s 162 TooFullThreshold: DWORD = (playbackFrequency div 10)*sizeof(single); //0.1s
@@ -169,6 +170,11 @@ begin
169 Result := BASS_ChannelGetData(PlayStream, nil, BASS_DATA_AVAILABLE) > TooFullThreshold; 170 Result := BASS_ChannelGetData(PlayStream, nil, BASS_DATA_AVAILABLE) > TooFullThreshold;
170 end; 171 end;
171 172
173 function SoundBufferSize: Integer;
174 begin
175 Result := BASS_ChannelGetData(PlayStream, nil, BASS_DATA_AVAILABLE);
176 end;
177
172 procedure EnableSound; 178 procedure EnableSound;
173 begin 179 begin
174 if soundEnable then 180 if soundEnable then
Addeddriver-format.txt +30−0
@@ -0,0 +1,30 @@
1 Note instr effect
2 0000.0000-0000-0000.0000.0000
3
4 Stuff to do:
5
6 - Make tick stuff happen on ticks other than zero (and multiple times)
7 - Get rid of popping and pushing HL
8
9 If note value is greater than 71 then it's considered to be an empty note cell.
10
11 00 is an empty instrument cell.
12
13 000 is an empty effect cell.
14
15 0 - Arpeggio
16 1 - Slide up
17 2 - Slide down
18 3 - Toneporta
19 4 - Vibrato
20 5 - Toneporta + volslide
21 6 - Vibrato + volslide
22 7 - Tremolo
23 8 - Set panning
24 9 - Set duty cycle
25 A - Volslide
26 B - Position jump
27 C - Set volume
28 D - Pattern break
29 E - Note cut
30 F - Set speed
Addeddriver.sav +0−0
File metadata changed without textual changes.
Modifieddriver.z80 +220−73
@@ -6,18 +6,12 @@ include "constants.inc"
6 include "debug_macros.inc" 6 include "debug_macros.inc"
7 7
8 ; Constants 8 ; Constants
9 STACK_SIZE EQU $200 9 STACK_SIZE EQU 20
10 SCREEN_WIDTH_TILES EQU 20 10 ;; Stack starts at $FFFE
11 CANVAS_WIDTH_TILES EQU 32
12 SCREEN_HEIGHT_TILES EQU 18
13 CANVAS_HEIGHT_TILES EQU 32
14 11
15 PATTERN_LENGTH EQU 64 12 PATTERN_LENGTH EQU 64
16 TICKS EQU 7 13 TICKS EQU 7
17 14
18 ;; Macros
19
20
21 ; $0000 - $003F: RST handlers. 15 ; $0000 - $003F: RST handlers.
22 16
23 SECTION "restarts", ROM0[$0000] 17 SECTION "restarts", ROM0[$0000]
@@ -79,7 +73,7 @@ SECTION "P10-P13 signal low edge interrupt", ROM0[$0060]
79 reti 73 reti
80 74
81 ; Reserved stack space 75 ; Reserved stack space
82 SECTION "Stack", WRAM0[$d000 - STACK_SIZE] 76 SECTION "Stack", HRAM[$FFFE - STACK_SIZE]
83 ds STACK_SIZE 77 ds STACK_SIZE
84 78
85 79
@@ -102,8 +96,8 @@ DB "NF"
102 SECTION "Song Data", ROM0 96 SECTION "Song Data", ROM0
103 ;; order_cnt is the number of orders times 2 97 ;; order_cnt is the number of orders times 2
104 order_cnt: db 2 98 order_cnt: db 2
105 order1: dw unreal ;; unreal;, lunawaves, lunawaves, unreal 99 order1: dw empty ;; unreal ;; unreal;, lunawaves, lunawaves, unreal
106 order2: dw empty, unreal, unreal, lunawaves 100 order2: dw unreal ;; empty, unreal, unreal, lunawaves
107 order3: dw empty, lunawaves, lunawaves, unreal 101 order3: dw empty, lunawaves, lunawaves, unreal
108 order4: dw empty, lunawaves, lunawaves, unreal 102 order4: dw empty, lunawaves, lunawaves, unreal
109 103
@@ -136,16 +130,41 @@ pattern4: dw
136 130
137 current_order: dw 131 current_order: dw
138 132
139 channel_periods: 133 ;; Reset to this once done screwing with channel values
140 channel_period1: dw 134 old_stack_pointer: dw
141 channel_period2: dw 135
142 channel_period3: dw 136 channels:
143 channel_period4: dw
144 137
145 toneporta_targets: 138 ;;;;;;;;;;;
139 ;;Channel 1
140 ;;;;;;;;;;;
141 jumpback1: dw
142 channel1:
143 channel_period1: dw
146 toneporta_target1: dw 144 toneporta_target1: dw
145
146 ;;;;;;;;;;;
147 ;;Channel 2
148 ;;;;;;;;;;;
149 jumpback2: dw
150 channel2:
151 channel_period2: dw
147 toneporta_target2: dw 152 toneporta_target2: dw
153
154 ;;;;;;;;;;;
155 ;;Channel 3
156 ;;;;;;;;;;;
157 jumpback3: dw
158 channel3:
159 channel_period3: dw
148 toneporta_target3: dw 160 toneporta_target3: dw
161
162 ;;;;;;;;;;;
163 ;;Channel 4
164 ;;;;;;;;;;;
165 jumpback4: dw
166 channel4:
167 channel_period4: dw
149 toneporta_target4: dw 168 toneporta_target4: dw
150 169
151 row: ds 1 170 row: ds 1
@@ -213,6 +232,18 @@ _addr = _addr + 1
213 ld a, $77 232 ld a, $77
214 ldh [rAUDVOL], a 233 ldh [rAUDVOL], a
215 234
235 ;; Setup jumpback addresses for the stack hack
236 load_jumpbacks: MACRO
237 ld a, LOW(\2)
238 ld [\1], a
239 ld a, HIGH(\2)
240 ld [\1+1], a
241 ENDM
242 load_jumpbacks jumpback1, after_effect1
243 load_jumpbacks jumpback2, after_effect2
244 load_jumpbacks jumpback3, after_effect3
245 load_jumpbacks jumpback4, after_effect4
246
216 ld c, 0 ;; Current order index 247 ld c, 0 ;; Current order index
217 call _refresh_patterns 248 call _refresh_patterns
218 249
@@ -297,87 +328,152 @@ _lookup_note:
297 ccf ;; uh....? 328 ccf ;; uh....?
298 ret 329 ret
299 330
331 strip_instrument: MACRO
332 ld a, b
333 and %00001111
334 cp $3
335 ENDM
336
337 _update_channel:
338 ;; Call with:
339 ;; Channel in B
340 ;; Note tone in HL
341 ;; Volume in D
342
343 ;; TODO: Turn this into a jump table? Or find some other way to optimize
344 ld a, b
345
346 cp 0
347 jp z, _update_channel1
348 cp 1
349 jp z, _update_channel2
350 cp 2
351 jp z, _update_channel3
352 cp 3
353 jp z, _update_channel4
354
355 _update_channel1:
356 ld a, l
357 ldh [rAUD1LOW], a
358 ld a, h
359 ldh [rAUD1HIGH], a
360 ret
361 _update_channel2:
362 ld a, l
363 ldh [rAUD2LOW], a
364 ld a, h
365 ldh [rAUD2HIGH], a
366 ret
367 _update_channel3:
368
369 ret
370 _update_channel4:
371
372 ret
373
300 _playnote1: 374 _playnote1:
301 ;; Call with: 375 ;; Call with:
302 ;; Note value in H and L 376 ;; Note value in H and L
303 ;; instrument + effect code in B 377 ;; instrument + effect code in B
304 378
305 ;; Play a note on channel 1 (square wave) 379 ;; Play a note on channel 1 (square wave)
380 strip_instrument
306 381
307 ld a, b
308 and %00001111
309 cp $3
310 jp nz, .no_toneporta 382 jp nz, .no_toneporta
311 ld a, h 383 ld a, h
312 ld [toneporta_target1], a
313 ld a, l
314 ld [toneporta_target1+1], a 384 ld [toneporta_target1+1], a
385 ld a, l
386 ld [toneporta_target1], a
315 ret 387 ret
316 388
317 .no_toneporta: 389 .no_toneporta:
318 ; ld a, %01111001 390 ld a, %10111111
319 ; ld [rAUD1SWEEP], a 391 ld [rAUD1LEN], a
320 392
321 ld a, %10111111 ; $10
322 ldh [rAUD1LEN], a
323 ld a, %11110000 393 ld a, %11110000
324 ldh [rAUD1ENV], a 394 ld [rAUD1ENV], a
325 395
326 ld a, l 396 ld a, l
327 ld [channel_period1+1], a 397 ld [channel_period1], a
328 ldh [rAUD1LOW], a 398 ld [rAUD1LOW], a
329 399
330 ld a, h 400 ld a, h
331 ld [channel_period1], a 401 ld [channel_period1+1], a
332 or %10000000 402 or %10000000
333 ldh [rAUD1HIGH], a 403 ld [rAUD1HIGH], a
334 404
335 ret 405 ret
336 406
337 _playnote2: 407 _playnote2:
338 ;; Call with: 408 ;; Call with:
339 ;; Note value in H and L 409 ;; Note value in H and L
410 ;; instrument + effect code in B
340 411
341 ;; Play a note on channel 2 (square wave) 412 ;; Play a note on channel 2 (square wave)
413 strip_instrument
342 414
415 jp nz, .no_toneporta
416 ld a, h
417 ld [toneporta_target2+1], a
418 ld a, l
419 ld [toneporta_target2], a
420 ret
421
422 .no_toneporta:
343 ld a, %00111111 ; $10 423 ld a, %00111111 ; $10
344 ldh [rAUD2LEN], a 424 ld [rAUD2LEN], a
345 ld a, %11110000 425 ld a, %11110000
346 ldh [rAUD2ENV], a 426 ld [rAUD2ENV], a
347 427
348 ld a, l 428 ld a, l
349 ldh [rAUD2LOW], a 429 ld [channel_period2], a
430 ld [rAUD2LOW], a
350 ld a, h 431 ld a, h
432 ld [channel_period2+1], a
351 or %10000000 433 or %10000000
352 ldh [rAUD2HIGH], a 434 ld [rAUD2HIGH], a
353 ret 435 ret
354 436
355 _playnote3: 437 _playnote3:
356 ;; Call with: 438 ;; Call with:
357 ;; Note value in H and L 439 ;; Note value in H and L
440 ;; instrument + effect code in B
358 441
359 ;; Play a note on channel 3 (waveform) 442 ;; Play a note on channel 3 (waveform)
443 strip_instrument
444
445 jp nz, .no_toneporta
446 ld a, h
447 ld [toneporta_target2+1], a
360 ld a, l 448 ld a, l
361 ldh [rAUD3LOW], a 449 ld [toneporta_target2], a
450 ret
451
452 .no_toneporta:
453 ld a, l
454 ld [channel_period3], a
455 ld [rAUD3LOW], a
362 ld a, h 456 ld a, h
457 ld [channel_period3+1], a
363 or %10000000 458 or %10000000
364 ldh [rAUD3HIGH], a 459 ld [rAUD3HIGH], a
365 ret 460 ret
366 461
367 _playnote4: 462 _playnote4:
368 ;; Call with: 463 ;; Call with:
369 ;; Note value in H and L 464 ;; Note value in H and L
465 ;; instrument + effect code in B
370 466
371 ;; Play a "note" on channel 4 (noise) 467 ;; Play a "note" on channel 4 (noise)
372 468
373 ld a, %00011111 469 ld a, %00011111
374 ld [rAUD4LEN], a 470 ld [rAUD4LEN], a
375 ld a, %11110001 471 ld a, %11110001
376 ldh [rAUD4ENV], a 472 ld [rAUD4ENV], a
377 ld a, l; %01000100 473 ld a, l; %01000100
378 ldh [rAUD4POLY], a 474 ld [rAUD4POLY], a
379 ld a, %10000000 475 ld a, %10000000
380 ldh [rAUD4GO], a 476 ld [rAUD4GO], a
381 477
382 ret 478 ret
383 479
@@ -394,6 +490,8 @@ loadShort: MACRO
394 ld \2, a 490 ld \2, a
395 ENDM 491 ENDM
396 492
493
494
397 _doeffect: 495 _doeffect:
398 ;; Call with: 496 ;; Call with:
399 ;; B: instrument nibble + effect type nibble 497 ;; B: instrument nibble + effect type nibble
@@ -442,6 +540,10 @@ _doeffect:
442 jp fx_no_op ;fx_set_speed ;Fxy 540 jp fx_no_op ;fx_set_speed ;Fxy
443 541
444 fx_no_op: 542 fx_no_op:
543 dec sp
544 dec sp
545 dec sp
546 dec sp
445 ret 547 ret
446 548
447 fx_toneporta: 549 fx_toneporta:
@@ -451,25 +553,24 @@ fx_toneporta:
451 553
452 ;; free registers: A, D, E, H, L 554 ;; free registers: A, D, E, H, L
453 555
454 ; Do nothing on the noise channel 556 pop hl ; Load channel_period
455 ; ld a, 4 557 pop de ; Load toneporta_target
456 ; cp e
457 ; ret z
458 558
459 ld a, [channel_period1+1] 559 ; ld a, [channel_period2]
460 ld l, a 560 ; ld l, a
461 ld a, [channel_period1] 561 ; ld a, [channel_period2+1]
462 ld h, a 562 ; ld h, a
463 563
464 ld a, [toneporta_target1+1] 564 ; ld a, [toneporta_target2]
465 ld e, a 565 ; ld e, a
466 ld a, [toneporta_target1] 566 ; ld a, [toneporta_target2+1]
467 ld d, a 567 ; ld d, a
468 568
469 ;; Comparing which direction to move the current value 569 ;; Comparing which direction to move the current value
470 ;; TODO: Optimize this!!!! 570 ;; TODO: Optimize this!!!!
471 571
472 ;; Compare high byte 572 ;; Compare high byte
573 ld a, d
473 cp h 574 cp h
474 jp c, .subtract ; target is less than the current period 575 jp c, .subtract ; target is less than the current period
475 jp z, .high_byte_same 576 jp z, .high_byte_same
@@ -480,10 +581,12 @@ fx_toneporta:
480 jp c, .subtract ; the target is less than the current period 581 jp c, .subtract ; the target is less than the current period
481 jp z, .done ; both nibbles are the same so no portamento 582 jp z, .done ; both nibbles are the same so no portamento
482 .add: 583 .add:
483 ld b, 0 584 ld a, c
484 ; ld e, c 585 add a, l
485 586 ld l, a
486 add hl, bc 587 adc a, h
588 sub l
589 ld h, a
487 590
488 ld a, d 591 ld a, d
489 cp h 592 cp h
@@ -516,16 +619,23 @@ fx_toneporta:
516 ld h, d 619 ld h, d
517 ld l, e 620 ld l, e
518 .done: 621 .done:
519 ld a, l 622 push de
520 ld [channel_period1+1], a
521 ldh [rAUD1LOW], a
522 623
523 ld a, h 624 ; ;; Write new channel period
524 ld [channel_period1], a 625 push hl
525 ldh [rAUD1HIGH], a
526 626
527 .do_nothing: 627 dec sp
528 ret 628 dec sp
629 dec sp
630 dec sp
631
632 ; ld a, l
633 ; ld [channel_period2], a
634
635 ; ld a, h
636 ; ld [channel_period2+1], a
637
638 jp _update_channel
529 639
530 _dosound: 640 _dosound:
531 ld a, [tick] 641 ld a, [tick]
@@ -533,7 +643,7 @@ _dosound:
533 jp nz, .process_effects 643 jp nz, .process_effects
534 644
535 ;; We're on tick zero, so lookup and play the notes, 645 ;; We're on tick zero, so lookup and play the notes,
536 ;; then continue as usual 646 ;; then return
537 647
538 ;; For each call of _lookup_note we push an address to jump to, 648 ;; For each call of _lookup_note we push an address to jump to,
539 ;; and the procedure will discard its normal return value if it needs to 649 ;; and the procedure will discard its normal return value if it needs to
@@ -559,20 +669,57 @@ _dosound:
559 .after_note3: 669 .after_note3:
560 loadShort pattern4, d, e 670 loadShort pattern4, d, e
561 call _lookup_note 671 call _lookup_note
562 jp nc, .process_tick 672 jp nc, process_tick
563 call _playnote4 673 call _playnote4
564 674
565 jp .process_tick 675 jp process_tick
566 676
567 .process_effects: 677 .process_effects:
678
679 restore_stack_pointer: MACRO
680 ld hl, old_stack_pointer+1
681 ld a, [hl-]
682 ld l, [hl]
683 ld h, a
684 ld sp, hl
685 ENDM
686
568 ;; Only do effects if not on tick zero 687 ;; Only do effects if not on tick zero
688 ld [old_stack_pointer], sp
689
569 loadShort pattern1, d, e 690 loadShort pattern1, d, e
570 call _lookup_note 691 call _lookup_note
692 ld e, 0
693 ld sp, channel1
694 jp _doeffect
695
696 after_effect1:
697 restore_stack_pointer
698
699 loadShort pattern2, d, e
700 call _lookup_note
571 ld e, 1 701 ld e, 1
572 call _doeffect 702 ld sp, channel2
703 jp _doeffect
704
705 after_effect2:
706 restore_stack_pointer
707
708 ; loadShort pattern3, d, e
709 ; call _lookup_note
710 ; ld e, 2
711 ; call _doeffect
712
713 after_effect3:
714
715 ; loadShort pattern4, d, e
716 ; call _lookup_note
717 ; ld e, 3
718 ; call _doeffect
573 719
720 after_effect4:
574 721
575 .process_tick: 722 process_tick:
576 ld a, [tick] 723 ld a, [tick]
577 inc a 724 inc a
578 725
@@ -591,7 +738,7 @@ _newrow:
591 ld a, [row] 738 ld a, [row]
592 inc a 739 inc a
593 cp PATTERN_LENGTH 740 cp PATTERN_LENGTH
594 jr nz, noreset 741 jr nz, .noreset
595 742
596 ;; Increment order and change loaded patterns 743 ;; Increment order and change loaded patterns
597 ld a, [order_cnt] 744 ld a, [order_cnt]
@@ -599,9 +746,9 @@ _newrow:
599 ld a, [current_order] 746 ld a, [current_order]
600 add a, 2 747 add a, 2
601 cp b 748 cp b
602 jp nz, update_current_order 749 jp nz, .update_current_order
603 ld a, 0 750 ld a, 0
604 update_current_order: 751 .update_current_order:
605 ld [current_order], a 752 ld [current_order], a
606 ld c, a 753 ld c, a
607 754
@@ -611,7 +758,7 @@ update_current_order:
611 ld [row], a 758 ld [row], a
612 reti 759 reti
613 760
614 noreset: 761 .noreset:
615 ld [row], a 762 ld [row], a
616 763
617 reti 764 reti
Addedtest_wave.raw +1−0
@@ -0,0 +1 @@
1 iiiiK[i-i����-���yi�iiiiiii
Addedtest_wave3.raw +1−0
@@ -0,0 +1 @@
1 ``fEbj�*�g`fff