EmbDev.net

Forum: FPGA, VHDL & Verilog Please help I am stuck.


von Nikhil R. (Company: Dublin City University) (ruudrarai)


Rate this post
0 useful
not useful
Hello all,

I am pretty new to verilog. I need help ASAP.

I am writing a code for ALU, where I need to add a verilog 
module(ADDER/SUB) in VHDL(ALU).

When I am trying to use if statement for ctrl I am getting error for 
"illegal expression for if statement".

What should I do, any hel would be appreciated(as soon as possible).
The code is as follows

module add_nbit(ctr,in1,in2,inc,s1,c1);

    input [3:0]ctr;   // control
   input [15:0]in1;
   input [15:0]in2;
   input inc;
   output [15:0]s1;
   output c1;
   wire [3:0]a1=4'b0000;     // reg to save the value of ctr
   wire [3:0]a2=4'b0001;
   genvar i;
   generate for(i=0;i<1;i=i+1)begin: A
   if (ctr == 0) begin       // tried "0000", 4'b0000.
   addnbtt A1(in1,in2,1'b0,s1,c1);
   end
   if (ctr == 1) begin
   addnbtt A2(in1,~in2,1'b1,s1,c1);
   end
   end
  endgenerate
endmodule


Or any other way where I can achieve this aim. In my ALU I have a 
control wire which will initiate my add and subtract using verilog 
module.
PLEASE HELP

Nikhil Rai

von Astudentofminewhowasalittlepiggie (Guest)


Rate this post
0 useful
not useful
2nd if needs to be else if.

von Lothar M. (lkmiller) (Moderator)


Rate this post
0 useful
not useful
Nikhil Rai wrote:
> generate for (i=0;i<1;i=i+1) begin:
Thats nonsense, you know?

Your problem here is, that an if-statement can only be placed in a 
"always" block (as far as I can see, I'm just a VHDL guy...). This way 
it compiles fine:
1
 module add_nbit(ctr,in1,in2,inc,s1,c1);
2
3
input [3:0]ctr; // control
4
input [15:0]in1;
5
input [15:0]in2;
6
input inc;
7
output [15:0]s1;
8
output c1;
9
wire [3:0]a1=4'b0000; // reg to save the value of ctr
10
wire [3:0]a2=4'b0001;
11
//genvar i;
12
//generate for(i=0;i<1;i=i+1)begin: A
13
always @(ctr or in1 or in2)
14
  begin
15
  if (ctr == 0) begin // tried "0000", 4'b0000.
16
    addnbtt A1(in1,in2,1'b0,s1,c1);
17
  end
18
  if (ctr == 1) begin
19
    addnbtt A2(in1,~in2,1'b1,s1,c1);
20
  end
21
end
22
//end
23
//endgenerate
24
endmodule

Just dig a little bit around. You have the internet!
http://www.electrosofts.com/verilog/if_else.html
http://www.asic-world.com/verilog/vbehave2.html

: Edited by Moderator

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.