How To

Peek and Poke

0

PranXter 2020-03-20 06:22

I understand what peek and poke do, I’m just not sure how to use them. Could someone show me how to plot a single pixel using peek, poke, or similar commands?


was8bit 2020-03-20 09:41

All graphics is via character cells, so the trick is to learn first how to use PEEK POKE to edit a single character on the Char Designer page... then lay out a blank array of character and "draw" to those characters...

https://lowresnx.inutilis.com/topic.php?id=854

But really just focus on this bit of code...

SUB PLOT(X,Y,C,CC)
A=$8000+CC*16
IF C=0 THEN
POKE A+Y,PEEK(A+Y) AND SET0(X)
POKE A+Y+8,PEEK(A+Y+8) AND SET0(X)
END IF
IF C=1 THEN
POKE A+Y,PEEK(A+Y) OR SET1(X)
POKE A+Y+8,PEEK(A+Y+8) AND SET0(X)
END IF
IF C=2 THEN
POKE A+Y,PEEK(A+Y) AND SET0(X)
POKE A+Y+8,PEEK(A+Y+8) OR SET1(X)
END IF
IF C=3 THEN
POKE A+Y,PEEK(A+Y) OR SET1(X)
POKE A+Y+8,PEEK(A+Y+8) OR SET1(X)
END IF
END SUB

Where x,y is 0 to 7
C is a color 0 to 3
cc is the character 0 to 255

This does NOT edit the character FILE, just the game's active character memory in real time... these changes are not directly permament....



was8bit 2020-03-20 09:44 (Edited)

If you look in the HELP file...

Hardware Reference

Memory Map

$0000 - Cartridge ROM (32 KB)

$8000 - Character Data (4 KB)
$9000 - BG0 Data (2 KB)
$9800 - BG1 Data (2 KB)
$A000 - Working RAM (16 KB)
$E000 - Persistent RAM (4 KB)
$FE00 - Sprite Registers (256 B)
$FF00 - Color Registers (32 B)
$FF20 - Video Registers
$FF40 - Audio Registers
$FF70 - I/O Registers

$8000 is where the game's active character file starts...

... also, each character is 16 big, so the actual memory address of character number (CC) is A=$8000+16*CC


was8bit 2020-03-20 09:46

To make the binary math easier for me, i created a data array to help me figure out the binary math....

FOR I=0 TO 7
READ SET0(I)
READ SET1(I)
NEXT I
DATA %01111111
DATA %10000000
DATA %10111111
DATA %01000000
DATA %11011111
DATA %00100000
DATA %11101111
DATA %00010000
DATA %11110111
DATA %00001000
DATA %11111011
DATA %00000100
DATA %11111101
DATA %00000010
DATA %11111110
DATA %00000001


was8bit 2020-03-20 09:48 (Edited)

I really cannot explain the binary math.. Timo and others have helped alot, but I still struggle with it.. my brain operates in Base10, so other Bases are abit confusing...


was8bit 2020-03-20 09:53

I know each character has 2 "mappings", high and low... if you only want to use the the two colors 0,1 then its easy as you only use i think the low map, but to use all 0,1,2,3 colors NX uses both maps to determine what color each point is...


was8bit 2020-03-20 10:04

Check this out ;)

https://lowresnx.inutilis.com/topic.php?id=1083


Timo 2020-03-20 10:38

The important thing to know is that there is no pixel buffer for the whole screen, only for characters. You can fill the screen/BG with unique characters to draw on them, but there are only 256 characters at once, not enough to fill all 20x16 cells on screen (only 16x16). Then again there are tricks using raster interrupts (ON RASTER CALL), but it's getting complicated.

LowRes NX just works like typical 8- and 16-bit game consoles, they worked like this to save memory. It's all made for tile based backgrounds.

It was a design decision that NX has the same advantages and disadvantages. I recommend using NX with tiles. Anything else can be a fun technical challenge, but it's not easy to use.


was8bit 2020-03-20 10:42 (Edited)

My program here uses only character #0 and maybe character#1

For real "drawing" with no extra tricks...

A 16x16 uses one layer and all 255 characters (3 color drawing)
(You have to turn off the unused layer)

A 10x10 uses both layers and 200 characters (6 color drawing)


PranXter 2020-03-21 00:13

Could I just have the code that would plot 1 pixel at 1,1? I learn best from simple (and I mean simple) barebones code samples I can tinker with.


was8bit 2020-03-21 06:13

Check this out....

https://lowresnx.inutilis.com/topic.php?id=1084

Remember, this requires extensive use of BIT math, not regular math.. and 3 colors is extensively more complicated than just 1color...


was8bit 2020-03-21 06:16 (Edited)

Oh, to answer your question, this plots a single color 1 dot at 1,1

POKE $8000+1,2^(7-1)

It also plots color 0 along the remaining horizontal y=1 line...


was8bit 2020-03-21 06:18 (Edited)

To plot a single color 1 dot at 1,1 without altering any existing pixels requires reading the existing pixels and performing the correct bit math to add a pixel at 1,1 to the existing pixels


was8bit 2020-03-21 06:21

Technically, each value you POKE is a specific pattern made up of 3 colors... you have to decode the bitmap patterns in order to recode new patterns that emulate adding a pixel...


was8bit 2020-03-21 06:48

Here is another demo...

https://lowresnx.inutilis.com/topic.php?id=1085


PranXter 2020-03-21 18:05

Thanks, this helped a lot!


was8bit 2020-03-22 03:16

Glad i could help :)


Log in to reply.