News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_cperezgrin

[cpctelera] read memory location & project KB?

Started by cperezgrin, 19:55, 31 August 18

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

cperezgrin

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 (CPCT_VMEM_START, 0, 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?

Arnaud

#1
Hello @cperezgrin ,

read data at specific address is easy :

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 :

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



cperezgrin


ronaldo

#3
Quote from: cperezgrin on 19: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 (CPCT_VMEM_START, 0, 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 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:

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 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:

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 :)

cperezgrin

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.

ronaldo

As you are spanish, you may want to have a look at videos on my Youtube Channel. Specially, I have lessons on C programming using CPCtelera, and also assembly programming. You may want to start with C programming lessons and later on progress to assembly, having a look at the Mastering Z80 Assembler course.

Hope it helps :) .

cperezgrin

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

GUNHED

Any hint for an English / German C manual?  :)
http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

arnoldemu

#8
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.


A = PEEK(&4000)

Change it to this:

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:

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.
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:

0x04000 and 0x04001 -> x 0x04002 and 0x04003 -> y 0x04004 -> flags
EDIT: formatting
My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

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
  • is the element in the list you want to access, the elements are numbered from 0 and increase.
    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.




My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

arnoldemu

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.

My games. My Games
My website with coding examples: Unofficial Amstrad WWW Resource

ervin

Thanks Kev.
That's really great information.

cperezgrin


SRS


GUNHED

http://futureos.de --> Get the revolutionary FutureOS (Update: 2023.11.30)
http://futureos.cpc-live.com/files/LambdaSpeak_RSX_by_TFM.zip --> Get the RSX-ROM for LambdaSpeak :-) (Updated: 2021.12.26)

Powered by SMFPacks Menu Editor Mod