C++ Coding Explaining & reviewing
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void sm(int scl, int sda);
enum states {
s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16
};
struct i2c {
int scl;
int sda;
int item;
};
struct i2c traces[1000];
int trace_length;
int main()
{
int item, scl, sda, index;
string trace0;
ifstream myfile; /* Naming the file to open*/
string myline;
char read_write_mode = 'R';
bool ack = false;
int read_count = 0;
int write_count = 0;
int ack_count = 0;
int nack_count = 0;
trace0 = "t0.txt"; /* Choosing the file trace*/
myfile.open(trace0.c_str()); /* Open file c_str is for the first line*/
getline(myfile, myline); /* First line*/
index = 0;
while (!myfile.eof()) /* Assigning the clock and data from file to code*/
{
myfile >> item >> scl >> sda;
traces[index].item = item;
traces[index].scl = scl;
traces[index].sda = sda;
index++;
}
trace_length = index; /* to count traces*/
myfile.close();
for (index = 0; index < trace_length; index++)
{
sm(traces[index].scl, traces[index].sda);
}
if ((scl == 0 && sda == 0) && (scl == 1 && sda == 0) && (scl == 0 && sda == 0))
{
ack_count++;
}
else
nack_count++;
cout << "ack = " << ack_count << endl;
cout << "nack = " << nack_count << endl;
system("pause");
}
void sm(int scl, int sda)
{
static int ps = 0;
static int ns = 0;
static int address;
static int r_w;
static int temp;
static int k;
static int index;
static bool ack;
static bool nack;
ps = ns;
cout << "ps " << ps << endl;
cout << "scl " << scl << " " << "sda " << sda << endl;
cout << "Address = " << temp << endl;
switch (ps)
{
case s0: if ((scl == 1) && (sda == 0)) /* IDLE*/
ns = s1;
else
ns = s0;
break;
case s1: if (scl == 1) /* START*/
ns = s1;
else
ns = s2;
k = 0;
temp = 0;
break;
case s2: if (scl == 0)
ns = s2;
else
ns = s3;
break;
case s3: if (scl == 1)
ns = s3;
else
ns = s4;
temp = temp << 1;
temp = temp + sda;
k = k + 1;
break;
case s4: if (k != 8)
ns = s2;
else {
ns = s5;
address = temp >> 1;
r_w = temp & 1;
cout << "address = " << hex << address << endl;
}
break;
case s5: if (scl == 0)
ns = s5;
else
ns = s6;
break;
case s6: if (scl == 1) /*ACK = 0, NACK = 1 */
ns = s6;
else
ns = s7;
break;
case s7: if (ack == false)
ns = s0;
else
ns = s8;
break;
case s8: if (scl == 0)
ns = s8;
else
ns = s9;
break;
case s9: if (scl == 1)
ns = s9;
else
ns = s10;
break;
case s10: if (k != 8)
ns = s8;
else
ns = s11;
break;
case s11: if (scl == 0)
ns = s11;
else
ns = s12;
break;
case s12: if (scl == 1)
ns = s12;
else
ns = s13;
break;
case s13: if (ack == false)
ns = s0;
else
ns = s14;
break;
case s14: if (scl == 0 && sda == 0)
ns = s15;
else
ns = s14;
break;
case s15: if (scl == 0 && sda == 0)
ns = s15;
else
ns = s16;
break;
case s16: if (scl == 1 && sda == 0)
ns = s16;
else
ns = s0;
break;
}
}
8 years ago 15
- refliction paper
- Human Resource: Exceptional Proff
- week 4
- Case study one You have been hired as a financial consultant to Tamar Circuitry Ltd (TCL), a large publicly traded firm...
- 2 pages
- NO PLAGIARISM PLEASE, PAYING $15.00 DUE 09/20/16 BY 6:00pm
- Explore Hacking Websites or Company Investigation
- who can do this in an hour?
- Assignment 2: Lending Institutions, Health Care, and Human Capital
- Case Study