Forum Discussion
Altera_Forum
Honored Contributor
15 years agoFull On-Chip Memory
Hi,
I'd like to know how to determine if the memory space of an on-chip memory is full. In my nios II system, I generated an on-chip M4K memory. It's size is 4096 bytes with a 32 bits data width. So I've done some trial and error and some calculation about the specifications and have concluded that I have 1023 addressable spaces from 0x000 - 0x3ff. I have calculated that each addressable space can contain 32 bits of data. I tested this with the following code:IOWR(ONCHIP_MEMORY2_1_BASE, 0x3fe, "THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN");
alt_printf("%s\n", IORD(ONCHIP_MEMORY2_1_BASE, 0x3fe));
alt_printf("%s\n", IORD(ONCHIP_MEMORY2_1_BASE, 0x3ff)); The problem I face now is I don't know how big 32 bits of data is because I know that a character is 1 byte and so I hypothesized that a memory space addressed by 0x3fe for example would contain just 4 characters. However it held more than that (the string "this is to test how much data can a memory space contain"). How big is a string anyway? and when I try to read out on a memory space that I didn't write to I get the garbage values below. nios2-terminal: connected to hardware target using JTAG UART on cable
nios2-terminal: "USB-Blaster ", device 1, instance 0
nios2-terminal: (Use the IDE stop button or Ctrl-C to terminate)
Hello onchip mem!
THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
... (goes on for around 400+ lines)
What happened here? Thanks in advanced! Regards,27 Replies
- Altera_Forum
Honored Contributor
As a follow up:
I checked the SOPC Builder component of the on-chip memory and the base address is at 0x00011000 and the end address is at 0x00011fff. Then why is it that I can't access beyond 0x000113ff? Also if the end address is at fff then it should have 4095 addressable space and 16,380 bytes of data. Why does the SOPC indicate an end address of 33FFF? - Altera_Forum
Honored Contributor
i will try for you.
- Altera_Forum
Honored Contributor
Thanks you so much. :)
- Altera_Forum
Honored Contributor
4096 bytes is 1024 words of 32 bits.
The IORD/IOWR macros read and write 32 bit words only, and it isn't an address that you give them, but a register number. When you do IORD(ONCHIP_MEMORY2_1_BASE, 0x3fe), the actual addresses accessed are the 4 bytes at ONCHIP_MEMORY2_1_BASE+4*0x3fe to ONCHIP_MEMORY2_1_BASE+4*0x3fe+3 (0x00011ff8 - 0x00011ffb). When you do IOWR(ONCHIP_MEMORY2_1_BASE, 0x3fe, "THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN") you don't write the string to the onchip memory, you just write its address, which is a 32-bit word. The size of the string doesn't matter. - Altera_Forum
Honored Contributor
Hi Daixiwen,
Thank you for your reply, I appreciate it. --- Quote Start --- When you do IOWR(ONCHIP_MEMORY2_1_BASE, 0x3fe, "THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN") you don't write the string to the onchip memory, you just write its address, which is a 32-bit word. The size of the string doesn't matter --- Quote End --- So where is the string stored in memory then? Is it the same case in writing bit streams? Where will the DATA be stored in the on-chip memory? You said that I just write the 32-bit word address to the onchip memory, then what happens to the DATA? --- Quote Start --- The IORD/IOWR macros read and write 32 bit words only, and it isn't an address that you give them, but a register number. --- Quote End --- It means that I have 1024 registers, each accessing 4 address spaces in the physical memory? Okay, then how many bits of data is stored in each byte address? it isn't 32bits I assume because FFF (4096) addressable spaces at 32 bits each would give 16,380 bytes of data, whereas the memory module is only 4096 bytes big. --- Quote Start --- When you do IORD(ONCHIP_MEMORY2_1_BASE, 0x3fe), the actual addresses accessed are the 4 bytes at ONCHIP_MEMORY2_1_BASE+4*0x3fe to ONCHIP_MEMORY2_1_BASE+4*0x3fe+3 (0x00011ff8 - 0x00011ffb). --- Quote End --- What is the significance of this? Can you point me to what literature this is found? Thank you very much. Regards, - Altera_Forum
Honored Contributor
You created 4096 bytes of memory. Which means 1024 words.
You can use IOWR_32DIRECT (word), IOWR_16DIRECT (half-word), IOWR_8DIRECT(byte) to perform data writes with the following syntax IOWR_32DIRECT(Base_component_address, offset, data). The standard IORD/IOWR macro's perform 32 bits operations. So If you have memory at 0x0 and you wrote 1,2,3 to the memory in word operations using IOWR (with offset increasing by 4) it would look like this if you read it back as bytes (=8 bits). 0x0 = 1 0x1 = 0 0x2 = 0 0x3 = 0 0x4 = 2 0x5 = 0 0x6 = 0 0x7 = 0 0x8 = 3 0x9 = 0 0xA = 0 0xB = 0 If you read it back as words (=32 bits) you would get: 0x0 = 1 0x4 = 2 0x8 = 3 So you have 4096 byte registers = 1024 word registers. The maximum value you can store in a byte = 1111 1111 And the maxium value you can store in a word = 1111 1111 1111 1111 1111 1111 1111 1111 As far as I know the string is converted (using ASCII) to a binary value and then stored, but I'm not sure. Using http://easycalculation.com/ascii-hex.php your string (THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN) would convert to: 01010100 01001000 01001001 01010011 0001 01001001 01010011 0001 01010100 01001111 0001 01010100 01000101 01010011 01010100 0001 01001000 01001111 01010111 0001 01001101 01010101 01000011 01001000 0001 01000100 01000001 01010100 01000001 0001 01000011 01000001 01001110 0001 01000001 0001 01001101 01000101 01001101 01001111 01010010 01011001 0001 01010011 01010000 01000001 01000011 01000101 0001 01000011 01001111 01001110 01010100 01000001 01001001 01001110 Which are quite a few words :). (So you are right about the fact that one word can store 4 characters and 1 byte can store 1 character.) --- Quote Start --- What happened here? --- Quote End --- The crash is probably caused by this: Do you write to the same memory as your pogram runs in? Because that would cause the program to crash like that. You can check it in the system library properties in the NIOS II IDE. You should have 2 different kinds of memory in your SOPC builder. One for the program to run in and one to perform the tests in. - Altera_Forum
Honored Contributor
Hi Thormodo,
So the string converted to binary value would occupy memory space as long as it can? I mean it would just occupy addresses until the data has all been stored. So if I store my string at 0x0 then it would occupy the byte address one by one until it reaches a byte address to store the last byte of binary value? --- Quote Start --- The crash is probably caused by this: Do you write to the same memory as your pogram runs in? Because that would cause the program to crash like that. You can check it in the system library properties in the NIOS II IDE. You should have 2 different kinds of memory in your SOPC builder. One for the program to run in and one to perform the tests in. --- Quote End --- I am using a separate on-chip memory, different from the one that the program runs in. Or so I think. I implemented two onchip memories. The mem_0 containes the reset vector, etc. and that is the memory used in the system library. I use the mem_1 for testing. Regards, - Altera_Forum
Honored Contributor
You mean if you do this:
IOWR_32DIRECT(mem_1,0x0,(int)"THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN"); ? It will try to store all that data in 4 bytes, which won't fit, so it will truncate the data as far as I know. Let me try :). First clearing the memory to all 0x00 : memset((void*)memory_base,0x00, memory_size) and then using the following functions: IOWR_32DIRECT(ALTMEMDDR_1_BASE,0x0,(int)"THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN"); int i; for (i=0x0;i<0x20;i+=4) { printf("Address 0x%X: %x\n",(i+ALTMEMDDR_1_BASE),IORD_32DIRECT(ALTMEMDDR_1_BASE,i)); } Gives (0xC being my ALTMEMDDR_1_BASE): Address 0xC000000: 8030210 Address 0xC000004: 0 Address 0xC000008: 0 Address 0xC00000C: 0 Address 0xC000010: 0 Address 0xC000014: 0 Address 0xC000018: 0 Address 0xC00001C: 0 Hmm it's not even possible to store a string with this code or by using IOWR(ALTMEMDDR_1_BASE,0x0,(int)"IN") . But as you can see it will only store data in the first word. - Altera_Forum
Honored Contributor
Hi Thormodo,
--- Quote Start --- Hmm it's not even possible to store a string with this code or by using IOWR(ALTMEMDDR_1_BASE,0x0,(int)"IN") . But as you can see it will only store data in the first word. --- Quote End --- Hmmm.. but the codes that I tried worked. As you can see in my 1st message, I was able to write that string using the standard IOWR. I did not put a "(int)" before the string though. When I read it and printed it out it was successfully printed. - Altera_Forum
Honored Contributor
--- Quote Start --- Hi Thormodo, Hmmm.. but the codes that I tried worked. As you can see in my 1st message, I was able to write that string using the standard IOWR. I did not put a "(int)" before the string though. When I read it and printed it out it was successfully printed. --- Quote End --- Didn't you get some warnings then? If I use the syntax below it get the following warning: warning: format argument is not a pointer (arg 2) IOWR_32DIRECT(ALTMEMDDR_1_BASE, 0x3fe, (int)"THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN"); printf("%s\n", IORD_32DIRECT(ALTMEMDDR_1_BASE, 0x3fe)); printf("%s\n", IORD_32DIRECT(ALTMEMDDR_1_BASE, 0x3ff)); And it prints out (without crashing) THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN THIS IS TO TEST HOW MUCH DATA CAN A MEMORY SPACE CONTAIN Maybe it's doing something funny with cache memory, I really don't know since I never used strings with NIOS II before.