Author Topic: This program dosen't work...why??  (Read 5121 times)

0 Members and 1 Guest are viewing this topic.

Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
This program dosen't work...why??
« on: December 27, 2010, 10:34:03 am »
So I tried writing a program without writing of half the code.

Result: nothing happens, not even a crash :(

Can someone tell me where my stupid beginner error is?

Here is the code:
Code: [Select]
#include <os.h>

//addresses
#define SCREEN_ADDRESS 0xA4000100 ;

//prototyps
void set_screen (void) ;
void set_value(int address, unsigned int value) ;
void clear_screen(void) ;

//global variables
int screen_cords[240][180] ;


int main(void)
{


set_screen() ;

clear_screen() ;

while ( !isKeyPressed ( KEY_NSPIRE_ESC) )
{
set_value( screen_cords[100][100] , 0x00 ) ;
set_value( screen_cords[100][101] , 0x00 ) ;
set_value( screen_cords[100][102] , 0x00 ) ;
set_value( screen_cords[101][100] , 0x00 ) ;
set_value( screen_cords[101][101] , 0x00 ) ;
set_value( screen_cords[101][102] , 0x00 ) ;
set_value( screen_cords[102][100] , 0x0F ) ;
set_value( screen_cords[102][101] , 0xFF ) ;
set_value( screen_cords[102][102] , 0x00 ) ;
set_value( screen_cords[103][100] , 0xF0 ) ;
set_value( screen_cords[103][101] , 0x00 ) ;
set_value( screen_cords[103][102] , 0xF0 ) ;
set_value( screen_cords[104][100] , 0xF0 ) ;
set_value( screen_cords[104][101] , 0x00 ) ;
set_value( screen_cords[104][102] , 0xF0 ) ;
set_value( screen_cords[105][100] , 0xFF ) ;
set_value( screen_cords[105][101] , 0xFF ) ;
set_value( screen_cords[105][102] , 0xF0 ) ;
set_value( screen_cords[106][100] , 0xF0 ) ;
set_value( screen_cords[106][101] , 0x00 ) ;
set_value( screen_cords[106][102] , 0xF0 ) ;
set_value( screen_cords[107][100] , 0xF0 ) ;
set_value( screen_cords[107][101] , 0x00 ) ;
set_value( screen_cords[107][102] , 0xF0 ) ;
set_value( screen_cords[108][100] , 0xF0 ) ;
set_value( screen_cords[108][101] , 0x00 ) ;
set_value( screen_cords[108][102] , 0xF0 ) ;
set_value( screen_cords[110][100] , 0xFF ) ;
set_value( screen_cords[110][101] , 0xFF ) ;
set_value( screen_cords[110][102] , 0xFF ) ;
}

return 0;


}


void set_screen(void)
{


int ycordinate, xcordinate ;
int screen_address = SCREEN_ADDRESS ;

for ( ycordinate = 1 ; ycordinate == 240 ; ycordinate++ )
{
for ( xcordinate = 1 ; xcordinate == 180 ; xcordinate++ )
{
screen_cords[ycordinate][xcordinate] = (screen_address + (xcordinate-1) + (ycordinate-1)*180) ;
}
}


}


void set_value(int address, unsigned int value)
{


(*(volatile unsigned*)address) = value ;


}


void clear_screen(void)
{


int ycordinate, xcordinate ;

for ( ycordinate = 1 ; ycordinate == 240 ; ycordinate++ )
{
for ( xcordinate = 1 ; xcordinate == 180 ; xcordinate++ )
{
set_value(screen_cords[ycordinate][xcordinate] , 0x00 ) ;
}
}


}


Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: This program dosen't work...why??
« Reply #1 on: December 27, 2010, 10:56:37 am »
Each byte of the screen buffer contains 2 bits.
int (of screen_cords) is 4 bytes long.
The screen width is 320 (see SCREEN_WIDTH in Ndless's common.h).

240*180=43200 > SCREEN_BYTES_SIZE (38400), so your code probably fails with a buffer overflow.
Ndless.me with the finest TI-Nspire programs

Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
Re: This program dosen't work...why??
« Reply #2 on: December 27, 2010, 11:21:44 am »
320/2=160... :banghead:
what was I thinking...

but there has to be another error.
*shrear tries to find the error*

*wonders why
      (*(volatile unsigned*)0xA4005200) = 0xFF ;
      (*(volatile unsigned*)0xA4005201) = 0xFF ;
      (*(volatile unsigned*)0xA4005202) = 0xFF ;
      (*(volatile unsigned*)0xA4005203) = 0xFF ;
      (*(volatile unsigned*)0xA4005204) = 0xFF ;
      (*(volatile unsigned*)0xA4005205) = 0xFF ;
      (*(volatile unsigned*)0xA4005206) = 0xFF ;
a)works b)causes a reboot.*
« Last Edit: December 27, 2010, 11:43:55 am by shrear »

Offline ExtendeD

  • CoT Emeritus
  • LV8 Addict (Next: 1000)
  • *
  • Posts: 825
  • Rating: +167/-2
    • View Profile
Re: This program dosen't work...why??
« Reply #3 on: December 27, 2010, 11:48:52 am »
"unsigned" (or unsigned int) means 4 bytes (i.e. a word). word writes must be "aligned", i.e. to an address multiple of 4.
Use instead something like:
(*(volatile unsigned*)0xA4005200) = 0xFFFFFFFF;
(*(volatile unsigned short*)0xA4005204) = 0xFFFFF;
(*(volatile unsigned char*)0xA4005206) = 0xFF;


By the way here is a more simple clear screen, which will be included in Ndless 2.0:
Code: [Select]
void clrscr(void) {
memset(SCREEN_BASE_ADDRESS, 0xFF, SCREEN_BYTES_SIZE);
}
Ndless.me with the finest TI-Nspire programs

Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
Re: This program dosen't work...why??
« Reply #4 on: December 27, 2010, 12:12:43 pm »
(*(volatile unsigned*)0xA4005200) = 0xFFFFFFFF;
(*(volatile unsigned char*)0xA4005206) = 0xFF;
do nothing


just to be safe

(*(volatile unsigned*)b) = a;

puts the value a into the address "b" or?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: This program dosen't work...why??
« Reply #5 on: December 27, 2010, 12:21:54 pm »
You only need "volatile" when writing to memory-mapped I/O ports. In this case, it will suffice to use *(unsigned*)b = a;

And 0xF is actually a white pixel, might you have been expecting black?
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
Re: This program dosen't work...why??
« Reply #6 on: December 27, 2010, 12:30:28 pm »
You only need "volatile" when writing to memory-mapped I/O ports. In this case, it will suffice to use *(unsigned*)b = a;

its listed under memory-mapped I/O ports at hackspire


And 0xF is actually a white pixel, might you have been expecting black?

OK that could be it

« Last Edit: December 27, 2010, 12:30:45 pm by shrear »

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: This program dosen't work...why??
« Reply #7 on: December 27, 2010, 12:33:32 pm »
0x00 or 0x01 will give you black. :)


Offline shrear

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 193
  • Rating: +17/-0
    • View Profile
Re: This program dosen't work...why??
« Reply #8 on: December 27, 2010, 01:16:07 pm »
Well big thanks to those who helped me.
I got my screen clean  :)