aboutsummaryrefslogtreecommitdiff
path: root/sdcc/beeper.z80
blob: b5b82e7dc23356d4b2bce035bde7ac70525acb8c (plain)
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
; Beeper engine
; Copyright (C) 2021 by Juan J. Martinez <jjm@usebox.net>
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
;
.globl _beeper_init
.globl _beeper_queue
.globl _beeper_play

_beeper_init::
        di
        ld (sfx_data), hl
        xor a
        ld (sfx_type), a
        ei
        ret

_beeper_queue::
        di
        ld a, l
        call queue_next
        ei
        ret

queue_next:
        ld (sfx_type), a
        or a
        ret z

        dec a

        ld hl, (sfx_data)
        ld c, l
        ld b, h

        ld h, #0
        ld l, a
        ld d, h
        ld e, l
        add hl, hl
        add hl, hl
        add hl, de
        add hl, bc

        ld de, #sfx_type
        ld bc, #5
        ldir
        ret

_beeper_play::
        ld a, (sfx_type)
        or a
        ret z

        dec a
        jr z, tone

        dec a
        ; shouldn't happen!
        ret nz

        ; noise
        ld a, (sfx_freq)
        ld d, a

        ld b, #0

noise_loop:
        call rnd
        and #0x10
        ; FIXME: border ?
        out (0xfe), a

        ld c, d
noise_freq_loop:
        dec b
        jr z, noise_done
        dec c
        jr nz, noise_freq_loop
        jr noise_loop

tone:
        ld a, (sfx_freq)
        ld d, a

        xor a
        ld b, a

tone_loop:
        ; FIXME: border ?
        out (0xfe), a
        xor #0x10

        ld c, d
freq_loop:
        dec b
        jr z, tone_done
        dec c
        jr nz, freq_loop
        jr tone_loop

tone_done:
noise_done:
        ld a, (sfx_next)
        ld hl, #sfx_frames
        dec (hl)
        jr z, queue_next

        ; freq change (slide)
        ld a, (sfx_freq_chg)
        add d
        ld (sfx_freq), a

        ret

rnd:
        ld hl, #0xf3a1
        ld a, h
        rra
        ld a, l
        rra
        xor h
        ld h, a
        ld a, l
        rra
        ld a, h
        rra
        xor l
        ld l, a
        xor h
        ld h, a
        ld (rnd + 1), hl
        ret

sfx_type:       .ds 1
sfx_frames:     .ds 1
sfx_freq:       .ds 1
sfx_freq_chg:   .ds 1
sfx_next:       .ds 1

sfx_data: .ds 2