0

I want to ask about switching one bit for example x[3] in bit vector x[0:3] to one if it's zero or to zero if it's one in verilog.So if x=0101 it will become x=0100 .I have tried concatination with not but it's error in verilog .Can you help me to do that? My code is here :http://www.edaplayground.com/x/JAc where x:input and y:x after mutate one bit

Thanks in advance.

Sereena
  • 37
  • 3
  • 8

2 Answers2

1

To alter one bit as part of a bus:

module bit_mangle(
  input  [3:0] x,
  output [3:0] y
);

  always @* begin
    y = {x[3:1], ~x[0]} ;
  end

endmodule

I have updated a copy of your code on EDA playground, and fixed a few issues in the testharness. Working simulation on EDA Playground.

It is more common to define buses from [SIZE-1:0]. Your old Mutation code was trying to drive the input in two places rather than letting the level above drive the value.

Morgan
  • 19,934
  • 8
  • 58
  • 84
0
always_comb begin
   x    = 4'b0101 ; 
   x[3] = ~x[3] ; //overwrite x[3] with its complement
end

or

always_comb begin
   x    = 4'b0101 ; 
   x[3] = x[3] ^ 1'b1 ; //XOR x[3] with 1
end
Ari
  • 7,251
  • 11
  • 40
  • 70
  • Thank you .. but there is still many errors.This my code can you help me please? where x:input and y:x after mutate 1 bit. module Mutation(x,y); input [0:3] x=0000; output [0:3] y; reg [0:3] y; always @(x) begin x = 4'b0101 ; x[3] = ~x[3] ; //overwrite x[3] with its complement y={x[0:3]}; end endmodule //also and this is the test bench module TBMutation; parameter s=4; reg [0:s-1] x; wire [0:s-1] y; Mutation m (.x(x),.y(y),); initial begin // Initialize Input x = 2; // Wait 100 ns for global reset to finish #100; end endmodule – Sereena Nov 18 '14 at 23:02
  • This is the error :Line 30: Procedural assignment to a non-register x is not permitted, left-hand side should be reg/integer/time/genvar line 30 is at :x[3] = ~x[3] ; – Sereena Nov 18 '14 at 23:20
  • 2
    It's good practice to write your code in a clean way -rather than in comments - so others can read it easily. You can also write it here http://www.edaplayground.com/ and post a link on this website. Also, you may want to review this page before posting questions: http://stackoverflow.com/help/how-to-ask – Ari Nov 18 '14 at 23:24
  • Assignments should either be in `always` blocks or with `assign` keyword. – Ari Nov 19 '14 at 00:32
  • but in my code assignments are inside always ,can you write the code after modification to understand what you mean? – Sereena Nov 19 '14 at 00:44
  • No, line 9 is outside. – Ari Nov 19 '14 at 01:02
  • I solve it this is the true one :) always @(x) begin y = {x[0],x[1],x[2],~x[3]}; end Thank you Ari – Sereena Nov 19 '14 at 01:05