Creating a 4 State Mealy Finite State Machine using Verilog [HELP] - state

I'm trying to figure out how to get this to run without getting any errors.
module main;
(
input wire clk, reset;
input wire x, y;
output reg n, c;
s0 = 0,
s1 = 1,
s2 = 2,
s3 = 3;
state_reg;
state_next;
);
always(posedge clk, posedge reset)
begin
if (reset) begin
state_reg = s0;
end
else begin
state_reg = state_next;
end
end
always (x, y, state_reg) begin
state_next = state_reg;
n = 0;
c = 0;
case (state_reg)
s0 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s1;
end
end
s1 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s2;
end
end
s2 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
s3 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 1;
state_next = s3;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
endcase
endmodule
I feel like the code enough should show what I'm trying to do but in case it doesn't I've also attached an image of the logic schematic version (I'm not sure how to attach the .cct file of the schematic on this website due to me being new). Sorry guys this is somewhat my first time using Verilog so I'm really new to this but I have to write this for an honors project with zero guidance. So the output should be if I input x or y with either 0 or 1, it should switch to a different state depending on the conditions (s0, s1, s2, s3) etc. If I hit reset, it should go back to s0, if I hit clk or the clock variable, it should take the inputs x and y to decide what the next state should be.
EDIT: Here are the errors I'm getting.
jdoodle.v:2: syntax error
jdoodle.v:3: error: invalid module item.
jdoodle.v:6: syntax error
jdoodle.v:6: error: Invalid module instantiation
jdoodle.v:11: error: Invalid module instantiation
jdoodle.v:12: error: Invalid module instantiation
jdoodle.v:13: error: invalid module item.
jdoodle.v:14: syntax error
jdoodle.v:17: Syntax in assignment statement l-value.
jdoodle.v:18: syntax error
jdoodle.v:20: error: invalid module item.
jdoodle.v:21: syntax error
jdoodle.v:25: error: invalid module item.
jdoodle.v:26: syntax error
jdoodle.v:26: error: Invalid module instantiation
jdoodle.v:27: error: Invalid module instantiation
jdoodle.v:31: syntax error
jdoodle.v:31: error: Invalid module instantiation
jdoodle.v:32: error: Invalid module instantiation
jdoodle.v:33: error: Invalid module instantiation
jdoodle.v:36: syntax error
jdoodle.v:36: error: Invalid module instantiation
jdoodle.v:37: error: Invalid module instantiation
jdoodle.v:38: error: Invalid module instantiation
jdoodle.v:41: syntax error
jdoodle.v:41: error: Invalid module instantiation
jdoodle.v:42: error: Invalid module instantiation
jdoodle.v:43: error: Invalid module instantiation
jdoodle.v:46: syntax error
jdoodle.v:46: error: Invalid module instantiation
jdoodle.v:47: error: Invalid module instantiation
jdoodle.v:48: error: Invalid module instantiation
jdoodle.v:53: syntax error
jdoodle.v:53: error: Invalid module instantiation
jdoodle.v:54: error: Invalid module instantiation
jdoodle.v:55: error: Invalid module instantiation
jdoodle.v:58: syntax error
jdoodle.v:58: error: Invalid module instantiation
jdoodle.v:59: error: Invalid module instantiation
jdoodle.v:60: error: Invalid module instantiation
jdoodle.v:63: syntax error
jdoodle.v:63: error: Invalid module instantiation
jdoodle.v:64: error: Invalid module instantiation
jdoodle.v:65: error: Invalid module instantiation
jdoodle.v:68: syntax error
jdoodle.v:68: error: Invalid module instantiation
jdoodle.v:69: error: Invalid module instantiation
jdoodle.v:70: error: Invalid module instantiation
jdoodle.v:75: syntax error
jdoodle.v:75: error: Invalid module instantiation
jdoodle.v:76: error: Invalid module instantiation
jdoodle.v:77: error: Invalid module instantiation
jdoodle.v:80: syntax error
jdoodle.v:80: error: Invalid module instantiation
jdoodle.v:81: error: Invalid module instantiation
jdoodle.v:82: error: Invalid module instantiation
jdoodle.v:85: syntax error
jdoodle.v:85: error: Invalid module instantiation
jdoodle.v:86: error: Invalid module instantiation
jdoodle.v:87: error: Invalid module instantiation
jdoodle.v:90: syntax error
jdoodle.v:90: error: Invalid module instantiation
jdoodle.v:91: error: Invalid module instantiation
jdoodle.v:92: error: Invalid module instantiation
jdoodle.v:97: syntax error
jdoodle.v:97: error: Invalid module instantiation
jdoodle.v:98: error: Invalid module instantiation
jdoodle.v:99: error: Invalid module instantiation
jdoodle.v:102: syntax error
jdoodle.v:102: error: Invalid module instantiation
jdoodle.v:103: error: Invalid module instantiation
jdoodle.v:104: error: Invalid module instantiation
jdoodle.v:107: syntax error
jdoodle.v:107: error: Invalid module instantiation
jdoodle.v:108: error: Invalid module instantiation
jdoodle.v:109: error: Invalid module instantiation
jdoodle.v:112: syntax error
jdoodle.v:112: error: Invalid module instantiation
jdoodle.v:113: error: Invalid module instantiation
jdoodle.v:114: error: Invalid module instantiation

