-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeaker.asm
More file actions
259 lines (193 loc) · 4.23 KB
/
Copy pathspeaker.asm
File metadata and controls
259 lines (193 loc) · 4.23 KB
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
CPU 8086
org 100h
; Notas musicales
; Frecuencia, tercera octava
%define _C 131
%define _C# 139
%define _D 147
%define _D# 155
%define _E 165
%define _F 175
%define _F# 185
%define _G 196
%define _G# 208
%define _A 220
%define _A# 233
%define _B 245
;
%define freqosc 1193180
; macro mus(n,o)
;
; n = frecuencia de la nota en tercera octava
; o = numero de octava que se desea
;
; Genera un numero de conteo para que el oscilador genere una frecuencia
; segun la nota musical y la octava que se le proporcione
;%define mus(n,o) ( freqosc / ( n << ( o−3 ) ) )
%define mus(n,o) freqosc / (n << (o - 3))
%macro SpeakerOn 0
in al, 61h
or al, 00000011b
out 61h, al
mov word [estadospeaker], 1
%endmacro
%macro SpeakerOff 0
in al, 61h
and al, 11111100b
out 61h, al
mov word [estadospeaker], 0
%endmacro
%macro setFreqSpeak 0
; Establece frecuencia del timer 2 (bocinas)
; bx => frecuencia
mov al, 0b6h
out 43h, al
mov al, bl
out 42h, al
mov al, bh
out 42h, al
%endmacro
section .text
start:
; 1 .- Guardar Rutina de timer del usuario 1C (Normalmente es un 'iret')
mov al,1Ch
mov ah,35h
int 21h
mov [timer_int_old_off], bx
mov [timer_int_old_seg], es
; inicia musica
; mov bx, 4554d
; setFreqSpeak
; SpeakerOn
mov ax, partitura
mov word [iniciopart], ax
; 2 .- Registrar nueva rutina de interrupción del teclado
mov al, 1Ch
mov ah, 25h
mov bx, cs
mov ds, bx
mov dx, rutinatimer
int 21h
ciclo:
mov ah, 1 ; "Get keystroke status"
int 16h
jz ciclo
mov ah, 0
int 16h
fin:
; 0 .- Desconectar Speaker
SpeakerOff
; 1 .- Reestablecer rutina original de timer del usuario 1C
mov dx,[timer_int_old_off]
mov ax,[timer_int_old_seg]
mov ds,ax
mov al,1Ch
mov ah,25h
int 21h
; 2 .- Salir al sistema
int 20h
rutinatimer:
; 1.- cargar segmento de datos (igual al segmento de codigo)
mov bx, cs
mov ds, bx
.carganota:
; 2.- cargar posicion de nota actual en la partitura
mov word dx, [iniciopart]
mov si, dx
mov word bx, [notapos]
shl bx, 1
shl bx, 1
lea si, [si + bx]
lodsw
; 3.- Verificar que no sea fin de partitura
test ax, ax
jnz .sig1
; 3.1 Si es el fin de partitura, volver a comenzar
xor bx, bx
mov word [notapos], bx
mov word [notatick], bx
jmp .carganota
.sig1:
; 4.- Verificar timer tick
mov word bx, [notatick]
test bx, bx
jnz .sig2
; Si tick es cero, tocar nota nueva
lodsw
test ax, ax
jnz .sonido
; si nota (ax) es cero, apagar speaker
SpeakerOff
jmp .inctick
.sonido:
; Si no es cero, reproducir en dicha frecuencia
mov bx, ax
setFreqSpeak
SpeakerOn
jmp .inctick
.sig2:
; Verificar si termina nota actual
cmp ax, bx
jge .inctick
mov word ax, [notapos]
inc ax
mov word [notapos], ax
xor bx, bx
mov word [notatick], bx
jmp .carganota
.inctick:
mov word bx, [notatick]
inc bx
mov word [notatick], bx
.fin:
iret
section .data
; program data
timer_int_old_off: dw 0
timer_int_old_seg: dw 0
; Variables para reproducir melodia
notapos: dw 0
notatick: dw 0
iniciopart: dw 0
estadospeaker: dw 0
; duracion, nota
partitura: ; Si la nota es cero: Guardar silencio
dw 4, mus( _C, 4 )
dw 2, mus( _D, 4 )
dw 1, mus( _E, 3 )
dw 1, mus( _F#, 3 )
dw 2, mus( _F, 3 )
dw 4, mus( _E, 4 )
dw 2, mus( _D, 4 )
dw 1, mus( _E, 3 )
dw 1, mus( _F#, 3 )
dw 2, mus( _F, 3 )
dw 4, mus( _G#, 4 )
dw 2, mus( _F#, 4 )
dw 1, mus( _E, 3 )
dw 1, mus( _F#, 3 )
dw 2, mus( _F, 3 )
dw 4, mus( _B, 4 )
dw 2, mus( _C, 5 )
dw 1, mus( _E, 3 )
dw 1, mus( _F#, 3 )
dw 2, mus( _F, 3 )
dw 0, 0 ; Si la duración es cero: Fin de la música
nuevapart:
dw 4, mus( _C, 4 )
dw 4, mus( _D, 4 )
dw 2, mus( _F, 4 )
dw 2, mus( _E, 4 )
dw 4, mus( _C, 5 )
dw 4, mus( _D, 5 )
dw 2, mus( _F, 5 )
dw 2, mus( _E, 5 )
dw 4, mus( _C, 6 )
dw 4, mus( _D, 6 )
dw 2, mus( _F, 6 )
dw 2, mus( _E, 6 )
dw 4, mus( _C, 7 )
dw 4, mus( _D, 7 )
dw 2, mus( _F, 7 )
dw 2, mus( _E, 7 )
dw 0, 0 ; Si la duración es cero: Fin de la música