CPC Ram selection ARRAY

Started by Baggey, November 09, 2024, 14:58:39

Previous topic - Next topic

Baggey

 So, I have these two ARRAY's but they dont seem to be the same but should be :-[
I mean 2 Dimensional Array of [4,8] Int

SuperStrict

AppTitle = "Amstrad CPC Ram selector"

Graphics 800,600

Global RamPEEK:Int[4]
Global RamPOKE:Int[4]

Global RamSelect:Int

' Should be a 2 Dimensional Array of (4,8)
Global SidewaysRam1 : Int [] [] = [[$00000,$00000,$10000,$00000,$00000,$00000,$00000,$00000],
                                  [$4000,$4000,$14000,$C000,$10000,$14000,$18000,$1C000],
                                  [$8000,$08000,$18000,$08000,$08000,$08000,$08000,$08000],
                                  [$C000,$1C000,$1C000,$1C000,$0C000,$0C000,$0C000,$0C000]]

' Should be also a 2 Dimensional Array of (4,8)
Global SideWaysRam2:Int[4,8]
         SideWaysRam2[0,0]=$00000 ; SideWaysRam2[0,1]=$00000 ; SideWaysRam2[0,2]=$10000 ; SideWaysRam2[0,3]=$00000 ; SideWaysRam2[0,4]=$00000 ; SideWaysRam2[0,5]=$00000 ; SideWaysRam2[0,6]=$00000 ; SideWaysRam2[0,7]=$00000
         SideWaysRam2[1,0]=$04000 ; SideWaysRam2[1,1]=$04000 ; SideWaysRam2[1,2]=$14000 ; SideWaysRam2[1,3]=$0C000 ; SideWaysRam2[1,4]=$10000 ; SideWaysRam2[1,5]=$14000 ; SideWaysRam2[1,6]=$18000 ; SideWaysRam2[1,7]=$1C000
         SideWaysRam2[2,0]=$08000 ; SideWaysRam2[2,1]=$08000 ; SideWaysRam2[2,2]=$18000 ; SideWaysRam2[2,3]=$08000 ; SideWaysRam2[2,4]=$08000 ; SideWaysRam2[2,5]=$08000 ; SideWaysRam2[2,6]=$08000 ; SideWaysRam2[2,7]=$08000
         SideWaysRam2[3,0]=$0C000 ; SideWaysRam2[3,1]=$1C000 ; SideWaysRam2[3,2]=$1C000 ; SideWaysRam2[3,3]=$1C000 ; SideWaysRam2[3,4]=$0C000 ; SideWaysRam2[3,5]=$0C000 ; SideWaysRam2[3,6]=$0C000 ; SideWaysRam2[3,7]=$0C000


Function SetCPC_RAM()

    RamPOKE[0] = SideWaysRam2[0,1] 'RamSelect]
   
End Function

Now when i call the Array1 type im getting error's
SuperStrict

AppTitle = "Amstrad CPC Ram selector"

Graphics 800,600

Global RamPEEK:Int[4]
Global RamPOKE:Int[4]

Global RamSelect:Int

' Should be a 2 Dimensional Array of (4,8)
Global SidewaysRam1 : Int [] [] = [[$00000,$00000,$10000,$00000,$00000,$00000,$00000,$00000],
                                  [$4000,$4000,$14000,$C000,$10000,$14000,$18000,$1C000],
                                  [$8000,$08000,$18000,$08000,$08000,$08000,$08000,$08000],
                                  [$C000,$1C000,$1C000,$1C000,$0C000,$0C000,$0C000,$0C000]]

