CHDK Forum

Using CHDK => CHDK Releases => Topic started by: Caefix on 27 / November / 2019, 12:58:58

Title: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 27 / November / 2019, 12:58:58
 :-X :o :o :-X Troubleshouting is an old bavarian tradition. We call it "Jodeln".

Here I have a simple Lua ask for value[addr]:
Code: [Select]
--[[--
@title peekdemo
@chdk_version 1.4
@param    p peek Address /4*4
 @range   p 4 999996
 @default p 1000
@subtitle :...,....1....,....2....,....3....,....4..
@param    u Logging
 @default u 1
 @range   u 0 1
@param    l Log:
 @default l -23
 --]]--
 print_screen(l)
p=p/4*4 -- avoid nil loop
print(string.format("%08x at 0x%08x",peek(p,4),p ))
print(string.format("%04x:%04x %08x ",peek(p-2,2),peek(p,2),peek(p,4)))
print(string.format("%02x %02x:%02x %02x at %d",peek(p-3,1),peek(p-2,1),peek(p-1,1),peek(p,1),p))

 print_screen(0)
wait_click(0)
The answer I expect is repeating only 4 char values, but only the peek(addr,1) line fits.
9002a011 at 0x00000490
7684:a011 9002a011
95 84:76 11 at 1168

that´s my current private ubasic factor peek, returning int´s 4 chars in expected order.
Code: [Select]
  } elseif x=6 exec {
?"Line1---------",x,bug:=% p,=%peek p,4.
? =%peek p-2,2;":";=%peek p,2,=%peek p,4
? =%peek p-3,1,=%peek p-2,1;":";=%peek p-1,1,=%peek p,1
  break

0x0490 0x95847611
0x9584:0x7611 0x95847611
0x95 0x84:0x76 0x11

edit: upper big endian values outdated, peek polarity switched below. ::)
Code: [Select]
  case TOKENIZER_PEEK:
    accept(TOKENIZER_PEEK);
long x,a=expr(); char s=4;
if (tokenizer_token()==TOKENIZER_COMMA) {
accept(TOKENIZER_COMMA); s=expr()%5; }
    if (a & (s-1)) {ended=1; break;}
    if (s==4) r=(unsigned int) *(unsigned int *)(a); else
    if (s==2) r=(unsigned short) *(unsigned short *)(a); else
    if (s==1) r=(unsigned char) *(unsigned char *)(a);
  break;

/*
memset(&r, (char)(*(char *)a),1);
    if (s>1) { x=0;
memset(&x, (char)(*(char *)(a+1)),1); // for big-endian (a-1) ...
x<<=8; r+=x;
    if (s>3) { x=0;
memset(&x, (char)(*(char *)(a+2)),1);
x<<=16; r+=x; x=0;
memset(&x, (char)(*(char *)(a+3)),1);
x<<=24; r+=x; }
}
break; */
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: reyalp on 27 / November / 2019, 13:38:53
Code: [Select]
print(string.format("%04x:%04x %08x ",peek(p-2,2),peek(p,2),peek(p,4)))
p-2 is a different address. If you want to look at the same word as the first version, you need to use peek(p,2) and peek(p+2,2), order depending on your endian preferences.
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 27 / November / 2019, 14:49:50
 ??? I expected that line printing 'left short' : 'right short' 'same int'.

edit: int following 95 86:76 11 > e0 02 90 da (p+1,p+2..)
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: reyalp on 27 / November / 2019, 16:20:53
??? I expected that line printing 'left short' : 'right short' 'same int'.

edit: int following 95 86:76 11 > e0 02 90 da (p+1,p+2..)
I don't understand.

