Machine Code Made Easy - Part 4


Machine Code Made Easy | 1 | 2 | 3 | 4

Fred 9

Machine Code Made Easy - Part 4

Howdy all! Yes, ‘tis I again - The Wizard ( known as genius to
my friends ). How are you all doing with your assemblers and
this series of articles? Is there anyone reading this at all?
‘Cos if there isn’t I might as well pack up and go home.

Oh well, that’s enough grumbling for 1 issue. On with the show..

A LTTLE BIT MORE ABOUT PORTS & SOME ROUTINES
============================================

I’m rapidly running out of ideas for this column, y’know. So it’s
back to ports I’m afraid. Last time we looked at the memory
management ports, which are the most frequently used in SAM
machine coding. Today I’ll share some little routines to help
this memory managment, and a wee key-scanning jobbie. So,
onwards we go….

Firstly, here are some routines which will page either the #
screen or ROM0/system variables into 0-7FFF :-


SCREENIN IN A,(LMPR) ; Let's store the current status
LD (LMPRSTORE),A ; of LMPR in a store.
IN A,(VMPR) ; Get the screen page number.
AND %00011111 ; Chop of unwanted bits.
OR %00100000 ; Make sure ROM is switched off.
OUT (LMPR),A ; Switch it into 0-7FFF.
RET
SCREEN
OUT LD A,(LMPRSTORE) ; Get whatever LMPR was before we
OUT (LMPR),A ; switched in the screen and put
RET ; it back.
ROM0IN IN A,(LMPR) ; Store the status of LMPR.
LD (LMPRSTORE),A
LD A,%00011111 ; Put page 31* into 0-3FFF and
OUT (LMPR),A ; switch on ROM0.
RET
ROM0
OUT JP SCREENOUT

* By putting page 31 into this section of memory, we put page 0
into the next 16k section. We do this because ROM0 expects the
system variables (page 0) to be here. Page 31 is covered with
the ROM so is not affected. *

These little routines are small and simple but are esential
when you are writing a large piece of code with lots of memory
swapping needed. If you prefer, the routines could be altered to
fiddle with upper memory - it all depends where your calling
code is located.

By the way, if you missed the last issue (where were you?)
LMPR is port 250 and VMPR is port 252. LMPRSTORE can be any
single byte address.

Now, here’s a small keyboard and joystick scanning routine
you can fit to any game or utility. I wrote it eons ago for the
Spectrum and it hasn’t been updated, so only keys common to both
computers are tested. Incidentally, the ROM’s interupts scan
the keyboard costantly and a bit-map of them is produced in page
0 at 5BEE-5BFF. Most of my programs run without interupts ( to
avoid continuously DI and EI-ing ) so this routine is required.


KSCAN LD BC,#0600
KS1 LD A,(HL)
RRA
RRA
RRA
AND %00001111
LD E,A
LD D,0
LD A,(HL)
INC HL
PUSH HL
LD HL,PORTADD
ADD HL,DE
LD D,A
PUSH BC
LD B,(HL)
LD C,254
IN A,(C)
CPL
KS2 LD E,A
POP BC
POP HL
LD A,D
AND %00000111
JR Z,KS4
KS3 RR E
DEC A
JR NZ,KS3
KS4 RR E
RL C
DJNZ KS1
RET
PORTADD DB 247,251,253,254,224,191,127

There you go - there are no comment lines because I want you to work out how it,
um, works all by yourself. I WILL tell you, however, that when you call the routine,
HL must point to a table of values that correspond to the keys you are testing.
On exit each bit of C is HI if that particular key was pressed, otherwise it is LO.

Each value in the table you point HL to is formed from by
taking the row number (see below), multipling it by 8 and adding
the distance the key is from the edge (0-4).


ROW No.s : 1-5 = 0 6-0 = 4 [ REMEMBER, this applies
Q-T = 1 Y-P = 5 to the speccy keyboard
A-G = 2 H-ret = 6 layout. ]
shft-V = 3 B-spc = 7

For example:-


LD HL,KEY
TABLE
CALL KSCAN
.
. rest of code
.
.
KEY_TABLE DB 52,41,40,08,16,56 ; values
^ ^ ^ ^ ^ ^
H O P Q A spc ; corresponding keys

^ ^ ^ ^ ^ ^
5 4 3 2 1 0 ; BIT no. of C

That’s it for this month, folks. I’ll be back in FRED 10 with:

* ROM routines - how to use them in your own progies.
* MATHS in m/c - everything from quadratics to straight lines.
* Putting together a game - part 1 of SPACE INVADERS !!!

I have to go now to finish my English homework so I’ll say goodbye and au revoir!