' Should be also a 2 Dimensional Array of (4,8)
Global SideWaysRam2:Int[4,8]
         SideWaysRam2[0,0]=$00000 ; SideWaysRam2[0,1]=$00000 ; SideWaysRam2[0,2]=$10000 ; SideWaysRam2[0,3]=$00000 ; SideWaysRam2[0,4]=$00000 ; SideWaysRam2[0,5]=$00000 ; SideWaysRam2[0,6]=$00000 ; SideWaysRam2[0,7]=$00000
         SideWaysRam2[1,0]=$04000 ; SideWaysRam2[1,1]=$04000 ; SideWaysRam2[1,2]=$14000 ; SideWaysRam2[1,3]=$0C000 ; SideWaysRam2[1,4]=$10000 ; SideWaysRam2[1,5]=$14000 ; SideWaysRam2[1,6]=$18000 ; SideWaysRam2[1,7]=$1C000
         SideWaysRam2[2,0]=$08000 ; SideWaysRam2[2,1]=$08000 ; SideWaysRam2[2,2]=$18000 ; SideWaysRam2[2,3]=$08000 ; SideWaysRam2[2,4]=$08000 ; SideWaysRam2[2,5]=$08000 ; SideWaysRam2[2,6]=$08000 ; SideWaysRam2[2,7]=$08000
         SideWaysRam2[3,0]=$0C000 ; SideWaysRam2[3,1]=$1C000 ; SideWaysRam2[3,2]=$1C000 ; SideWaysRam2[3,3]=$1C000 ; SideWaysRam2[3,4]=$0C000 ; SideWaysRam2[3,5]=$0C000 ; SideWaysRam2[3,6]=$0C000 ; SideWaysRam2[3,7]=$0C000


Function SetCPC_RAM()

    RamPOKE[0] = SideWaysRam1[0,1] 'RamSelect]
   
End Function

Im doing something silly but dont understand why the Difference? How do i access both types or have i found a BUG?

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Baggey

Okay i may have my own answer ???

QuoteMulti-Dimensional Auto Arrays
[b]Note[/b]: this is a slightly more advanced topic and as such, beginners may find it confusing.

It is not currently possible to create normal multi-dimensional arrays AND auto-initialize them. You can however cheat a little:



[pre]Local string_array:String[][] = [["Bob","Kate","Percy"],["Cecil","Edmund","Elizabeth"]]
Print string_array[/pre]


[2] 'this will print Percy
Print string_array[1]
 'this will print Cecil

What the above actually does is create an array of type String Array.
 If you were to attempt to refer to it in the conventional manner it
would cause an error (Incorrect number of array dimensions).
Local string_array:String[][] = [["Bob","Kate","Percy"],["Cecil","Edmund","Elizabeth"]]
Print string_array[0,0] 'this line would cause an error
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

col

#2
QuoteSo, I have these two ARRAY's but they dont seem to be the same but should be

Hiya, I think you have your terms a little confused there, which may be further confused by some compiler bugs.

The first ( SidewaysRam1:Int[][] ) is an array of arrays, the second ( SidewaysRam2:Int[,] )is a multi-dimension array. They are 2 completely different types where the elements are not accessed in the same way.
Array of Arrays:
Local array:String[][] = [["A", "B"],["C", "D"]]
Whose elements are accessed as:
Print array[0][1]
' should print B

Print array[1][1]
' should print D

QuoteLocal string_array:String[][] = [["Bob","Kate","Percy"],["Cecil","Edmund","Elizabeth"]]
Print string_array
You should get a compiler error with this, something like 'Cannot convert a string array array to a string'


Quote[2] 'this will print Percy
Print string_array[1]
'this will print Cecil
You should also get a compiler error here too, with something along the lines of 'Cannot convert a string array to a string'


QuoteLocal string_array:String[][] = [["Bob","Kate","Percy"],["Cecil","Edmund","Elizabeth"]]
Print string_array[0,0] 'this line would cause an error
This is correct as the array is not a multi-dimension array, but an array of arrays.
Using Print string_array[0][0] should print Bob


So it looks like there are a couple of compiler bugs there - no wonder you got confused.




https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Baggey

Hey @col thanks for your reply and your explanation for that,

The reason i ended up with Array of Arrays was the convenience of auto filling them.

Array [4,8]=[0,0,0,0],[1,1,1,1] and so on etc... Would of been the eaisiest way of auto filling?

With out accessing the Arrays and getting the Wrong addresses Play's Havoc, with Machine Code. You may be aware just one byte Address off, can Disassemble into a totally different piece of Code ::)

Im probably missing the point somewhere along the line, but why Have they designed it to be like this? It dosen't make Logical Sense. To me that is. :-[

Okay it's a matter of using the Syntax just how they want it. But surely an array of arrays is a Multidimensional Array, Anyways :-\

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

col

#4
QuoteBut surely an array of arrays is a Multidimensional Array

