CPCWiki forum

General Category => Programming => Topic started by: cperezgrin on 21:55, 31 August 18

Title: [cpctelera] read memory location & project KB?
Post by: cperezgrin on 21:55, 31 August 18
Hello everybody, I'm new in cpctelera so please be patient with my questions ;D
I'm pretty sure this will be simple and easy but can't find a way to read a memory location in cpctelera. I want to read the byte in xC000 (for example), which is the syntax to read a memory location inside a c file?
u8 value = <function>(cpct_getScreenPtr (http://lronaldo.github.io/cpctelera/files/video/cpct_getScreenPtr-asm.html#cpct_getScreenPtr) (CPCT_VMEM_START (http://lronaldo.github.io/cpctelera/files/video/video_macros-h.html#CPCT_VMEM_START), 0 (http://lronaldo.github.io/cpctelera/files/types-h.html#u8), 0));
Also, how can I know the total size of my resulting project? I don't want to exceed 64KB.Is the file size of the .cdt generated a good reference?
Title: Re: [cpctelera] read memory location & project KB?
Post by: Arnaud on 22:34, 31 August 18
Hello @cperezgrin (http://www.cpcwiki.eu/forum/index.php?action=profile;u=2791) ,

read data at specific address is easy :

Code: [Select]
u8* memLoc = (u8*)0xC000;
u8 value = *memLoc;
u8 nextByte = *memLoc++;

And to know the size of the project you have to read at the end of the build these informations :

Code: [Select]
Binary file start = 00000040
Records start     = 00000040
Highest address   = 00008A2B
The Highest address is what you want to know, with firmware disabled you can go near to 0xBF00.

Arnaud


Title: Re: [cpctelera] read memory location & project KB?
Post by: cperezgrin on 22:40, 31 August 18
Great. Thanks!
Title: Re: [cpctelera] read memory location & project KB?
Post by: ronaldo on 11:14, 02 September 18
Hello everybody, I'm new in cpctelera so please be patient with my questions ;D
I'm pretty sure this will be simple and easy but can't find a way to read a memory location in cpctelera. I want to read the byte in xC000 (for example), which is the syntax to read a memory location inside a c file?
u8 value = <function>(cpct_getScreenPtr (http://lronaldo.github.io/cpctelera/files/video/cpct_getScreenPtr-asm.html#cpct_getScreenPtr) (CPCT_VMEM_START (http://lronaldo.github.io/cpctelera/files/video/video_macros-h.html#CPCT_VMEM_START), 0 (http://lronaldo.github.io/cpctelera/files/types-h.html#u8), 0));
Also, how can I know the total size of my resulting project? I don't want to exceed 64KB.Is the file size of the .cdt generated a good reference?
Well, technically, your doubt is not about CPCtelera. Your doubt is about C programming language. You may probably want to refresh some C programming concepts not to have problems when dealing with low-level programming such as directly reading and writting memory bytes. In this case, you need to clearly understand pointers and memory access. Otherwise, the technical misconceptions that lay behind this doubt will probably drive you make mistakes that you won't understand and can frustrate you. Solid low-level understanding of pointers is your first weapon against big problems.

@Arnaud (http://www.cpcwiki.eu/forum/index.php?action=profile;u=1424) has already given you a possible solution. You may do it in many ways. The key is: in C you access memory through pointers. A pointer is a variable that holds a memory address. You use then the indirection operator (Also called dereference or 'Value at address' operator) '*' to get the value that lies at the address pointed by your pointer. So you may do this:
Code: [Select]
u8 value = *( (u8*)0xC000 );
0xC000 is just a value, and cannot be directly used to access a memory location (we need a pointer). So we cast it to a u8* (pointer to unsigned char, so it points to a byte). The result is a pointer, and so we can use the indirection operator on it '*' to access the contents of the memory it points to. Finally, that value is assigned to the variable 'value'.

@Arnaud (http://www.cpcwiki.eu/forum/index.php?action=profile;u=1424) did same operation, but divided into several steps.


If you plan to access several memory locations, you may want to use the pointer as if it was an array, using square brackets operator, like in here:
Code: [Select]
u8* memptr = (u8*)0xC000;  // memptr points to 0xC000
u8 v1 = *(memptr + 9);  // We get the value at location 0xC009
u8 v2 = memptr[9];    // Same as before, we get value at 0xC009, but using square brackets operator
u8 v3 = memptr[0];   // This gives as value at 0xC000, same as *memptr, same as *(memptr + 0)

As I said, this has nothing to do with CPCtelera. It is standard C and works either on SDCC for Z80, or on GCC/CLang for your PC.

Hop this helps :)
Title: Re: [cpctelera] read memory location & project KB?
Post by: cperezgrin on 21:34, 10 September 18
Thanks for the explanation.
Any recomendation for refreshing C? some website or public pdf to re-learn the concepts? I still know the basics, but everything regarding with pointers is my weakness.
Title: Re: [cpctelera] read memory location & project KB?
Post by: ronaldo on 11:57, 11 September 18
As you are spanish, you may want to have a look at videos on my Youtube Channel (https://www.youtube.com/ProfesorRetroman). Specially, I have lessons on C programming using CPCtelera (https://www.youtube.com/watch?v=zov-yI7I00s&index=1&list=PLmxqg54iaXriIiLmRbeOiwE_NWKM-i8DB), and also assembly programming (https://www.youtube.com/watch?v=13JGNTWcNLA&list=PLmxqg54iaXrjA0wzZmc_HDeZxs-vDwDFS). You may want to start with C programming lessons and later on progress to assembly, having a look at the Mastering Z80 Assembler (http://www.cpcwiki.eu/index.php/DEZ80) course.

Hope it helps :) .
Title: Re: [cpctelera] read memory location & project KB?
Post by: cperezgrin on 16:19, 11 September 18
I'm already subscribed to your channel and have seen your "masterclass" in Retropolis and Retropixel (even Introducing CPCtelera with Juanje), but everytime I deal with pointers is a nightmare to me. The videos from Mastering Z80 are also very helpful, because now I can start some programming with CPC when I never touch one before.
I will take a look to the assembly programming though I don't want to get low level by now. That's why I like cpctelera, it's very friendly.
Thanks
Title: Re: [cpctelera] read memory location & project KB?
Post by: GUNHED on 21:27, 11 September 18
Any hint for an English / German C manual?  :)
Title: Re: [cpctelera] read memory location & project KB?
Post by: arnoldemu on 21:35, 11 September 18
Think of the computer memory as a horizontal line of boxes. Each box is 1 byte. All the boxes is the RAM.A pointer is a variable which points to one of the boxes.
So a pointer describes the location of some data.
This allows you to pass location information to code. e.g. the location of sprite data to draw.

In BASIC you would use this to read memory location &4000.

Code: [Select]
A = PEEK(&4000)Change it to this:
Code: [Select]
P = &4000A = PEEK(P)P is a variable holding the address you want to peek. In C this variable type is called a pointer. ;)P variable defines the memory address which is the box to read the data from. A is the data inside the box.
So here you are using a pointer to read data.

In C we would write it like this:
Code: [Select]
char *p = (char *)0x04000;'char' is the type of the data you want to work on.'*' means the variable is a pointer. You can put the '*' next to char 'char*' or next to the variable name 'char *', or you can put it on it's own "char * ". It's not really important. Some people write it one way, other people write it other ways. They have reasons why they do this and they make sense but I'll not go into them here.
So this is saying "p" is a variable, it's a pointer, it has the value 0x04000 and it points to a char.

In C a char is 8-bits of data also called a 'byte'.
Sometimes you will see "char" and "unsigned char".
a char is a signed value, e.g. -127 to +128a unsigned char is an unsigned value e.g. 0-255.
it doesn't really matter when it comes to cpctelera. So you may see both used.
In C it becomes more powerful. You can use a pointer to point to a a byte, a word/'short' (16-bit or 2 bytes), or to memory which is in a form you define ( a struct).
e.g.
Code: [Select]
typedef struct {   short x; /* this is 16-bit data, or 2 bytes */   short y; /* this is 16-bit data, or 2 bytes */  char flags; /* this is 8-bit data, or 1 byte */} sprite_data;
sprite_data * p = (sprite_data *)0x04000;
this is saying, "p" is a variable which is a pointer to data in memory which has the form "sprite_data".

The value is initialised to 0x04000, but because 0x04000 has a different type to "sprite_data" C will not be happy unless you "cast" or convert it to the type it wants.
The (sprite_data *) is telling C that you really want to make it think of it as a pointer to a sprite_data type.

So for sprite_data, it will be stored like this:
Code: [Select]
0x04000 and 0x04001 -> x 0x04002 and 0x04003 -> y 0x04004 -> flagsEDIT: formatting
Title: Re: [cpctelera] read memory location & project KB?
Post by: arnoldemu on 21:40, 11 September 18
so using pointers:
sprite_data *p=(sprite_data *)0x04000;
Now you have the pointer you can access the data in many ways:
p->x = 20;
This means using "p" access the "x" in the struct and set it's value.
Then if you had a list of these you can go to the next with this:
p++;
which is the same as:
p=p+1;
or the previous with:
p--;
or

p=p-1;
you can also treat it like an array in basic:
p[0].x=30;
the
So 0,1,2,3, etc
It is worth noting that when you do p++ this means "increase the pointer by the size of the type it is pointing to".
So for char, p++ increments by 1, for sprite_data it's moving 2+2+1 which is the size of the struct..
This may be part of your confusion.




Title: Re: [cpctelera] read memory location & project KB?
Post by: arnoldemu on 21:42, 11 September 18
also you can tell C to help you.
You can tell it that the pointer is looking at data that should not be changed. it is "read only" or "constant" or in C "const".
You write this:
const char *p=(const char *)0x04000;
This means "p" is a variable which locates data of type "char" which is "read only".
if you then try to write to the data using this pointer it will tell you off.

Title: Re: [cpctelera] read memory location & project KB?
Post by: ervin on 02:51, 12 September 18
Thanks Kev.
That's really great information.
Title: Re: [cpctelera] read memory location & project KB?
Post by: cperezgrin on 19:38, 12 September 18
This really helps. Thanks!
Title: Re: [cpctelera] read memory location & project KB?
Post by: SRS on 22:07, 12 September 18
Any hint for an English / German C manual?  :)
ftp://ftp.oeaw.ac.at/pc/e-books/linux/clinux.pdf (ftp://ftp.oeaw.ac.at/pc/e-books/linux/clinux.pdf) ?
http://dblabor.fh-stralsund.de/skripte/Skript%20Horst-Meyer.pdf ?
Title: Re: [cpctelera] read memory location & project KB?
Post by: GUNHED on 22:10, 12 September 18
Danke, Super!  :)