Skip to content
Logos-Coder edited this page May 9, 2019 · 7 revisions

peek

peek addr -> val (byte)

Parameters:

  • addr : any address of the 80k RAM byte you want to read

Output:

  • val : the value read from the addr parameter. Each address stores a byte, so the value will be an integer from 0 to 255.

Description:

This function allow to read the memory from TIC.
It's useful to access resources created with the integrated tools like sprite, maps, sounds, cartridges data? Never dream to sound a sprite?
Address are in hexadecimal format but values are decimal.

To write to a memory address, use poke.

Example peek/poke

Example

-- lua demo peek/poke

-- use poke to create a sprite from a hexadecimal data string
img="0a0000a00a0ff0a0aaaaaaaaaaafaaa6faffaaaaaaaf6aaaaaaaaaaa33333333"
-- foreach of the 32 bytes in the sprite
for i=0,31 do
 --extract the data from the string 2 chars at time
 s = string.sub(img,i*2+1,i*2+2)
 --convert the hex string to a decimal number
 val = tonumber(s,16)
 --put the value in the RAM area dedicated to sprites, sprite number 4
 poke(0x4000+128+i,val)
end

-- read back the sprite data and print it to console
msg=''
-- foreach of the 32 bytes in the sprite
for i=0,31 do
 --read the byte value in decimal format
 val=peek(0x4000+128+i)
 -- convert the value in hexadecimal format
 s=string.format("%02x",val)
 -- concatenate the string to a final message
 msg=msg..s
end
-- print it to the console
trace(msg)
// add sprite to TIC cartridge
sync(0,0,true)

function TIC()
 cls()
 print('Done.. have a look at the sprite editor.',0,10) 
 print('And to the console also.',0,20)
end

This is the same as above written in Javascript. Make sure your script cartridge metadata is set to // script: js

// javascript demo peek/poke

// use poke to create a sprite from a hexadecimal data string
var img="0a0000a00a0ff0a0aaaaaaaaaaafaaa6faffaaaaaaaf6aaaaaaaaaaa33333333"
// loop over one byte at a time
// remember that 2 hex digits is 1 byte
for(var i=0; i < 64; i=i+2) {
 //extract byte and convert into a hex string
 val=parseInt(img.substring(i,i+2), 16)
 //put the value into RAM at sprite number 4
 poke(0x4080+(i/2),val)
}

// read back the sprite data and print it to console
// this is a regular expression to find a single hex digit
var regHex=/^[0-9a-fA-F]{1}$/
var msg=''
// loop over sprite #4, one byte at a time
for (var i=0; i < 64; i=i+2) {
 //extract byte and convert it to a hexadecimal
 val=(peek(0x4080+(i/2))).toString(16)
 /*pad the string with the regular expression
   if needed, and concatenate it to the message */
 msg=msg+val.replace(regHex,'0'+val)
}

// print it to the console
trace(msg)
// add sprite to TIC cartridge
sync(0,0,true)

function TIC(){
 cls()
 print('Done.. have a look at the sprite editor.',0,10) 
 print('And to the console also.',0,20)
}
You can’t perform that action at this time.