In languages such as c and c++ - (not the c++ std::array, but 'raw arrays'), and in assembly where you design these data structures yourself, the memory used for arrays can be contiguous in memory - one arrays data elements can immediately follow on from another arrays data elements in an array of arrays style or a multi-dimension style.
This can be done because arrays in these languages are just the element data only - for eg there's no information within the array itself about the number of elements in those arrays. This is why when passing a raw array into a function you would normally pass in the number of elements too, because otherwise that information is unknown.

BlitzMax arrays are completely different in that they are a container object that is made up of a pointer to the base object type (all BMax Types extend from a base object, appropriately called Object), the size of the array data in bytes, the dimensions of the array are calculated as scales from one dimension to the next (so that a simple multiplication is all that is needed to move through a multi-dimension array elements), this data is then followed by the actual array elements themselves. This data is all contiguous in memory for an array object.

So as you can see, Blitzmax arrays also contain more just the element data that c/c++ raw arrays are composed of, and they cannot really be put in to memory in a contiguous fashion due to that extra data that a BMax array object carries with it.

All objects in BMax are memory managed via a garbage collector meaning the memory is allocated from small heaps of memory within the garbage collector itself and returned as a pointer into said memory.

When you create an array of arrays your array data elements are actually pointers to the array objects, as opposed to just each array element data following on from each other in memory.

Local array:Int[][] = [[1,2,3,4],[5,6,7,8]]
Becomes
Local array:Int[][] = Pointer to array whose elements are [Pointer to array, Pointer to array]
The first array element contains a pointer to array container which will have the data of 1,2,3,4, and the 2nd element will also be a pointer to another array object container that contains the 5,6,7,8 data.
These array elements also don't have to be the same length. You could have
Local array:Int[][] = [[1,2,3], [4,5,6,7,9]]
giving you an array that has 2 elements, the first element is an array of 3 elements, and the second element is an array of 5 elements.

On the other hand... a multi-dimension array is made from the same array data structure described above but the elements are contiguous in memory as you are expecting.

This means that some thing such as Local array:Int[3,4], creates a single 'flat' array that has 12 elements, and you use the indices to access the data from within those 12 elements.
In a 2 dimensional array, the first index is multiplied by the number of elements in the second index, then the 2nd index is added in order to get to the correct element in memory.
For eg if you have the array of n:Int[3,4], and you want the element at 2,3 (using the syntax n[2,3]), then the data will be retrieved from the single contiguous memory using the caculation: 2 * 4 + 3, giving element 11. Accessing n[1,2] will retrieve the value from element 1 * 4 + 2, so element 6. The indices calculated are zero-indexed, so element 11 is the last element, and element 6 is somewhere around the middle.


Ultimately you can see that an array of arrays is completely different from a multi-dimension array.

I know that you understand assembly and memory access etc hence I gave a reasonably in-depth explanation. I just hope that I haven't confused you even more :D
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Baggey

Thank you for your time and in depth explanation. much appreciated.

Youve just given me an idea of something else.

So when Parsing or Lexing. We come across Dim:byte (3,4)

So i know ive got a token for dim and  my next two numbers give me 12 elements in memory which i would have an address Pointer to with an indexed offset of the multiplication. To retrieve or Store my byte. Sorta makes sense ::)

I know that's deviating slightly but you've also made the penny drop with something else! Thankyou for that. Id like to write my own IDE/Language Oneday :-X

Kind Regards Baggey

Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

col

#6
QuoteSo i know ive got a token for dim and  my next two numbers give me 12 elements in memory which i would have an address Pointer to with an indexed offset of the multiplication

Indeed, that's ok if the array dimensions have to be constant at compile time according to the language specification.

For thought... how about if the language you refer to supports dynamic array dimensions (ie. not constants), and are say the result of a function call, or a value loaded from a file, meaning that the size is not known at compile time.

For eg, something along the lines of

Dim array:byte(getArrayWidth(), getArrayHeight())

Although this is much more flexible, and seems such an easy language feature to add, it does require a considerably large amount of extra work, both in design and implementation..
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Baggey

Deviating slightly. I have been recommended some books to right a language Parser sort of thing.

But i don't have a Degree in English Technical literature to understand it.

Do you know of a Good "The DUMMIES GUIDE to writing a parser / Lexor"

I mean i need an easy read. How you described the Dim statement was easy to digest.

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on Acer 24" . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!