News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_ervin

OUT commands (help please)

Started by ervin, 14:09, 23 January 11

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ervin

Hi there.

The OUT commands are something I've been wanting to learn to use for some time, but for the life of me I just can't figure them out!

Can someone please explain them to me in a "for dummies" fashion?

I'm particularly interested in reading the keyboard using OUT commands.
For example, I want to be able to tell if the user is pressing the up arrow key.

Also, is it possible to temporarily store data in a port, and access it using OUT or IN commands?

Thanks for any help.

redbox

#1
Quote from: ervin on 14:09, 23 January 11
The OUT commands are something I've been wanting to learn to use for some time, but for the life of me I just can't figure them out!

I learnt about most of this from Grim's website - there are great pages on the IO devices on the CPC with the CRTC and Gate Array being covered in more depth.

Quote from: ervin on 14:09, 23 January 11
I'm particularly interested in reading the keyboard using OUT commands.
For example, I want to be able to tell if the user is pressing the up arrow key.

There is a good keyboard scanning routine on the cpctech website which can be used along with the scancode table on the wiki (which also has a pretty fast routine to scan one bitline).

ervin

#2
Fantastic, thanks!
(And thanks for the very fast reply!)

EDIT: Wow - those articles on Grim's website are absolutely brilliant. Thanks for the link!

arnoldemu

Quote from: ervin on 14:09, 23 January 11
Hi there.

The OUT commands are something I've been wanting to learn to use for some time, but for the life of me I just can't figure them out!

Can someone please explain them to me in a "for dummies" fashion?

I'm particularly interested in reading the keyboard using OUT commands.
For example, I want to be able to tell if the user is pressing the up arrow key.

Also, is it possible to temporarily store data in a port, and access it using OUT or IN commands?

Thanks for any help.

The OUT command is the way you *write* to the hardware.


ld bc,&7f80
ld a,3
out (c),a


BC is used for theI/O  port address, think of it as a way of identifing the part of the hardware you want to talk to.
In this example port 7F80.
Then I load A with the 8-bit data I want to write.

then the out instruction.
OUT (C),A means "write the value in the A register to the I/O port address defined by the value in the BC register".

The hardware normally recognises a particular pattern of bits in the port address, so it is possible to write to the same hardware from more than one I/O address. In addition doing this:


LD BC,&7F8C
OUT (C),C

Is the same as saying "write the value in the C register to the I/O port address defined by the value in the BC register pair".
Now this works, because the hardware in question ignores the lower 8-bits when recognising it's port address.

If you want to read, use the IN instruction in a similar way.

Can you store data temporarily in a port? Depends on the hardware you are writing to.
Some "latch" (e.g. 8255 or AY I/O port) or store the data written, others it only exists at the time the OUT instruction is active and is passed through.

Some hardware allows you to write, but then not to read. (e.g. most of the CRTC registers).

Some hardware is write only, some is read only.

So to know what to do, read the docs ;)

In terms of the keyboard, it's a lengthy process using both the AY and 8255 to talk to it. So it's not simple :(
Same with the AY, that's not so simple. For keyboard it is best to scan the whole thing, and store the state in a buffer, then query the buffer.
Refreshing the buffer each vsync to see the keyboard state update.

Access to 8255, printer, gate-array however is simple.

I hope that has answered some of your questions.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

ervin

#4
Wow - thanks so much for taking the time to write that!
That's probably the best explanation of OUT etc. that I've ever read.

Now I'll understand what all you guys are doing when you use OUTs to set the screen mode and all sorts of other things.

Regarding the keyboard scanning, I reckon I might stick with the firmware method for the time-being!

Thanks again - that's amazingly helpful info.
It should really be put into the wiki itself.

[EDIT]
I've just been perusing this page:
http://www.cpcwiki.eu/index.php/8255

It talks about port B on the 8255 having a VSYNC flag (bit 0).
Would setting this to OFF have any effect on program execution speed?

arnoldemu

#5
Quote from: ervin on 00:31, 24 January 11
Wow - thanks so much for taking the time to write that!
That's probably the best explanation of OUT etc. that I've ever read.
You're welcome!


Quote from: ervin on 00:31, 24 January 11
Regarding the keyboard scanning, I reckon I might stick with the firmware method for the time-being!
Nab the wiki one ;)

Quote from: ervin on 00:31, 24 January 11
It should really be put into the wiki itself.
Ok I could add it later.

Quote from: ervin on 00:31, 24 January 11
  [EDIT]
I've just been perusing this page:
http://www.cpcwiki.eu/index.php/8255

It talks about port B on the 8255 having a VSYNC flag (bit 0).
Would setting this to OFF have any effect on program execution speed?
The 8255 is an I/O chip. It has 3 ports, port A is 8-bit, port B is 8-bit, port C is split into 2 4-bit ports.
You can define if they operate in output mode (e.g. any data you write with out which goes to the 8255, is then reflected on the associated port output), or if it works in input, that is data is supplied to the 8255 ports by other hardware, this can be read in real time. So when you do an IN, you are reading the data given to it's ports.

port B is operating in input mode. This means it reflects the current immediate state of the inputs to it, one of these is the vsync output from the crtc. When this changes from 0->1 this is the start of the vsync and then it goes from 1->0 when it ends.

You can try and write data but this is ignored, because it's operating in input mode. So you'll only ever see the input values, and writing makes no difference because the 8255 doesn't pass it on, and in addition even if it did, the hardware device would override it with it's input data.

If you want to change the vsync, which is not recommended, you have to change some values in the crtc.
Best is to not change it, and leave it as it is.
(if you change it then weird timings don't work with all monitors, or displays)

In terms of execution speed, you can only alter your code to take less z80 instructions or time, something you've already done, or try and put in a faster cpu ;)  :laugh:
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

ervin

Thanks once again for all your help.
All that information is very useful, and I now understand it!

Powered by SMFPacks Menu Editor Mod