Simplify combinational logic using yosys - yosys

I am wondering if it is possible to use Yosys in simplifying logic equations.
For example:
module top
(
output [31:0] cipher,
input [31:0] plain,
input [63:0] key
);
wire tmp = key[31:0];
wire tmp2 = key[63:32] & 0;
assign cipher = (tmp & plain) | tmp2;
endmodule
When I use the command "show" it plots the circuit:
I tried using "opt" and "freduce" commands but it did not reduce the equation.

You probably want to use opt -fine which does finer-grain optimisations rather than optimising whole words at a time. This gives a single 1-bit $and gate as expected.
Alternatively techmap; abc will produce an optimised gate-level circuit.

Related

Verilog HDL syntax error at practice.v(7) near text "or"; expecting ")"

I have changed the setting to the same name as the module. There are no other Verilog files in the folder where this file is stored. I don't know what is wrong with the grammar.
module test(A,B,F);
input A;
input [1:0]B;
output F;
reg F;
always #(*)
if({A,B}==3'b001 or {A,B}==3'b010 or {A,B}==3'b100 or {A,B}==3'b101)
F=1;
else F=0;
endmodule
or is a legal keyword in Verilog, but it can not be used in that context. It can be used as a built-in primitive OR gate, or in a sensitivity list.
You likely want it to behave as the logical OR operator, in which case you would use || instead:
if({A,B}==3'b001 || {A,B}==3'b010 || {A,B}==3'b100 || {A,B}==3'b101)
Alternately, you could enable SystemVerilog features in your tools and use the set membership inside operator:
module test(A,B,F);
input A;
input [1:0]B;
output F;
assign F = ({A,B} inside {3'b001, 3'b010, 3'b100, 3'b101});
endmodule
This code is simpler to understand, and it scales better since it is easier to add/remove a comparison value.
The logical OR operator in Verilog is ||, not or.

How can I sum two numbers in Latex with my own command?