There are several syntax errors preventing this module from compiling.
The semicolon after the module name 'main' is a syntax error.
Semicolons in the IO list (after the module name are errors, need to be commas.
You are tying to declare a state variables is the IO list area (just after the word module . Its an error. Put the declarations after IO listing.
Search 'verilog state machine' to learn how and where to declare the state variables.
You need non-blocking assignments (<=) rather than non blocking assignments (=) in the always block with the clock.
Your always blocks were missing the timing control operator #.
You were missing a 'end' keyword at the end of the module.
The state machine does not have a :default state, which could cause some strange behavior.
I made these changes (did not add a :default state because it will modify your design intent).
Here is your code that now compiles using Mentor Questa on edaplayground.com
You did not post a testbench, there is no way to know if it behaves the way you want it to or not.
Search on 'basic verilog testbench' to get started on a testbench design.
module main
(
input wire clk,
input wire reset,
input wire x,
input wire y,
output reg n,
output reg c
);
localparam
s0 = 2'b00,
s1 = 2'b01,
s2 = 2'b10,
s3 = 2'b11;
reg [1:0] state_reg,state_next;
always #(posedge clk, posedge reset)
begin
if (reset) begin
state_reg <= s0;
end
else begin
state_reg <= state_next;
end
end
always #(x, y, state_reg) begin
state_next = state_reg;
n = 0;
c = 0;
case (state_reg)
s0 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s1;
end
end
s1 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 0;
state_next = s2;
end
end
s2 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
s3 : begin
if (x == 0 && y == 0) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x == 0 && y == 1) begin
n = 0;
c = 0;
state_next = s0;
end
else if (x== 1 && y == 0) begin
n = 0;
c = 1;
state_next = s3;
end
else if (x== 1 && y == 1) begin
n = 1;
c = 1;
state_next = s3;
end
end
endcase
end
endmodule

Related

Why doesn't g++ recognize stoi even using C++11?

I have this function in C++11:
bool ccc(const string cc) {
vector<string> digits;
int aux;
for(int n = 0; n < cc.length(); ++n) {
digits.push_back(to_string(cc[n])); }
for(int s = 1; s < digits.size(); s += 2) {
aux = stoi(digits[s]);
aux *= 2;
digits[s] = to_string(aux);
aux = 0;
for(int f = 0; f < digits[s].length(); ++f) {
aux += stoi(digits[s][f]); }
digits[s] = to_string(aux);
aux = 0; }
for(int b = 0; b < digits.size(); ++b) {
aux += stoi(digits[b]); }
aux *= 9;
aux %= 10;
return (aux == 0); }
And I get this error when compiling with g++ with the -std=c++11 flag:
crecarche.cpp: In function ‘bool ccc(std::string)’:
crecarche.cpp:18:12: error: no matching function for call to ‘stoi(__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&)’
18 | aux += stoi(digits[s][f]); }
| ~~~~^~~~~~~~~~~~~~
But I used the stoi function after and I did not get any error with that line.
Why is the compiler throwing me this error and how can I fix it?
The error message is telling you that the argument you pass to stoi is of the type
__gnu_cxx::__alloc_traits<std::allocator<char>, char>::value_type&
which is a fancy way of saying char&. This happens because digits[s] is already of type string&, and subscribing it further gives you a char&.
It's not clear to me what you are trying to accomplish. Maybe you need to remove the extra subscript, or use digits[s][f] - '0' to compute the digit value. C++ requires that the decimal digits are represented by subsequent code points, so this works even in theoretical implementations which are not based on the ISO 646 subset of Unicode.

Can't write into a file using fstream - I can't seem to get why

