john120
Banned
- Joined
- Aug 13, 2011
- Messages
- 257
- Helped
- 11
- Reputation
- 22
- Reaction score
- 10
- Trophy points
- 1,298
- Activity points
- 0
the index of 8 is 7 here . because K[0]=1 , k[1] =2 ..... k[8]=9.
you might need to check the libraries of C that search for an item inside an array and tells you at what index it is . but problem if there is more than one !!
#define ARRAY_SIZE 9
const int k[]={1,2,3,4,5,6,7,8,9}
int FindIndex(int *vals, int size, int search)
{
int i;
for ( i = 0; i < size ; i++)
if ( vals[i] == search )
return i;
return -1;
}
This can be to make using a very simple loop on the entire array and return the first index that contain the search value
(and return -1 if the value is not in this array)
Code:#define ARRAY_SIZE 9 const int k[]={1,2,3,4,5,6,7,8,9} int FindIndex(int *vals, int size, int search) { int i; for ( i = 0; i < size ; i++) if ( vals[i] == search ) return i; return -1; }
In your specific example, a call to FindIndex(k, ARRAY_SIZE, 8) should logically return 7
(if the array have more than one occurence of the search value, this fonction always return the lowest index that contain this value and if the value is not in the array, the fonction return -1)
But this is a "true" C example, so I don't think that this can coded as this on a FPGA language ...
=> I implement/test the same thing but using the verilog syntax tomorow (with the print of the result of course)
yannoo@Ubuntoo:~/dev/FPGA$ ./between 1232950
Search the first index when 1232950 is between the array's limits
1232950 is not between 1235900 and 1234900, so the index cannot to be 0
1232950 is not between 1234800 and 1234100, so the index cannot to be 1
1232950 is between 1231900 and 1234000, so the index is 2
jean = 7 + 0.1 * 2
The final value is 7.2
yannoo@Ubuntoo:~/dev/FPGA$ ./between 122700
Search the first index when 122700 is between the array's limits
122700 is not between 1235900 and 1234900, so the index cannot to be 0
122700 is not between 1234800 and 1234100, so the index cannot to be 1
122700 is not between 1231900 and 1234000, so the index cannot to be 2
122700 is not between 1231800 and 1231500, so the index cannot to be 3
122700 is not between 1230300 and 1230090, so the index cannot to be 4
122700 is not between 1230100 and 1228900, so the index cannot to be 5
122700 is between 122800 and 122600, so the index is 6
jean = 7 + 0.1 * 6
The final value is 7.6
yannoo@Ubuntoo:~/dev/FPGA$ ./between 0
Search the first index when 0 is between the array's limits
0 is not between 1235900 and 1234900, so the index cannot to be 0
0 is not between 1234800 and 1234100, so the index cannot to be 1
0 is not between 1231900 and 1234000, so the index cannot to be 2
0 is not between 1231800 and 1231500, so the index cannot to be 3
0 is not between 1230300 and 1230090, so the index cannot to be 4
0 is not between 1230100 and 1228900, so the index cannot to be 5
0 is not between 122800 and 122600, so the index cannot to be 6
Any interval between the array's limits can contain the value 0 :(
The final value is 7.0
#include <stdio.h>
#include <stdlib.h>
const double hi_limit[]={1234900,1234100,1234000,1231500,1230090,1228900,122600};
const double lo_limit[]={1235900,1234800,1231900,1231800,1230300,1230100,122800};
// the number of values into hi_limit[] and low_limit[] array
# define NUM_LIMITS 7
// the jean's starting number (???) and the index multiplier
#define STARTING_NUMBER 7.0f
#define MULTIPLIER 0.1f
double jean = STARTING_NUMBER;
int FindIndex(const double *low, const double *hi, int size, double search)
{
int i;
printf("Search the first index when %.0f is between the array's limits \n", search);
for ( i = 0; i < size ; i++)
{
if ( ((low[i] <= search) && (hi[i] >= search)) || ((low[i] >= search) && (hi[i] <= search)) )
{
printf("%.0f is between %.0f and %.0f, so the index is %d \n", search, lo_limit[i], hi_limit[i], i);
return i;
}
else
{
printf("%.0f is not between %.0f and %.0f, so the index cannot to be %d \n", search, lo_limit[i], hi_limit[i], i);
}
}
printf("Any interval between the array's limits can contain the value %.0f :( \n", search);
return -1;
}
int main(int argc, char **argv)
{
double tosearch = (1234000 + 1231900) / 2;
int firstidx;
if ( argc > 1 )
{
tosearch = atof(argv[1]);
}
firstidx = FindIndex(lo_limit, hi_limit, NUM_LIMITS, tosearch);
if( firstidx != -1 )
{
printf("jean = %.0f + %.1f * %d \n", STARTING_NUMBER, MULTIPLIER, firstidx);
jean = STARTING_NUMBER + (MULTIPLIER * (float)(firstidx));
}
printf("The final value is %.1f \n", jean);
}
can you plz help me on the codes for this index searching:I am thinking that it is better that I find the index which correspond to a number which has been read by the processor then
that index is multiplied by 0.1 and then added to the starting number which is 8
for example
const double hi_limit[]={1234900,1234100,1234000,1231500,1230090,1228900,122600};
const double lo_limit[]={1235900,1234800,1231900,1231800,1230300,1230100,122800};
the index of those bold number will be 2 then jean=jean+0.1*2 ;as jean has been initialized equals 7.0 the output will be 7.2%
meanwhile what is needed is to find that index 2 (how may I search it inside array);in those two arrays no number appearing twice.
And also the comparison has to be made inside the FindIndex function because I need to see whether the value read is in a given boundary and the calculate jean and then print its final value.
Thanks.
I declared the function FindIndex and coded,but when I call it it says:Attempt to create a pointer to a constant
The given codes does not give me the index of a number in the array,help to solve the issue plz.can you plz help me on the codes for this index searching:I am thinking that it is better that I find the index which correspond to a number which has been read by the processor then
that index is multiplied by 0.1 and then added to the starting number which is 8
for example
const double hi_limit[]={1234900,1234100,1234000,1231500,1230090,1228900,122600};
const double lo_limit[]={1235900,1234800,1231900,1231800,1230300,1230100,122800};
the index of those bold number will be 2 then jean=jean+0.1*2 ;as jean has been initialized equals 7.0 the output will be 7.2%
meanwhile what is needed is to find that index 2 (how may I search it inside array);in those two arrays no number appearing twice.
And also the comparison has to be made inside the FindIndex function because I need to see whether the value read is in a given boundary and the calculate jean and then print its final value.
Thanks.
The given codes does not give me the index of a number in the array,help to solve the issue plz.
Hello See all my codes below:Can you please post your array please ?
(I don't understand why this can to work with somes datas and not with others ...)
[note that this code return **the first** index when the search value is contained between the high and low limits arrays ...]
for ( i =0; i <size ; i++)
while((value1<= j[i])&&(k[i]<=value1))
for ( i =0; i <size ; i++)
if ( ((value1<= j[i]) &&( k[i] <=value1)) || ((value1<= k[i]) &&( j[i] <=value1)) )
Why this ?
Code:for ( i =0; i <size ; i++) while((value1<= j[i])&&(k[i]<=value1))
You don't want to make this ?
(cf. use if instead while + test if value1 is **between** k and j with no assumption than k is always <= j )Code:for ( i =0; i <size ; i++) if ( ((value1<= j[i]) &&( k[i] <=value1)) || ((value1<= k[i]) &&( j[i] <=value1)) )
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
//#define ARRAY_SIZE 7
#define ARRAY_SIZE 9
int i, found;
int value;
int j[]={1,2,3,4,5,6,7,8,9};
void main(void)
{
set_tris_c(0xff);
set_tris_b(0x00);
lcd_init();
delay_us(200);
while(1)
{
set_timer1(0);
setup_timer_1(t1_external | T1_DIV_BY_1);
delay_ms(1000);
setup_timer_1(T1_DISABLED);
value=get_timer1();
delay_ms(20);
for ( i = 0, found = -1 ; i < ARRAY_SIZE ; i++)
if ( j[i] == value )
{
printf(lcd_putc,"j[%d] is %d", i, value);
found = 1;
}
if ( found == -1 )
printf(lcd_put, "cannot found the %d value :(", value);
delay_ms(1000);
} // end while(1)
} // end main()
Ok, so you want everything into the main() for to begin, right ?
if yes, this is something like this :
Code:#include <16F877A.h> #DEVICE ADC=10 #fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT #use delay(clock=20000000) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) #include "lcd.c" //#define ARRAY_SIZE 7 #define ARRAY_SIZE 9 int i, found; int value; int j[]={1,2,3,4,5,6,7,8,9}; void main(void) { set_tris_c(0xff); set_tris_b(0x00); lcd_init(); delay_us(200); while(1) { set_timer1(0); setup_timer_1(t1_external | T1_DIV_BY_1); delay_ms(1000); setup_timer_1(T1_DISABLED); value=get_timer1(); delay_ms(20); for ( i = 0, found = -1 ; i < ARRAY_SIZE ; i++) if ( j[i] == value ) { printf(lcd_putc,"j[%d] is %d", i, value); found = 1; } if ( found == -1 ) printf(lcd_put, "cannot found the %d value :(", value); delay_ms(1000); } // end while(1) } // end main()
PS : please, respond me about what platform you use for to transfert the code from your computer to the FGPA board
(I haven't already find how to make this from my Linux box to a SPARTAN FPGA board :sad: )
#include <16F877A.h>
#DEVICE ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include "lcd.c"
set_timer1(0);
setup_timer_1(t1_external | T1_DIV_BY_1);
delay_ms(1000);
setup_timer_1(T1_DISABLED);
value=get_timer1();
// your specifics dev plateform
// #include <16F877A.h>
// #DEVICE ADC=10
// #fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
// #use delay(clock=20000000)
// #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#include "lcd.c"
// specific printf / delay headers used on my Linux box
#include <stdio.h>
#include <unistd.h>
#define delay_ms(t) usleep(t*1000L)
//#define ARRAY_SIZE 7
#define ARRAY_SIZE 9
int i, found;
int value, n = 0;
int j[]={1,2,3,4,5,6,7,8,9};
void main(void)
{
// set_tris_c(0xff);
// set_tris_b(0x00);
// lcd_init();
delay_ms(200);
while(1)
{
printf("\nStep %d \n", n++);
// set_timer1(0);
// setup_timer_1(t1_external | T1_DIV_BY_1);
delay_ms(1000);
// setup_timer_1(T1_DISABLED);
// value=get_timer1();
value = rand() % 20;
printf("search the value %d \n", value);
delay_ms(20);
for ( i = 0, found = -1 ; i < ARRAY_SIZE ; i++)
if ( j[i] == value )
{
printf("j[%d] is %d \n", i, value);
found = 1;
}
if ( found == -1 )
printf("cannot found the %d value :( \n", value);
delay_ms(100);
} // end while(1)
} // end main()
Step 0
search the value 3
j[2] is 3
Step 1
search the value 6
j[5] is 6
Step 2
search the value 17
cannot found the 17 value :(
Step 3
search the value 15
cannot found the 15 value :(
Step 4
search the value 13
cannot found the 13 value :(
Step 5
search the value 15
cannot found the 15 value :(
Step 6
search the value 6
j[5] is 6
...
module declarations_5;
reg [31:0] VideoRam [7:0]; // An 8-word by 32-bit wide memory.
initial begin
VideoRam[1] = 'bxz; // We must specify an index for a memory.
VideoRam[2] = 1;
VideoRam[7] = VideoRam[VideoRam[2]]; // Need 2 clock cycles for this.
VideoRam[8] = 1; // Careful! the compiler won't complain about this!
// Verify what we entered:
$display("VideoRam[0] is %b",VideoRam[0]);
$display("VideoRam[1] is %b",VideoRam[1]);
$display("VideoRam[2] is %b",VideoRam[2]);
$display("VideoRam[7] is %b",VideoRam[7]);
end
endmodule
VideoRam[0] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
VideoRam[1] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz
VideoRam[2] is 00000000000000000000000000000001
VideoRam[7] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz
module between;
reg clock;
integer idx;
integer search = 8;
reg [31:0] limits [8:0];
initial
begin
clock = 0; idx = 0;
limits[0] = 1;
limits[1] = 2;
limits[2] = 3;
limits[3] = 4;
limits[4] = 5;
limits[5] = 6;
limits[6] = 7;
limits[7] = 8;
limits[8] = 9;
#1000 $finish;
end
always #10 clock = ~ clock;
always
begin @ (negedge clock);
if (idx == 9)
idx = 0;
else
idx = idx + 1;
if ( limits[idx] == search)
$display("time = ",$time," limits[", idx, "]=", search);
end
endmodule
yannoo@Ubuntoo:~/dev/FPGA$ make
iverilog -o between_fpga between.vl
vvp between_fpga
time = 140 limits[ 7]= 8
time = 320 limits[ 7]= 8
time = 500 limits[ 7]= 8
time = 680 limits[ 7]= 8
time = 860 limits[ 7]= 8
if ( limits[idx] == search)
begin
$display("time = ",$time," limits[", idx, "]=", search);
$finish;
end
module between;
reg clock;
integer idx;
integer search = 11;
reg [31:0] limits [8:0];
initial
begin
clock = 0; idx = 0;
limits[0] = 1;
limits[1] = 2;
limits[2] = 3;
limits[3] = 4;
limits[4] = 5;
limits[5] = 6;
limits[6] = 7;
limits[7] = 8;
limits[8] = 9;
#1000 $finish;
end
always #10 clock = ~ clock;
always
begin @ (negedge clock);
if ( limits[idx] == search)
begin
$display("time = ",$time," limits[", idx, "]=", search);
$finish;
end
else
if (idx == 9)
begin
$display("Cannot found the value ", search, " into the array");
$finish;
end
else
idx = idx + 1;
end
endmodule
time = 160 limits[ 7]= 8
integer search = 11;
Cannot found the value 11 into the array
I have see this at https://iroi.seu.edu.cn/books/asics/book2/ch11/CH11.02.htm :
There are no multidimensional arrays in Verilog, but we may declare a memory data type as an array of registers
You have to use something like this :
Code:module declarations_5; reg [31:0] VideoRam [7:0]; // An 8-word by 32-bit wide memory. initial begin VideoRam[1] = 'bxz; // We must specify an index for a memory. VideoRam[2] = 1; VideoRam[7] = VideoRam[VideoRam[2]]; // Need 2 clock cycles for this. VideoRam[8] = 1; // Careful! the compiler won't complain about this! // Verify what we entered: $display("VideoRam[0] is %b",VideoRam[0]); $display("VideoRam[1] is %b",VideoRam[1]); $display("VideoRam[2] is %b",VideoRam[2]); $display("VideoRam[7] is %b",VideoRam[7]); end endmodule VideoRam[0] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx VideoRam[1] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz VideoRam[2] is 00000000000000000000000000000001 VideoRam[7] is xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz
- - - Updated - - -
I have a verilog source that work well when emulated on my linux box :
=> this search the value 8 (cf. the line "integer search = 8") into the array limits[] declared at the line "reg [31:0] limits [8:0];"Code:module between; reg clock; integer idx; integer search = 8; reg [31:0] limits [8:0]; initial begin clock = 0; idx = 0; limits[0] = 1; limits[1] = 2; limits[2] = 3; limits[3] = 4; limits[4] = 5; limits[5] = 6; limits[6] = 7; limits[7] = 8; limits[8] = 9; #1000 $finish; end always #10 clock = ~ clock; always begin @ (negedge clock); if (idx == 9) idx = 0; else idx = idx + 1; if ( limits[idx] == search) $display("time = ",$time," limits[", idx, "]=", search); end endmodule
This give this at output when emulated on my Linux box
(I don't know how to handle only one occurence, so I have employ "#1000 $finish;" for to be sure that this test stop a day)Code:yannoo@Ubuntoo:~/dev/FPGA$ make iverilog -o between_fpga between.vl vvp between_fpga time = 140 limits[ 7]= 8 time = 320 limits[ 7]= 8 time = 500 limits[ 7]= 8 time = 680 limits[ 7]= 8 time = 860 limits[ 7]= 8
[note than the array begin at 0, not at 1]
- - - Updated - - -
I have found how to finish as soon as possible :
Code:if ( limits[idx] == search) begin $display("time = ",$time," limits[", idx, "]=", search); $finish; end
=> now, I have only to found how to handle the nesting of multiples modules between them for to have the equivalent of fonctions
- - - Updated - - -
This version exit at the first index that contain the searched value or display a message such as the value isn't found at the end
Code:module between; reg clock; integer idx; integer search = 11; reg [31:0] limits [8:0]; initial begin clock = 0; idx = 0; limits[0] = 1; limits[1] = 2; limits[2] = 3; limits[3] = 4; limits[4] = 5; limits[5] = 6; limits[6] = 7; limits[7] = 8; limits[8] = 9; #1000 $finish; end always #10 clock = ~ clock; always begin @ (negedge clock); if ( limits[idx] == search) begin $display("time = ",$time," limits[", idx, "]=", search); $finish; end else if (idx == 9) begin $display("Cannot found the value ", search, " into the array"); $finish; end else idx = idx + 1; end endmodule
With search = 8
Code:time = 160 limits[ 7]= 8
And if I modify the value of search with 11 :
Code:integer search = 11;
Code:Cannot found the value 11 into the array
But in first view, you want a true C source (not a verilog source), so my last post with the linux source seem to more adapted for you
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?