I have to sum two numbers (integers) in LaTeX. I also have to "print" the process of sum. So it would look like 5+2=7 in text. Any ideas?
My code so far:
\newcommand{\Sum}
{\newcounter{cnt}
\setcountter{cnt}{1+1}
}
In LaTeX, first you have to define a counter with:
\newcounter{countername}
Then you can put a value in this counter with:
\setcounter{countername}{<value>}
where <value> is an integer. Or you can add one to this counter with:
\stepcounter{countername}
or you can add some arbitrary value to this counter with:
\addtocounter{countername}{<value>}
Then, to access this counter you use:
\value{countername}
so you can, for example, make calculations with this counter:
\setcounter{anothercounter}{%
\numexpr\value{countername}+10\relax%
}
Then, when you need to print the value of this counter to the pdf file, you can you the mighty \the:
\the\value{countername}
or you can use one of these:
\arabic{countername}
\roman{countername}
\Roman{countername}
\alph{countername}
\Alph{countername}
Perhaps a different syntax can be in order; instead of supplying each argument to calculate a sum, you can supply the operator with the operands. This allows you to be a little more flexible in terms of the input and also provide more functionality:
\documentclass{article}
\usepackage{xfp}
\NewDocumentCommand{\showcalculation}{o m}{$
\IfValueTF{#1}
{#1}{#2} = \fpeval{#2}
$}
\begin{document}
\showcalculation{7+3}
\showcalculation{1+2-3+4}
\showcalculation[22 \div 7]{22 / 7}
\showcalculation[10 \times 3^{7 - 7}]{10 * 3 ^ (7 - 7)}
\end{document}
The optional argument for \showcalculation uses a LaTeX formatting for the printable calculation.
I've managed to solve the problem.
\newcommand{\Sum} [2] {#1 + #2 = \the\numexpr #1 + #2 \relax \\}
And then I use my command as:
\Sum {7} {3}

I cant force a value into the "thereg" registry file

module myRegister
(input clk,
input [3:0] write,
input [3:0] read1,
input [3:0] read2,
input [3:0]writedata);
reg[3:0]thereg[7:0];
reg [3:0]readdata1;
reg [3:0]readdata2;
always #(posedge clk) begin
readdata1=thereg[read1];
readdata2=thereg[read2];
end
always #(negedge clk) begin
thereg[write]=writedata;
end
endmodule
I need to force values (1s and 0s) into the "thereg" registry file and read from it twice on the positive edge from the clock and write from it once on the negative edge of the clock. I am unable to force any values however. I click force and nothing happens, however, I am able to force values into everything else. Any ideas?
In testbench, you will get access only to the input/output ports, but not to the wire/reg. Better way for your requirement is to force the values to input port writedata at each negative edges and you will get the same data at successive positive clock edges on the output ports readdata1 & readdata2.

Write data to file in columns (Fortran)

I need to write some data to file in Fortran 90. How should I use WRITE (*,*) input to have the values grouped in columns? WRITE always puts a new line after each call, that's the problem.
code example:
open (unit = 4, file = 'generated_trajectories1.dat', form='formatted')
do time_nr=0, N
write (4,*) dble(time_nr)*dt, initial_traj(time_nr)
end do
And now the point is to have it written in separate columns.
You can use implied DO loops to write values as single records. Compare the following two examples:
integer :: i
do i=1,10
write(*,'(2I4)') i, 2*i
end do
It produces:
1 2
2 4
3 6
...
Using implied DO loops it can rewritten as:
integer :: i
write(*, '(10(2I4))') (i, 2*i, i=1,10)
This one produces:
1 2 2 4 3 6 ...
If the number of elements is not fixed at compile time, you can either use the <n> extension (not supported by gfortran):
write(*, '(<n>(2I4))') (i, 2*i, i=1,n)
It takes the number of repetitions of the (2I4) edit descriptor from the value of the variable n. In GNU Fortran you can first create the appropriate edit descriptor using internal files:
character(len=20) :: myfmt
write(myfmt, '("(",I0,"(2I4))")') n
write(*, fmt=myfmt) (i, 2*i, i=1,n)
Of course, it also works with list directed output (that is output with format of *):
write(*, *) (i, 2*i, i=1,10)
This really depends on what data you are trying to write to file (i.e. whether you have a scalar within a loop or an array...). Can you include a description of this in your question?
If your are trying to write a scalar multiple times to the same row then try using non-advancing I/O, passing the keyword argument advance="no" to the write statement, e.g.
integer :: x
do x = 1,10
write(*, fmt='(i4,1x)', advance="no") x
end do
However, be aware of a suprise with non-advancing I/O.
The answer depends on your answer to Chris's question. If you want a single line, then you will have to use non-advancing IO as described by Chris. Without this, with multiple formatted write statement you will always get multiple lines.
Also, you will likely need to use formatted IO instead of list-directed (*) IO. The rules are loose for list-directed IO. Different compilers may produce different output. With many output items, line breaks are likely to keep the output lines from being too long.
Here a format that should work if all of your variables are reals:
write (4, '( *(2X, ES14.6) )', advance="no" )
how about the good old $ edit descriptor:
write(*, fmt='(i4,$)') x
remember to do a write(*,*) after your loop...

Bitwise Arithmetic and Operators

Currently studying bitwise arithmetic. It's really easy, because I have some CS background. But I just don't understand one moment with this operator.
For example:
variable3 = variableOne & 3;
or
variable3 &= 3;
Actually this doesn't matter.
I don't understand how the process of setting the bits to 0 is going on. And how you can process it on the paper?
Let’s say 5&3, four-bit width:
0101b = 5dec
0011b = 3dec
------------
0001b = 1dec
You just & the bits in the same column. And since the & operator only returns 1 when both arguments are 1, the higher bits from 5 not present in 3 are masked out.
As for your example from the comments:
$ perl -E 'printf "%b\n", 0x76'
1110110
And now:
1110110 = 0x76
0000011 = 3dec
-------
0000010 = 2dec
…and just to validate:
$ perl -E 'say 0x76&3'
2
The schema is simple, you just & each column:
x
y
-
z
Where z is x&y.
Aha, judging by your comments in the neighbouring answer the problem is elsewhere. Numeric variables do not contain “hexadecimal values” in them. Numeric variables contain a bit pattern representing a number. “A number” is never binary, decimal or hexadecimal. When you say “three”, there’s no number system in play, three is a three no matter what.
When you say something like var x = 0x76 in the source code, the machine reads the hexadecimal representation of the number, creates a bit pattern representing this number and stores it in the memory corresponding to the variable. And when you then say something like x &= 3, the machine creates a bit pattern representing number three, combines that with the bit pattern stored in the variable and stores the result in the variable.