I need to create a program using graph theory which takes as an input multiple undirected graphs and writes out either YES if it is a complete graph or NO if it is not a complete graph.
E.g.
for:
2 //number of graps
3 //number of lines of the first graph's matrix
0 1 1 //the first graph's matrix
1 0 1
1 1 0
4 //number of lines of the second graph's matrix
0 1 1 1 //the second graph's matrix
1 0 0 1
1 0 0 1
1 1 1 0
It should output:
YES
NO
I solved it this way:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("graf_complet.in");
ofstream g("graf_complet.out");
int verif(int matrice[][100], int t)
{
int c = 0;
bool ok = 1;
for (int i = 1; i <= t; i++)
{
c = 0;
for (int j = 1; j <= t; j++)
{
if (matrice[i][j] == 1)
{
c++;
}
}
if (c != (t - 1))
{
ok = 0;
break;
}
}
if (ok == 1)
{
return 1;
}
return 0;
}
int main()
{
int g, c = 0, t, matrice[100][100];
f >> g;
while (c != g)
{
f >> t;
for (int i = 1; i <= t; i++)
{
for (int j = 1; j <= t; j++)
{
f >> matrice[i][j];
}
}
if (verif(matrice, t))
{
g << "YES";
}
else
{
g << "NO";
}
c++;
}
}
Using cout instead of writing into files works, and I seem to get the proper results. When I try to write into the file with g<< (as I'm supposed to) I get:
Severity Code Description Project File Line Suppression State
Error C2297 '<<': illegal, right operand has type 'const char [3]' ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 52
Error C2297 '<<': illegal, right operand has type 'const char [3]' ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 56
Error (active) E2140 expression must have integral or unscoped enum type ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 52
Error (active) E2140 expression must have integral or unscoped enum type ConsoleApplication1 C:\Users\x\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 56
I can't get why this happens tbh. It is not the first time I write into files this way and I've never encountered this. I think it might be something about the fact that the file which I'm taking the input from (so basically f>>) is not closed, it is still open.
Is that the reason?
If so, how can I still make the program work? If not, what is the issue causing the error?
Thanks.

Conditional operation with += in C++

What does these following statement do?
The output from gcc is: 1 101.
Can anyone explain why so is the case?
int a = 100, b = 108, c = 2;
a += b ? c = 1 : c = 0;
cout << c << " " << a;
It's equivalent to:
a += (b ? (c=1) : (c=0));
Expanding out:
a = a + (b ? (c=1) : (c = 0)); // if (b!=0) then let c=1, else let c=0
Filling in for a and b:
a = 100 + (108 ? (c=1) : (c = 0));
Since 108 is not a "false" (0) expression, then the assignment of c=1 is executed. A side effect of an assignment is that the assignment is equal to that assigned value. And the result of that assignment is added to a:
a = 100 + (c=1);
Which is equivalent to:
t = (c = 1); // t is 1
a = 100 + t;
And thus, since t is 1:
a = 100 + 1;
Add parenthesis (and space) so you can see what is happening:
int a = 100,b=108,c=2;
a += ((b != 0) ? c=1 : c=0);
cout<<c<<" "<<a;
Now replace with words (no longer legal C/C++):
int a = 100,b=108,c=2;
a += (if b != 0 then result=c=1 else result=c=0);
cout<<c<<" "<<a;
Then separate:
if b != 0 then result=c=1 else result=c=0
a += result
Since b != 0:
result = c = 1
a += result
Lets expand
a = a + (b ? c = 1 : c = 0);
since b = 108 which evaluates to true, the expression c = 1 is evaluated.
Now, the assignment c = 1 is successfull, so that returns 1, and the expression becomes
a = a + 1
Therefore, in the end, the values are c = 1 and a = 101
int a = 100, b = 108, c = 2;
a += b ? c = 1 : c = 0;
this is equivalent to
a = a + (b ? c = 1 : c = 0);
This (?:) is ternary condition operator which work like if-else condition.
if condition is true it will run first otherwise second.
(b ? c = 1 : c = 0;)
if true ^^^^^
if false ^^^^^^
as b=108 which will give TRUE, so it will make c=1 and return c;
now
a = a + 1;
this will make a = 101
? is the ternary operator. It's logically equivalent to the following:
int a = 100,b=108,c=2;
if(b) {
c = 1;
} else {
c = 0;
}
a+=c;
// c is 1 because b is true (nonzero)
// a is a+c = 101

Passing a 2D array through a function and I get an error

I am making a recursive algorithm as part of an extra credit project for class to find the shortest path through a 2D int array maze of 0's and 1's. 0 represents a wall and 1 represents a hallway where you can pass through. I think I have everything perfect but it just won't compile. It says I am trying to convert something from int to array or whatever. Here is my code, please help.
#include <iostream>
using namespace std;
#define 20 SIZEX;
#define 5 SIZEY;
int value; //to compare paths to take
int starti = 1;
int startj = 0;
int newi;
int newj;
int counter = 0; //keeps track of how many steps taken
void pathfinder(int a[][SIZEX], int currenti, int currentj);
int arr[SIZEY][SIZEX] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,
0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int main()
{
pathfinder(arr, starti, startj);
system("PAUSE");
return 0;
}
void pathfinder(int a[][SIZEX], int currenti, int currentj)
{
//as soon as it walks somewhere, the value of that spot increments
int temp;
temp = a[currenti][currentj];
temp++;
a[currenti][currentj] = temp;
if (counter == 0) //keeps track of starting point
{
starti = currenti;
startj = currentj;
}
if (currenti-1 >= 0 && a[currenti-1][currentj] != 0) //checks up
{
value = a[currenti-1][currentj];
}
else if (currenti+1 < 5 && a[currenti+1][currentj] != 0) //checks down
{
value = a[currenti+1][currentj];
}
else if (currentj-1 >= 0 && a[currenti][currentj-1] != 0) //checks left
{
value = a[currenti][currentj-1];
}
else if (currentj+1 < 20 && a[currenti][currentj+1] != 0) //checks right
{
value = a[currenti][currentj+1];
}
//value has a value, now check it against all possible values for the least travelled path
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value > a[currenti-1][currentj])
{
value = a[currenti-1][currentj];
}
if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value > a[currenti+1][currentj])
{
value = a[currenti+1][currentj];
}
if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value > a[currenti][currentj-1])
{
value = a[currenti][currentj-1];
}
if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value > a[currenti][currentj+1])
{
value = a[currenti][currentj+1];
}
//value now holds the smallest possible value among the four possible paths
if ((currenti-1 >= 0 && a[currenti-1][currentj] !=0) && value == a[currenti-1][currentj]) //move up
{
newi = currenti-1;
newj = currentj;
counter++;
}
else if ((currenti+1 < 5 && a[currenti+1][currentj] !=0) && value == a[currenti+1][currentj]) //move down
{
newi = currenti+1;
newj = currentj;
counter++;
}
else if ((currentj-1 >= 0 && a[currenti][currentj-1] != 0) && value == a[currenti][currentj-1]) //move left
{
newi = currenti;
newj = currentj-1;
counter++;
}
else if ((currentj+1 < 20 && a[currenti][currentj+1] != 0) && value == a[currenti][currentj+1]) //move right
{
newi = currenti;
newj = currentj+1;
counter++;
}
//upon reaching the exit, it will print out a new 2d maze, and the path with the smallest value of non-zero integers is the shortest path
if ((currenti == 0 || currentj == 0 || currenti == 4 || currentj == 19) && (currenti != starti || currentj !=startj))
{
for (int i = 0; i < 5; i++)
{
for (int j = 0;j < 20;j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
return;
}
pathfinder(arr, newi, newj);
}
1>------ Build started: Project: Project5, Configuration: Debug Win32
------ 1> Source.cpp 1>c:\users\justin\documents\visual studio 2012\projects\project5\project5\source.cpp(22): error C2664:
'pathfinder' : cannot convert parameter 1 from 'int' to 'int [][20]'
1> Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
1>c:\users\justin\documents\visual studio
2012\projects\project5\project5\source.cpp(114): error C2664:
'pathfinder' : cannot convert parameter 1 from 'int' to 'int [][20]'
1> Conversion from integral type to pointer type requires
reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
These are incorrect:
#define 20 SIZEX;
#define 5 SIZEY;
If you really want macros, the correct way to define them is:
#define SIZEX 20
#define SIZEY 5
But, as this is C++, you should make use of const int here:
const int SIZEX = 20;
const int SIZEY = 5;
That should fix your compile problem.
On a separate note: If you're going to pass arr as your initial argument to pathfinder() to start the recursion in main, you might change your recursive call to pass the argument pathfinder() was passed. ie:
pathfinder(a, newi, newj);
Otherwise, even if main were to pass a different array to pathfinder, it would still do most of its work on arr, rendering the parameter useless.

Writing code for power module in c++, receiving several errors

I'm writing code that doesn't use pow() in c++, but I am getting quite a few errors which I can't figure out:
double power (double X, unsigned int N)
{
double value;
unsigned int i = 1;
for (i = 1, i <= N, i++)
{
result = result * X;
}
if (finite(result))
{
return result;
}
else
{
return INFINITY;
}
}
Errors:
In function 'double power(double, unsigned int)':
Line 5: warning: right-hand operand of comma has no effect
Line 5: error: expected ';' before ')' token
Line 10: error: expected primary-expression before 'if'
Line 10: error: expected ';' before 'if'
Line 10: error: expected primary-expression before 'if'
Line 10: error: expected ')' before 'if'
Any help would be appreciated, thanks.
It should be for (i = 1; i <= N; i++).
In C++, the semicolon is usd to delimit the different parts of the for loop.
for (i = 1, i <= N, i++) => for (i = 1; i <= N; i++)
Get a good C book