Skip to content
Josh Goebel edited this page Oct 7, 2021 · 14 revisions

poke4

poke4 addr4 val

Parameters

  • addr4 : the nibble (4-bit) address of RAM you desire to write (0 to 0x30000)
  • val : the 4-bit integer value to write to RAM (0-15)

Description

This function allows you to write directly to RAM. The address is often specified in hexadecimal format.

For both peek4 and poke4 RAM is addressed in 4 bit segments (nibbles). Therefore, to access the the RAM at byte address 0x4000 you would need to access both the 0x8000 and 0x8001 nibble addresses.

Note: Memory is sequenced with least significant nibbles first, which may be the opposite of what you expecting if you are reading the hexadecimal byte by byte. A small example:

-- set byte address 0x4000 to the value 0xF1 (241)
poke(0x4000, 0xF1)

-- the low nibble (least significant bits)
peek4(0x8000) -- => 1  (0x01)
-- the high nibble (most significant bits)
peek4(0x8001) -- => 15 (0x0F)

Example

-- title:   poke4/peek4 example
-- author:  PaulR
-- script:  Lua

-- This example reads and writes into
-- sprite memory so we can see the
-- effect directly

-- Sprites are stored from address
-- 0x04000 onwards. Memory can be
-- addressed in 8 or 4 bit form so
-- we can access sprite memory as follows:

-- byte address  |  nibble address
--  0x04000      |  0x08000 (low nibble)
--               |  0x08001 (high nibble)
--  0x04001      |  0x08002 (low nibble)
--               |  0x08003 (high nibble)


-- write color 5 to first pixel of sprite 0
poke4(0x08000,5)
-- write color 10 to next pixel of sprite 0
poke4(0x08001,10)

function TIC()
	cls(0)

	-- display sprite 0
	spr(0,0,0)
        -- the two pixels set by our poke4
        -- commands are visible

        -- display the 8 bit value stored at 0x04000
	print(peek(0x04000),30,30,4)
	-- prints '165'
	-- ie value of 10 shifted left by 4
	-- into the high nibble (10<<4=160)
	-- plus 5 in the low nibble

	-- show our color values using
	-- 4 bit addressing
	print(peek4(0x08000),30,40,4)
	print(peek4(0x08001),30,50,4)
end

See also: Spritesheet editing example in the examples page

Clone this wiki locally