Нашел интересную статью в журнале Outlet 21. Вытащил текст вручную, потому что было трудно разобраться с процедурой, хранящейся по адресу $8708.
PROGRAMMING IN MACHINE CODE
BY NICK LEWIS
---------------------------
Welcome once again to OUTCLASS.
This unfortunately is my last
edition, for now anyway. This is
due to the fact that I have the
good old GCSE exams coming my
way this June. Anyway, enough of
my problems - what's happening
this month? A while ago I set
the task of writing a routine to
test for the BREAK key. This is
what you may have come up with:
10 LOOP LD A,#7E
20 IN A,(254)
30 AND #01
40 JR NZ,LOOP
50 RET
The above routine tests the two
half rows, one with space in and
another with caps shift in.Then,
by using AND logic, I masked off
unwanted keys.If the remaining's
zero then both keys should have
been pressed and this should
then result in a return. The
problem with the routine is that
it will return if just space or
caps shift are pressed instead
of both. This should not be the
case as data line 0(bit 0)should
be high if either key is not
pressed. I don't know why it
doesn't work as I know nothing
about the Spectrum hardware.
The only way I see of getting
around this problem is to test
caps shift and space separately.
The routine below does just
that:
10 SPACE LD A,#7F
20 IN A,(254)
30 AND #01
40 JR Z,CAPS
50 JR SPACE
60 CAPS LD A,#FE
70 IN A,(254)
80 AND #01
90 JR NZ,SPACE
100 RET
The routine firstly tests for
the space key, then if the test
is positive it tests for the
caps shift key. If both tests
are positive a return occurs. If
one or both tests are negative
the routine jumps to SPACE. The
routine does look slightly messy
just to test for BREAK but I see
no way round it.
THIS MONTH
Being the last month I was
undecided of what to do. I have
actually had a request from an
OUTLET reader asking me if I
would do a feature on scrolling.
A detailed look at scrolling
would take more than one issue
so sorry, but I can't do it this
time. Brian also asked me about
number crunching programs. I
presume he means floating point
calculators. I cannot find any
information on how a computer
stores floating point numbers so
I cannot be of any help. After
much thinking I decided to have
another look at sound. Last time
I was really concentrating on
ports and ROM routines so didn't
go into much detail.
PITCH,LOUDNESS AND
QUALITY OF SOUND
A noise has three main factors
which affect the way it sounds.
Before I can point out the
limitations of the Spectrum
sound you need to be aware of:
(1) Pitch
(2) Loudness
(3) Quality
PITCH
The pitch of a sound depends on
its frequency. The higher the
pitch of a note the higher the
frequency. Frequency means how
many times something operates in
a second. For example the Z80
processor has a frequency of
4MHz or 4,000,000 Hz. This means
it cycles four million times a
second.
LOUDNESS
The loudness of a sound depends
on the amplitude of a sound
wave. The higher the amplitude
the louder the note. The 48K
Spectrum has a set volume so can
only produce square sound waves
like the following diagram:
+++++++++ +++++++++
+ + + +
+ + + +
++++ +++++++++ +++++
The 128K series of Spectrums
have a sound chip(AY series)that
can alter the loudness so can
produce more natural sound waves
like this:
+++ +++
++ ++ ++ ++
+ + + +
++ ++ ++ ++
+++ +++
Because the Spectrum 48K can't
alter the loudness it can't sim-
ulate an instrument like a piano
where the sound fades away.
QUALITY
All musical instruments sound
different even if they play at
the same volume and play the
same note.The reason why instru-
ments differ is because they
produce a different quality of
note. The last wave we looked at
played a "pure" note. That is a
sound with one frequency.A sound
with just one frequency is
called a fundamental frequency
and looks like a sine wave:
+++ +++
++ ++ ++ ++
+ + + +
++ ++ ++ ++
+++ +++
An instrument produces a sound
consisting of the fundamental
note and harmonics. A harmonic
is a multiple of the fundamental
frequency. So for example:
Middle C is 512 Hz (fundamental)
First Harmonic is 512*2 = 1024Hz
Second " " 512*3 = 1536Hz
All these frequencies combine to
give a unique wave pattern for a
particular sound:
++
+ +
+ + +
+ ++ + +
+ +++ + ++ + +
++ +++ +++ ++ ++
Making the sound nature involves
changing volume as well as
frequency pattern like on the
last wave. The Spectrum 48K can
only change frequency. If you
own a 128K Spectrum you can
change volume. Digitized speech
is made possible by harmonics.
The Spectrum 48K only has one
channel, but because of speed it
can be made to sound like two or
more, introducing harmonics. The
Spectrum 128K has three separate
channels so a much greater range
of sounds can be created.
SOUND GENERATION
That's the theory side of things
so how can we produce sound, and
what benefits have we when using
machine code rather than BASIC?
Sound is made be vibrating air
molecules. A speaker can vibrate
the air molecules for us.It does
this by phyically vibrating.The
faster it vibrates the higher
pitch we get. The extent of
vibration determines the volume.
The Spectrum 48K can either push
the speaker out or pull the
speaker in. To push it out use:
LD A,16
OUT (254),A
And to pull it back in again:
LD A,0
OUT (254),A
Continual pulling and pushing
causes the vibration. The Z80
processor can actually go so
fast that it stops the speaker
moving. It does this because by
the time the speaker starts to
6ush out, the Z80 has sent a
signal to come back in. Then
when the speaker starts going in
the Z80 sends a signal to push
out. This stops the speaker
moving in any direction. To stop
this happening we need a pause
between in and out pulses. It is
possible to simulate volume by
only giving the speaker time to
move in and out a certain
distance. This is not very
practical because speed effects
pitch. Alright for sound effects
though.
I have designed a master sound
generator which gives you the
following variables to alter.
H reg.
++++++++++ ++++++++++ ++++++
+ + + + +
+ + + + +
+++ ++++ ++++
L reg.
The H register determines the
period of time for the speaker
to be pushed. The L register
determines the period of time
for the speaker to be pulled.
The BC register pair determines
how many times the speaker is to
be pulled and pushed. You can
see the H and L registers in the
square wave above. This is how
the looping works:
------BC register-------
! !
! --L register-- !
! ! ! !
! ! SPEAKER IN ! !
! ! ! !
! !---LOOP-2---! !
! !
! --H register-- !
! ! ! !
! ! SPEAKER OUT! !
! ! ! !
! ----LOOP-3---- !
! !
---------LOOP-1---------
10 SOUND DI
We always disable interrupts to
give us a constant time cycle.
20 LOOP1 LD A,16
30 OUT (254),A
Push the speaker out!
40 PUSH HL
50 LOOP2 DEC L
60 LD A,L
70 JR NZ,LOOP2
80 POP HL
LOOP2 is the time period for the
speaker to be out. Note HL is
pushed to the stack as it is
corrupted.
90 LD A,0
100 OUT (254),A
Pull the speaker in!
110 PUSH HL
120 LOOP3 DEC H
130 LD A,H
140 JR NZ,LOOP3
150 POP HL
LOOP3 determines the time period
for the speaker to be in. Again
HL is pushed to the stack as it
is corrupted.
160 DEC BC
The BC counter is decremented.
Remember this determines how
many times the speaker vibrates.
170 LD A,B
180 OR C
Logically OR BC into A so we can
test for zero (end of loop).
190 JR NZ,LOOP1
If non-zero then vibrate again.
200 EI
210 RET
Enable the interrupts again and
return. I am going to use the
SOUND program as a subroutine so
I need to save the values of HL
and BC as they may be used in
the control program.
15 PUSH HL
16 PUSH BC
195 POP BC
196 POP HL
The SOUND generator is now
complete so we can test it with
the following control program.
First renumber the listing. If
using JACKSOFT enter N1000,10.
10 LD BC,200 ;period
20 LD H,100 ;pitch1
30 LD L,H ;pitch2
40 CALL SOUND
50 RET
If you run the program you
should hear a short note. The
square wave form for that note
looks like:
++++++ ++++++ ++++++ ++
+ + + + + + +
+ + + + + + +
+ ++++++ ++++++ ++++++
Both H and L are equal. We could
alter the pitch by altering the
frequency.
10 LD BC,100 ;period
20 LD H,150 ;pitch1
30 LD L,H ;pitch2
This will give a lower note
because the frequency is lower.
H is loaded with a larger number
to increase the time between
speaker alternating giving the
slower vibration. BC has been
changed because now the
frequency is lower the same
amount of vibrating will take
longer. For example a speaker
vibrating at 100 times a second
for 500 vibrations will take:
1
--- * 500 = 5 seconds
100
BUT a speaker vibrating at 50
times a second for again 500
vibrations will take:
1
--- * 500 = 10 seconds.
50
This means you will need to
experiment with pitch and length
to get your required note.To get
a higher pitch we can use:
10 LD BC,400 ;period
20 LD H,50 ;pitch1
30 LD L,H ;pitch2
I have designed the SOUND
generator so it can independent-
ly control the time period of
when the speaker is pulled or
pushed. We can make a note with
this wave pattern.
++++++++++ ++++++++++ ++++++++++
+ + + + + +
+ + + + + +
+ +++ +++ +
10 LD BC,500 ;period
20 LD H,100 ;pitch1
30 LD L,10 ;pitch2
Pitch 1 and 2 are now different.
What does that mean?
Having control over both states
of the speaker gives a more var-
ied sound. Type in the next dem-
onstration.
10 LD BC,200
20 LD H,100
30 LD L,0
40 LOOP LD A,L
50 ADD A,10
60 LD L,A
70 CP 100
80 RET Z
90 LD A,C
100 SUB 10
110 LD C,A
120 JR LOOP
This program varies the pitch by
only altering the time period
when the speaker is pulled. The
other time period remains the
same. This sounds different to
when we alternate both time
periods. We can, in fact alter
both time periods but in
different ways. The next example
makes one time period longer and
the other time period shorter.
10 LD BC,100
20 LD H,100
30 LD L,1
40 LOOP INC L
50 DEC H
60 LD A,H
70 RET Z
80 CALL SOUND
90 JR LOOP
Run this program and you will
hear the same pitch note. This
is because the overall frequency
of H+L is the same. Try adding
the line:
45 INC L
Run the program again and you
hear a scale. This is because
this time H+L does not equal the
same frequency all the time.
To finish off sound and my time
with OUTLET I have one last
routine. This routine takes the
frequency and slightly changes
it giving a trembling effect.
The routine needs SOUND to be
present.
500 TREM LD A,H
510 ADD A,4
520 LD H,A
530 CALL SOUND
540 LD A,H
550 SUB 7
560 LD H,A
570 CALL SOUND
580 LD A,H
590 ADD A,5
600 LD H,A
610 CALL SOUND
620 LD A,H
630 SUB 2
640 LD H,A
650 CALL SOUND
660 RET
As an example try:
10 LD BC,20
20 LD H,150
30 LD L,H
40 CALL TREM
50 RET
Try the other routines but type
in CALL TREM instead of CALL
SOUND.I will leave you to exper-
iment. Remember to calculate how
long the note or scale is going
to last or you may be in for a
long wait.
To conclude, if you want great
sound, don't get a 48K Spectrum!
A 128K Spectrum is a step up but
still limited. If you need
really good sound get an AMIGA,
don't sell that Spectrum though
- OUTLET needs you!
BY NICK LEWIS
---------------------------
Welcome once again to OUTCLASS.
This unfortunately is my last
edition, for now anyway. This is
due to the fact that I have the
good old GCSE exams coming my
way this June. Anyway, enough of
my problems - what's happening
this month? A while ago I set
the task of writing a routine to
test for the BREAK key. This is
what you may have come up with:
10 LOOP LD A,#7E
20 IN A,(254)
30 AND #01
40 JR NZ,LOOP
50 RET
The above routine tests the two
half rows, one with space in and
another with caps shift in.Then,
by using AND logic, I masked off
unwanted keys.If the remaining's
zero then both keys should have
been pressed and this should
then result in a return. The
problem with the routine is that
it will return if just space or
caps shift are pressed instead
of both. This should not be the
case as data line 0(bit 0)should
be high if either key is not
pressed. I don't know why it
doesn't work as I know nothing
about the Spectrum hardware.
The only way I see of getting
around this problem is to test
caps shift and space separately.
The routine below does just
that:
10 SPACE LD A,#7F
20 IN A,(254)
30 AND #01
40 JR Z,CAPS
50 JR SPACE
60 CAPS LD A,#FE
70 IN A,(254)
80 AND #01
90 JR NZ,SPACE
100 RET
The routine firstly tests for
the space key, then if the test
is positive it tests for the
caps shift key. If both tests
are positive a return occurs. If
one or both tests are negative
the routine jumps to SPACE. The
routine does look slightly messy
just to test for BREAK but I see
no way round it.
THIS MONTH
Being the last month I was
undecided of what to do. I have
actually had a request from an
OUTLET reader asking me if I
would do a feature on scrolling.
A detailed look at scrolling
would take more than one issue
so sorry, but I can't do it this
time. Brian also asked me about
number crunching programs. I
presume he means floating point
calculators. I cannot find any
information on how a computer
stores floating point numbers so
I cannot be of any help. After
much thinking I decided to have
another look at sound. Last time
I was really concentrating on
ports and ROM routines so didn't
go into much detail.
PITCH,LOUDNESS AND
QUALITY OF SOUND
A noise has three main factors
which affect the way it sounds.
Before I can point out the
limitations of the Spectrum
sound you need to be aware of:
(1) Pitch
(2) Loudness
(3) Quality
PITCH
The pitch of a sound depends on
its frequency. The higher the
pitch of a note the higher the
frequency. Frequency means how
many times something operates in
a second. For example the Z80
processor has a frequency of
4MHz or 4,000,000 Hz. This means
it cycles four million times a
second.
LOUDNESS
The loudness of a sound depends
on the amplitude of a sound
wave. The higher the amplitude
the louder the note. The 48K
Spectrum has a set volume so can
only produce square sound waves
like the following diagram:
+++++++++ +++++++++
+ + + +
+ + + +
++++ +++++++++ +++++
The 128K series of Spectrums
have a sound chip(AY series)that
can alter the loudness so can
produce more natural sound waves
like this:
+++ +++
++ ++ ++ ++
+ + + +
++ ++ ++ ++
+++ +++
Because the Spectrum 48K can't
alter the loudness it can't sim-
ulate an instrument like a piano
where the sound fades away.
QUALITY
All musical instruments sound
different even if they play at
the same volume and play the
same note.The reason why instru-
ments differ is because they
produce a different quality of
note. The last wave we looked at
played a "pure" note. That is a
sound with one frequency.A sound
with just one frequency is
called a fundamental frequency
and looks like a sine wave:
+++ +++
++ ++ ++ ++
+ + + +
++ ++ ++ ++
+++ +++
An instrument produces a sound
consisting of the fundamental
note and harmonics. A harmonic
is a multiple of the fundamental
frequency. So for example:
Middle C is 512 Hz (fundamental)
First Harmonic is 512*2 = 1024Hz
Second " " 512*3 = 1536Hz
All these frequencies combine to
give a unique wave pattern for a
particular sound:
++
+ +
+ + +
+ ++ + +
+ +++ + ++ + +
++ +++ +++ ++ ++
Making the sound nature involves
changing volume as well as
frequency pattern like on the
last wave. The Spectrum 48K can
only change frequency. If you
own a 128K Spectrum you can
change volume. Digitized speech
is made possible by harmonics.
The Spectrum 48K only has one
channel, but because of speed it
can be made to sound like two or
more, introducing harmonics. The
Spectrum 128K has three separate
channels so a much greater range
of sounds can be created.
SOUND GENERATION
That's the theory side of things
so how can we produce sound, and
what benefits have we when using
machine code rather than BASIC?
Sound is made be vibrating air
molecules. A speaker can vibrate
the air molecules for us.It does
this by phyically vibrating.The
faster it vibrates the higher
pitch we get. The extent of
vibration determines the volume.
The Spectrum 48K can either push
the speaker out or pull the
speaker in. To push it out use:
LD A,16
OUT (254),A
And to pull it back in again:
LD A,0
OUT (254),A
Continual pulling and pushing
causes the vibration. The Z80
processor can actually go so
fast that it stops the speaker
moving. It does this because by
the time the speaker starts to
6ush out, the Z80 has sent a
signal to come back in. Then
when the speaker starts going in
the Z80 sends a signal to push
out. This stops the speaker
moving in any direction. To stop
this happening we need a pause
between in and out pulses. It is
possible to simulate volume by
only giving the speaker time to
move in and out a certain
distance. This is not very
practical because speed effects
pitch. Alright for sound effects
though.
I have designed a master sound
generator which gives you the
following variables to alter.
H reg.
++++++++++ ++++++++++ ++++++
+ + + + +
+ + + + +
+++ ++++ ++++
L reg.
The H register determines the
period of time for the speaker
to be pushed. The L register
determines the period of time
for the speaker to be pulled.
The BC register pair determines
how many times the speaker is to
be pulled and pushed. You can
see the H and L registers in the
square wave above. This is how
the looping works:
------BC register-------
! !
! --L register-- !
! ! ! !
! ! SPEAKER IN ! !
! ! ! !
! !---LOOP-2---! !
! !
! --H register-- !
! ! ! !
! ! SPEAKER OUT! !
! ! ! !
! ----LOOP-3---- !
! !
---------LOOP-1---------
10 SOUND DI
We always disable interrupts to
give us a constant time cycle.
20 LOOP1 LD A,16
30 OUT (254),A
Push the speaker out!
40 PUSH HL
50 LOOP2 DEC L
60 LD A,L
70 JR NZ,LOOP2
80 POP HL
LOOP2 is the time period for the
speaker to be out. Note HL is
pushed to the stack as it is
corrupted.
90 LD A,0
100 OUT (254),A
Pull the speaker in!
110 PUSH HL
120 LOOP3 DEC H
130 LD A,H
140 JR NZ,LOOP3
150 POP HL
LOOP3 determines the time period
for the speaker to be in. Again
HL is pushed to the stack as it
is corrupted.
160 DEC BC
The BC counter is decremented.
Remember this determines how
many times the speaker vibrates.
170 LD A,B
180 OR C
Logically OR BC into A so we can
test for zero (end of loop).
190 JR NZ,LOOP1
If non-zero then vibrate again.
200 EI
210 RET
Enable the interrupts again and
return. I am going to use the
SOUND program as a subroutine so
I need to save the values of HL
and BC as they may be used in
the control program.
15 PUSH HL
16 PUSH BC
195 POP BC
196 POP HL
The SOUND generator is now
complete so we can test it with
the following control program.
First renumber the listing. If
using JACKSOFT enter N1000,10.
10 LD BC,200 ;period
20 LD H,100 ;pitch1
30 LD L,H ;pitch2
40 CALL SOUND
50 RET
If you run the program you
should hear a short note. The
square wave form for that note
looks like:
++++++ ++++++ ++++++ ++
+ + + + + + +
+ + + + + + +
+ ++++++ ++++++ ++++++
Both H and L are equal. We could
alter the pitch by altering the
frequency.
10 LD BC,100 ;period
20 LD H,150 ;pitch1
30 LD L,H ;pitch2
This will give a lower note
because the frequency is lower.
H is loaded with a larger number
to increase the time between
speaker alternating giving the
slower vibration. BC has been
changed because now the
frequency is lower the same
amount of vibrating will take
longer. For example a speaker
vibrating at 100 times a second
for 500 vibrations will take:
1
--- * 500 = 5 seconds
100
BUT a speaker vibrating at 50
times a second for again 500
vibrations will take:
1
--- * 500 = 10 seconds.
50
This means you will need to
experiment with pitch and length
to get your required note.To get
a higher pitch we can use:
10 LD BC,400 ;period
20 LD H,50 ;pitch1
30 LD L,H ;pitch2
I have designed the SOUND
generator so it can independent-
ly control the time period of
when the speaker is pulled or
pushed. We can make a note with
this wave pattern.
++++++++++ ++++++++++ ++++++++++
+ + + + + +
+ + + + + +
+ +++ +++ +
10 LD BC,500 ;period
20 LD H,100 ;pitch1
30 LD L,10 ;pitch2
Pitch 1 and 2 are now different.
What does that mean?
Having control over both states
of the speaker gives a more var-
ied sound. Type in the next dem-
onstration.
10 LD BC,200
20 LD H,100
30 LD L,0
40 LOOP LD A,L
50 ADD A,10
60 LD L,A
70 CP 100
80 RET Z
90 LD A,C
100 SUB 10
110 LD C,A
120 JR LOOP
This program varies the pitch by
only altering the time period
when the speaker is pulled. The
other time period remains the
same. This sounds different to
when we alternate both time
periods. We can, in fact alter
both time periods but in
different ways. The next example
makes one time period longer and
the other time period shorter.
10 LD BC,100
20 LD H,100
30 LD L,1
40 LOOP INC L
50 DEC H
60 LD A,H
70 RET Z
80 CALL SOUND
90 JR LOOP
Run this program and you will
hear the same pitch note. This
is because the overall frequency
of H+L is the same. Try adding
the line:
45 INC L
Run the program again and you
hear a scale. This is because
this time H+L does not equal the
same frequency all the time.
To finish off sound and my time
with OUTLET I have one last
routine. This routine takes the
frequency and slightly changes
it giving a trembling effect.
The routine needs SOUND to be
present.
500 TREM LD A,H
510 ADD A,4
520 LD H,A
530 CALL SOUND
540 LD A,H
550 SUB 7
560 LD H,A
570 CALL SOUND
580 LD A,H
590 ADD A,5
600 LD H,A
610 CALL SOUND
620 LD A,H
630 SUB 2
640 LD H,A
650 CALL SOUND
660 RET
As an example try:
10 LD BC,20
20 LD H,150
30 LD L,H
40 CALL TREM
50 RET
Try the other routines but type
in CALL TREM instead of CALL
SOUND.I will leave you to exper-
iment. Remember to calculate how
long the note or scale is going
to last or you may be in for a
long wait.
To conclude, if you want great
sound, don't get a 48K Spectrum!
A 128K Spectrum is a step up but
still limited. If you need
really good sound get an AMIGA,
don't sell that Spectrum though
- OUTLET needs you!
Комментарии
Отправить комментарий