Verilog 3 bit concentrate of 2 variables - verilog
I am Trying to understand this bit of Verilog code..
reg [2:0] SYNC;
always #(posedge clk) SYNC <= {SYNC[1:0], ASYNC};
wire SYNC_risingedge = (SYNC[2:1] == 2'b01);
wire SYNC_fallingedge = (SYNC[2:1] == 2'b10);
wire SYNC_on = ~SYNC[1];
From what i understand.
A 3 bit reg is made (sync)
When the clock rises, sync is equal to the combination of bits 1 and 0 with the current value of (async).
The SYNC_risingedge is equal to (sync) bits 2 and 1 and the value of binary '01'
The SYNC_fallingedge is equal to (sync) bits 2 and 1 and the value of binary '10'
The SYNC_on is equal to the inverse of sync.
My questions are next to the lines in quotes.
reg [2:0] SYNC;
always #(posedge clk) SYNC <= {SYNC[1:0], ASYNC}; *"does this mean that it concentrates the state of ASYNC with only bits 1 and 0?"*
wire SYNC_risingedge = (SYNC[2:1] == 2'b01); *"is the binary number 01 placed only in bits 2 and 1? if so, how does it affect the previous line?"*
wire SYNC_fallingedge = (SYNC[2:1] == 2'b10); *"same question as previous line"*
wire SYNC_on = ~SYNC[1]; *"What does the [1] mean in ~SYNC[1]?"*
I've scoured the web, looking for Verilog syntax to understand this bit of code, but have come up short.
Any assistance would be appreciated.
Question 1
always #(posedge clk) SYNC <= {SYNC[1:0], ASYNC};
does this mean that it concentrates the state of ASYNC with only bits 1 and 0?
I think you mean "concatenates", and you're about right, yes. This statement essentially left-shifts SYNC by 1 and then puts the value of ASYNC in bit 0. This shifting operation takes place on each rising clock edge.
Complete table:
SYNC | ASYNC | New SYNC Setting
---------+-----------|--------------------
000 | 0 | 000
001 | 0 | 010
010 | 0 | 100
011 | 0 | 110
100 | 0 | 000
101 | 0 | 010
110 | 0 | 100
111 | 0 | 110
000 | 1 | 001
001 | 1 | 011
010 | 1 | 101
011 | 1 | 111
100 | 1 | 001
101 | 1 | 011
110 | 1 | 101
111 | 1 | 111
Question 2
wire SYNC_risingedge = (SYNC[2:1] == 2'b01);
is the binary number 01 placed only in bits 2 and 1? if so, how does it affect the previous line?
No, the == is a test, not an assignment. If those two bits of SYNC match 2b'01, then the SYNC_risingedge wire will be high. Otherwise it will be low.
Note: the "assignment" to SYNC_risingedge is asynchronous - it's just combinational logic.
Question 3
wire SYNC_fallingedge = (SYNC[2:1] == 2'b10);
same question as previous line
Same answer, too.
Question 4
wire SYNC_on = ~SYNC[1];
What does the [1] mean in ~SYNC[1]?
It's just referring to bit 1 of SYNC. SYNC_on is high whenever SYNC[1] is low, and vice versa.
Related
What is the meaning of "= |" in Verilog?
Can someone please tell me what is the meaning of "= |" in these Verilog lines: wire result; wire [NUM_BITS-1:0] pterms; assign result = | pterms; If it means assign result = result | pterms, then does this mean that it does an OR operation between the result wire and pterms[0]?
No, in your code, | is the reduction-OR operator which does a bitwise OR of all the bits of pterms and assigns the 1-bit result to the result wire. Refer to IEEE Std 1800-2017, section 11.4.9 Reduction operators. If pterms is 4 bits, then the assignment is the same as: assign result = pterms[3] | pterms[2] | pterms[1] | pterms[0]; Here is a running example: module tb; parameter NUM_BITS = 4; wire result; logic [NUM_BITS-1:0] pterms; assign result = | pterms; initial begin for (int i=0; i<16; i++) begin #1 pterms = i; #1 $displayb(pterms,,result); end end endmodule Displays: 0000 0 0001 1 0010 1 0011 1 0100 1 0101 1 0110 1 0111 1 1000 1 1001 1 1010 1 1011 1 1100 1 1101 1 1110 1 1111 1
Counting 15's in Cribbage Hand
Background This is a followup question to my previous finding a straight in a cribbage hand question and Counting Pairs in Cribbage Hand Objective Count the number of ways cards can be combined to a total of 15, then score 2 points for each pair. Ace worth 1, and J,Q,K are worth 10. What I have Tried So my first poke at a solution required 26 different formulas. Basically I checked each possible way to combine cards to see if the total was 15. 1 way to add 5 cards, 5 ways to add 4 cards, 10 ways to add 3 cards, and 10 ways to add 2 cards. I thought I had this licked until I realized I was only looking at combinations, I had not considered the fact that I had to cap the value of cards 11, 12, and 13 to 10. I initially tried an array formula something along the lines of: MIN(MOD(B1:F1-1,13)+1,10) But the problem with this is that MIN takes the minimum value of all results not the individual results compared to 10. I then tried it with an IF function, which worked, but involved the use of CSE formula even wehen being used with SUMPRODUCT which is something I try to avoid when I can IF(MOD(B1:F1-1,13)+1<11,MOD(B1:F1-1,13)+1,10) Then I stumble on an answer to a question in code golf which I modified to lead me to this formula, which I kind of like for some strange reason, but its a bit long in repetitive use: --MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2) My current working formulas are: 5 card check =(SUMPRODUCT(--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2))=15)*2 4 card checks =(SUM(AGGREGATE(15,6,--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2),{1,2,3,4}))=15)*2 =(SUM(AGGREGATE(15,6,--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2),{1,2,3,5}))=15)*2 =(SUM(AGGREGATE(15,6,--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2),{1,2,4,5}))=15)*2 =(SUM(AGGREGATE(15,6,--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2),{1,3,4,5}))=15)*2 =(SUM(AGGREGATE(15,6,--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2),{2,3,4,5}))=15)*2 3 card checks same as 4 card checks using all combinations for 3 cards in the {1,2,3}. There are 10 different combinations, so 10 different formulas. The 2 card check was based on the solution by Tom in Counting Pairs in Cribbage Hand and all two cards are checked with a single formula. (yes it is CSE) 2 card check {=SUM(--(--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2)+TRANSPOSE(--MID("01020304050607080910101010",1+(MOD(B1:F1-1,13)*2),2))=15))} Question Can the 3 and 4 card combination sum check be brought into a single formula similar to the 2 card check? Is there a better way to convert cards 11,12,13 to a value of 10? Sample Data | B | C | D | E | F | POINTS +----+----+----+----+----+ | 1 | 2 | 3 | 17 | 31 | <= 2 (all 5 add to 15) | 1 | 2 | 3 | 17 | 32 | <= 2 (Last 4 add to 15) | 11 | 18 | 31 | 44 | 5 | <= 16 ( 4x(J+5), 4X(5+5+5) ) | 6 | 7 | 8 | 9 | 52 | <= 4 (6+9, 7+8) | 1 | 3 | 7 | 8 | 52 | <= 2 (7+8) | 2 | 3 | 7 | 9 | 52 | <= 2 (2+3+K) | 2 | 4 | 6 | 23 | 52 | <= 0 (nothing add to 15) Excel Version Excel 2013
For 5: =(SUMPRODUCT(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10))=15)*2 For 4: =SUMPRODUCT(--(MMULT(INDEX(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)*ROW($1:$10)^0,ROW($1:$5),{1,2,3,4;1,2,3,5;1,2,4,5;1,3,4,5;2,3,4,5}),ROW($1:$4)^0)=15))*2 For 3 =SUMPRODUCT(--(MMULT(INDEX(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)*ROW($1:$10)^0,ROW($1:$10),{1,2,3;1,2,4;1,2,5;1,3,4;1,3,5;1,4,5;2,3,4;2,3,5;2,4,5;3,4,5}),ROW($1:$3)^0)=15))*2 For 2: SUMPRODUCT(--((CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10))+(TRANSPOSE(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)))=15)) All together: =(SUMPRODUCT(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10))=15)*2+ SUMPRODUCT(--(MMULT(INDEX(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)*ROW($1:$10)^0,ROW($1:$5),{1,2,3,4;1,2,3,5;1,2,4,5;1,3,4,5;2,3,4,5}),ROW($1:$4)^0)=15))*2+ SUMPRODUCT(--(MMULT(INDEX(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)*ROW($1:$10)^0,ROW($1:$10),{1,2,3;1,2,4;1,2,5;1,3,4;1,3,5;1,4,5;2,3,4;2,3,5;2,4,5;3,4,5}),ROW($1:$3)^0)=15))*2+ SUMPRODUCT(--((CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10))+(TRANSPOSE(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)))=15)) For older versions we need to "trick" INDEX into accepting the arrays as Row and Column References: We do that by using N(IF({1},[thearray])) =(SUMPRODUCT(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10))=15)*2+ SUMPRODUCT(--(MMULT(INDEX(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)*ROW($1:$10)^0,N(IF({1},ROW($1:$5))),N(IF({1},{1,2,3,4;1,2,3,5;1,2,4,5;1,3,4,5;2,3,4,5}))),ROW($1:$4)^0)=15))*2+ SUMPRODUCT(--(MMULT(INDEX(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)*ROW($1:$10)^0,N(IF({1},ROW($1:$10))),N(IF({1},{1,2,3;1,2,4;1,2,5;1,3,4;1,3,5;1,4,5;2,3,4;2,3,5;2,4,5;3,4,5}))),ROW($1:$3)^0)=15))*2+ SUMPRODUCT(--((CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10))+(TRANSPOSE(CHOOSE(MOD(A1:E1-1,13)+1,1,2,3,4,5,6,7,8,9,10,10,10,10)))=15)) This is a CSE That must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
Implementation of a Command
I am a bit lost on understanding the implementation of a specific command. In this example, there is a command passed 0x00c6ba23 which is 0000 0000 1100 0110 1011 1010 0010 0011 in binary I am attempting to find the ALU control unit’s inputs for this instruction. From this I can see opcode = 0100011 imm[4:0] = 10100 funct3 = 011 (incorrect...) rs1 = 01101 rs2 = 01100 imm[11:5] = 0000000 I am using this image to decode it My question is how do I get the ALU control bits and ALUOp control bits for this function? And why is the function SD, even though the funct 3 is showing 011 instead of 111?
... why is the function SD, even though the funct 3 is showing 011 instead of 111? 011 is correct. The funct3 bits must be 011 in order for this to be an SD instruction. According to page 105 of https://content.riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf the SD instruction has the format: | imm[11:5] | rs2 | rs1 | 011 | imm[4:0] | 0100011 | If the funct3 bits were 111 then this instruction would not be SD. ... how do I get the ALU control bits and ALUOp control bits for this function? Since this is an SD instruction, you can read those bits straight out of the SD line of the lower table in the diagram that you referenced in your question.
RFC 1035 Header Structure
I'm studying about dns and would like to understand about this information, because I could not fully understand. The header contains the following fields: 1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA| Z | RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ I would to know what mean this numbers on top.
The numbers across the top are simply the bit numbers within the 16 bit word, although as is common with the RFC series of documents they're ordered from most significant bit to least, instead of the (more intuitive) other way around. So, for example, given an array data of octets containing that header, the ID would be: (data[0] << 8) | data[1] and the QR bit would be the most significant bit of data[2]
verilog subtraction does not yield carry out
I want to design an ALU to perform some operations on two 8bits register ( A , B ) and in order to detect carry_out, I defined a 9bits register as temp and put the results of operation on A,b in that register. The MSb of that temp register is used as carry out. Here is a part of my code: module ALU(input signed [7:0] A, input [7:0] B, input carry_in, input [2:0] acode, output reg [7:0] R, output zero, output reg carry_out); reg [8:0] temp; reg [15:0] temp2; always #(A, B, acode) begin case(is_shift) 1'b0: begin case(acode) 3'b000: temp = A + B; 3'b010: temp = A - B; endcase R = temp[7:0]; carry_out = temp[8]; Given A = 11100101 and B = 11000111, here is the log: //addition A: 11100101 , B: 11000111 acode: 000 R: 10101100 zero: 0, carry_out: 1 //subtraction A: 11100101 , B: 11000111 acode: 010 R: 00011110 zero: 0, carry_out: 0 In both cases, the 9th bit of temp should be 1 and it's right in the addition case but in the subtraction case, the subtraction is right but the 9th bit of temp is not set to 1. what is the problem here? By the way: The effect of declaration of a register as signed is only in shifting and extending, yes? So this problem is not because of A being signed and B being unsigned , right?
The effect of declaration of a register as signed is only in shifting and extending No, it effects all arithmetic. Although usually if you combine any unsigned or part select bus then it will default back to unsigned arithmetic. You can not really have one input signed and one not, twos complement arithmetic will simply not work. You at least have to sign extend the signed value and insert a 0 MSB on to the unsigned, making sure it will be evaluated as positive. Your first example is: 1110 0101 // -27 1100 0111 // -57 1 1010 1100 // -84 (-27 -57) Second example (subtraction) 1110 0101 // -27 0011 1001 // +57 1 0001 1110 // 30 (ignoring MSB) -226 Including MSB But note that the output is 1 bit wider, RTL does not give you access to the carry, but rather an extra sum, therefore the inputs are sign extended. 1 1110 0101 // -27 1 1100 0111 // -57 1 1010 1100 // -84 1 1110 0101 // -27 0 0011 1001 // +57 0 0001 1110 // 30 Note in the correctly sign extended subtraction the MSB is 0 But for your addition with the second value unsigned you need a 0 to show it is a positive number, and you will have bit growth of 1 bit: 1 1 1110 0101 // -27 0 0 1100 0111 // 199 0 0 1010 1100 // 172 (-27+199) Here the extended bit (not a carry) is 0. not 1 as you predicted.