If p is 100 in your example, then
Code: [Select]
print(string.format("%04x:%04x %08x ",peek(p-2,2),peek(p,2),peek(p,4)))
prints the halfword starting at 98, the halfword starting at 100, and the word starting at 100
The halfword at 98 is a totally different value, not part of the word at 100.
The halfword at 100 is lower two bytes of the word at 100, which on a little endian system like this will be the least significant values, appearing on the right when you print the whole word.
Example (chdkptp, because I'm lazy)
Code: [Select]
con 1> =p=0xff810000 return string.format('0x%08x',peek(p))
2:return:'0xea000001'
con 3> =p=0xff810000 return string.format('0x%04x0x%04x',peek(p,2),peek(p+2,2))
4:return:'0x0001 0xea00'
con 4> =p=0xff810000 return string.format('0x%02x 0x%02x 0x%02x 0x%02x',peek(p,1),peek(p+1,1),peek(p+2,1),peek(p+3,1))
5:return:'0x01 0x00 0x00 0xea'
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 28 / November / 2019, 11:09:29
We are talking about int value | eulav tni ( ( (  :lol
90.02:a0.11 at 0x00000490
7684:a011 9002a011
95 84:76 11 at 1168
 95 86:76 < 11 > e0 02 90 (da) | 90.02:a0.11

peeking for char at 0x00000490 =11 is the axis.
I expect there the lowest char of "my" int, Lua-peek takes it as the first char of a string.
"My" int/short is built reading backwards from 11, the other is reading forward and returns that mirrored.
(btw. it´s a 'funny' address in Sx710, something lives there.)
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: reyalp on 28 / November / 2019, 16:58:16
I still don't understand what you are trying to say, but I'm confident Lua peek works correctly.  It literally just does return *(type *)(address)

That returns the byte/halfword/word starting at the given address in the cameras native little endian (https://en.wikipedia.org/wiki/Endianness) byte order. Nothing is "mirrored", that's just how it's stored in memory.
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 30 / November / 2019, 11:10:04
Thx @vergilius for that journey, gimmy a ":P".

Now I know  :), that little endian is not a font, and I will not distort the native bytes.

Peek is done.
My con-fusion is gone!  :D


Oops, :blink:?? that´s  conf.using....



 :haha Where  :-X is it gone? :-X?                                                                                                                                          (  (  ( ;)


 :-X :o :o :-X  Beware of not-endian confusion outside!!
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 23 / April / 2022, 16:22:03
Code: [Select]

function printf(...)
  local str=string.format(...)
  print(str)
return  str
end

printf("0x%08x = %d",0xFE020004,0xFE020004)
Quote
=== CHDK Lua local ====================
 Run Script: printf.lua
=======================================

Process started >>>
---------------------------------------
conf = D:\hostluaPortable\bin\local_conf.lua
=== START =============================
0xfe020004 = 4261543940
---------------------------------------
<<< Process finished. (Exit code 0x0)

================ READY ================

I thought to have an equivalent in  :-[
Code: [Select]
Ubasic.c:
if (x>0xFFFF) {sprintf(string, "0x%08x",(unsigned)x);

printf.bas:

  ?"'";mem$(0xFE020004,q);"'":0xFE020004,,=%0xFE020004

Should be 'gaonisoy' on Sx270, but it outputs:
Quote
Хоооооооооооооооооооооооооооооооооооооооо
2147483647, 0x7fffffff
Code: [Select]
case TOKENIZER_AND:
             sprintf(string, "?=&:0x%08x=>%s",0xFE020004,
             memstr((void*)0xFE020004,40)); break;
Quote
?=&:0xfe020004=>gaonisoyЯш„Р
... as expected, but how to grep from expr() ?  :-[
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 26 / April / 2022, 16:06:36
...  :blink: atoi(ptr) has limited range, max 0x7fffffff.  :o

Now I call after parsing "0x" hexval=
Code: [Select]
static int hex2u()
{ int c=0;
  register unsigned u=0;
  while ((*nextptr>'/' && *nextptr<':')
        || strpbrk(nextptr,"abcdefABCDEF")==nextptr) {
    c++; if (c==9) return 0x80000000;
    u<<=4;
    if (*nextptr<64) u|=*nextptr-48;
    else u|=(*nextptr|32)-87;
    nextptr++;
  }
  return u;
}
long  tokenizer_value(void)  {return hexval ? hexval:decval} // !atoi(ptr);
// could ((avoid) atoi(ptr)) with : :haha
Code: [Select]
    decval=0;
    for (i = 0; i <= (MAX_NUMLEN); ++i) {
      if(isdigit(ptr[i]))
        {t=decval<<3; decval<<=1; decval+=t+ptr[i]-48;}
      else { break; }
    }

So in hex_dump.log appears:
Quote
gaonisoyßø„Ð ð(ø JhAð`HI K™B<¿Pø+Aø+

and console prints
?bug,mem$(0xFE020004,q):0xfe020004,,=%0xFE020004:=&:=(0xFE020004).
61 gaonisoy??„?
-33423356, 0xfe020004
&:=(0xFE020004).

&& my confusion is gaon...  :D
Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: Caefix on 28 / April / 2022, 12:12:02
Summary ...  :haha  :xmas
Quote

static int str2u(const char *s)
{
  int t, c=0, com=0, neg=0;
  register unsigned u=0;
  while (*s && *s<'!') s++;
  if (!*s)  return 0;
  if (*s=='+')  {s++;}
  if (*s=='~')  {s++; com=1;}
  if (*s=='-')  {s++; neg=1;}
  while (*s && *s<'!') s++;
  if (*s=='0' && (s[1]|32)=='x') { s+=2;
     while (( *s>'/'    &&  *s<':')
        || (((*s|32)>96 && (*s|32)<'g'))) {
        if (c==9) break;
        u<<=4;
        u |= (*s<64) ? (*s-48):(*s|32)-87;
        c++; s++;}
 }else
     while (*s>'/' && *s<':') {
        if (c==11) break;
        t=u<<3; u<<=1;
        u += t+*s-48;
        c++; s++;}
  if (neg) u=-u;
  if (com) u=~u;
  return u;
}

Title: Re: peek (was Re: CHDK 1.4 release + bug reports)
Post by: reyalp on 28 / April / 2022, 17:12:09
...  :blink: atoi(ptr) has limited range, max 0x7fffffff.  :o
I don't really understand these posts, but FWIW atoi (https://pubs.opengroup.org/onlinepubs/009695399/functions/atoi.html) = ASCII to integer. You might prefer strtoul (https://pubs.opengroup.org/onlinepubs/009695399/functions/strtoul.html) for unsigned values.