Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

Accessing individual bits on a Commodore PET

2011-03-26

On a Commodore PET, each byte that can be read using the PEEK function, and set using the POKE statement. A byte consist of 8 bits, and represent a number between 0 and 255. This code writes the value 5 to the memory address 1020, and then reads it back. The output is of course 5.

POKE 1024, 5
PRINT PEEK(1024)

The POKE statement affects all eight bits at the given address (1024), but bits can be filtered out using logical operators AND and OR. The first (from right to left) of the eight bits represents the value 1, the second bit represents 2, the third 4, the fourth 8, the fifth 16, the sixth 32, the seventh 64 and the last bit represents the value 128.

Since the value 5 is stored at 1024, you would expect all the bits being 0 except the first and the third, since 1 + 4 equals 5. This code reads the first four bits at 1024, and the output is 1, 0, 4 and 0. 1 because the first bit (representing 1) is set, 0 because the second isn’t set, 4 because the third (representing 4) is set and 0 because the fourth isn’t set.

PRINT PEEK(1024) AND 1 : REM Read bit 1 - (1 if set)
PRINT PEEK(1024) AND 2 : REM Read bit 2 - (2 if set)
PRINT PEEK(1024) AND 4 : REM Read bit 3 - (4 if set)
PRINT PEEK(1024) AND 8 : REM Read bit 4 - (8 if set)

Multiple bits can also be read. This code reads both the first and the third bit on the address 1024:

PRINT PEEK(1024) AND 5

The result will be 0 if neither is set, 1 if the first is set, 4 if the second is set and 5 if both the first and third is set. The bit pattern of the value is 00000101, so the bits that are pointed out are the first and the third (from right to left).

Also, you can set individual bits. OR [n] turns a bit on, where [n] is the value that the bit represents. 1, 2, 4, 8, 16, 32, 64 or 128. This code turns on the last bit at 1024:

POKE 1024, PEEK(1024) OR 128

The bit pattern at 1024 is now 10000101. The value that is stored at 1024 is 133, because 1 + 4 + 128 equals 133. To turn the last bit (128) back to zero, use AND together with bit pattern that contains a one at all positions that you want the existing flag to remain, and 0 at the positions you want to turn bytes off. To turn the last bit from the right off, you would want a bit pattern that looks like this: 01111111. All bytes remains, but the last bit from the right is 0. The number 127 has this pattern.

POKE 1024, PEEK(1024) OR 127

Now, the bit pattern at 1024 is changed back from 10000101 to 00000101. The value that is stored in the byte at 1024 is now 5 again.

What can be done with this knowledge?

Categories: Geeky

Tags: C128

One response to “Accessing individual bits on a Commodore PET”

  1. […] Commodore 64 does not provide any API for graphics, so on the C64 this task is accomplished by manipulating bits in a memory area. You can jump to graphics mode by setting the bit 6 on byte 53265 to 1 and return […]

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips