Sunteți pe pagina 1din 10

`timescale 1ns / 1ps

//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date: 19:18:40 04/05/2017
// Design Name:
// Module Name: alwas
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module alwas(clk_out, disp, bt1, bt2, bt3, bt4, an0,an1,an2,an3,
cg,cf,ce,cd,cc,cb,ca
);
input clk_out; //clocku pe care merge, deja modificat yeeee
input [7:0] disp; //inputul primit din switch-uri
input bt1, bt2, bt3, bt4; //butoanieeee
output reg an0,an1,an2,an3, cg,cf,ce,cd,cc,cb,ca; //anoade catoade
reg [6:0] first, second, third, fourth; // reprezentarea cifrelor in format
reg [7:0] num1, num2; // stocam inputurile pentru cele 2 numere
reg [1:0] pos; // iterator pe anoade
reg [8:0] sum; // stocam suma, un bit in plus in caz de carry la suma
reg [7:0] ones, complement; //ones-plin de 1uri, complement e... complement
you nailed it
reg carry; // carry pentru o alta metoda la zcb

//sa nu dea vreo eroare ???


initial begin
first = 7'b0000000;
second = 7'b0000000;
third = 7'b0000000;
fourth = 7'b0000000;
pos = 2'b00;
sum = 9'b000000000;
ones = 8'b11111111;
end

//un always pe clock


always @(posedge clk_out) begin
//pentru fiecare buton e un caz
//primu buton, luam primul numar si il retinem pentru mai tarziu (il si
afisam duh)
if(bt1) begin
//mergem pe anoade folosind un registru de 2 biti care se
incrementeaza la fiecare clock
pos = pos+1;
case(pos)
2'b00:
begin
an0 = 0;
an1 = 1;
an2 = 1;
an3 = 1;
case(disp[3:0]) // primele 4 comutatoare
determina prima cifra
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
num1[3:0] = disp[3:0];
first[0] = ca;
first[1] = cb;
first[2] = cc;
first[3] = cd;
first[4] = ce;
first[5] = cf;
first[6] = cg;
end
2'b01:
begin
an0 = 1;
an1 = 0;
an2 = 1;
an3 = 1;
case(disp[7:4])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
num1[7:4] = disp[7:4];
second[0] = ca;
second[1] = cb;
second[2] = cc;
second[3] = cd;
second[4] = ce;
second[5] = cf;
second[6] = cg;
end
2'b10: // celelalte 2 7-segmente nu au nicio treaba
begin
an0 = 1;
an1 = 1;
an2 = 0;
an3 = 1;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
2'b11:
begin
an0 = 1;
an1 = 1;
an2 = 1;
an3 = 0;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
endcase
end
//la fel ca la butonul 1, doar ca e pt numarul al doilea
else if(bt2) begin
pos = pos+1;
case(pos)
2'b00:
begin
an0 = 0;
an1 = 1;
an2 = 1;
an3 = 1;
ca = first[0];
cb = first[1];
cc = first[2];
cd = first[3];
ce = first[4];
cf = first[5];
cg = first[6];
end // primele 2 butoane raman la fel
2'b01:
begin
an0 = 1;
an1 = 0;
an2 = 1;
an3 = 1;
ca = second[0];
cb = second[1];
cc = second[2];
cd = second[3];
ce = second[4];
cf = second[5];
cg = second[6];
end
2'b10: // se modifica in mod analog celelalte 2 7-segmente
begin
an0 = 1;
an1 = 1;
an2 = 0;
an3 = 1;
case(disp[3:0])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
num2[3:0] = disp[3:0];
third[0] = ca;
third[1] = cb;
third[2] = cc;
third[3] = cd;
third[4] = ce;
third[5] = cf;
third[6] = cg;
end
2'b11:
begin
an0 = 1;
an1 = 1;
an2 = 1;
an3 = 0;
case(disp[7:4])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
num2[7:4] = disp[7:4];
fourth[0] = ca;
fourth[1] = cb;
fourth[2] = cc;
fourth[3] = cd;
fourth[4] = ce;
fourth[5] = cf;
fourth[6] = cg;
end
endcase
end

//zcb
else if (bt3) begin // la butonul 3 ar trebui sa se faca suma ZCB
/*sum[8:0] = num1[7:0] + num2[7:0];
{carry,sum[0]} = num1[0] + num2[0];
{carry,sum[1]} = num1[1] + num2[1] + carry;
{carry,sum[2]} = num1[2] + num2[2] + carry;
{carry,sum[3]} = num1[3] + num2[3] + carry;
if (sum[3:0] >= 4'b1010) begin
sum[3:0] = sum[3:0] + 4'b0110;
end
{carry,sum[4]} = num1[4] + num2[4] + carry;
{carry,sum[5]} = num1[5] + num2[5] + carry;
{carry,sum[6]} = num1[6] + num2[6] + carry;
{carry,sum[7]} = num1[7] + num2[7] + carry;
if (sum[7:4] > 4'b1010) begin
sum[7:4] = sum[7:4] + 4'b0110 + carry;
end
sum[8] = carry;*/

//metoda de insumare pe biti, cred ca asta e cea mai buna,


netestata ca am bagat si corectie
//IT MAY BE THE ONE <3, decomenteaza asta si comenteaza cealalta
metoda de mai jos

/*sum[0] = num1[0] ^ num2[0];


carry = num1[0] && num2[0];
sum[1] = num1[1] ^ num2[1] ^ carry;
carry = ((num1[1] ^ num2[1]) && carry) || (num1[1] && num2[1]);
sum[2] = num1[2] ^ num2[2] ^ carry;
carry = ((num1[2] ^ num2[2]) && carry) || (num1[2] && num2[2]);
sum[3] = num1[3] ^ num2[3] ^ carry;
carry = ((num1[3] ^ num2[3]) && carry) || (num1[3] && num2[3]);
if (carry == 1'b1) begin
sum[3:0] = sum[3:0] + 4'b0110;
end
sum[4] = num1[4] ^ num2[4] ^ carry;
carry = ((num1[4] ^ num2[4]) && carry) || (num1[4] && num2[4]);
sum[5] = num1[5] ^ num2[5] ^ carry;
carry = ((num1[5] ^ num2[5]) && carry) || (num1[5] && num2[5]);
sum[6] = num1[6] ^ num2[6] ^ carry;
carry = ((num1[6] ^ num2[6]) && carry) || (num1[6] && num2[6]);
sum[7] = num1[7] ^ num2[7] ^ carry;
carry = ((num1[7] ^ num2[7]) && carry) || (num1[7] && num2[7]);
if (carry == 1'b1) begin
sum[7:4] = sum[7:4] + 4'b0110;
end
sum[8] = carry;*/

//metoda luata din primu lab, ma doare

sum[3:0] = num1[3:0] + num2[3:0];


if (sum[3:0] >= 4'b1010) begin // daca depasesc reprezentarea
posibila ZCB
sum[3:0] = sum[3:0] + 4'b0110;
sum[7:4] = num1[7:4] + num2[7:4];
if (sum[7:4] >= 4'b1010 - 1) begin // daca adunand
transportul anterior as depasi reprezentarea ZCB reiau procedeul
sum[7:4] = sum[7:4] + 4'b0110+ 4'b0001; //
transportul anterior
sum[8] = 1'b1; // un nou transport
end
else begin
sum[7:4] = sum[7:4] + 4'b0001; // adaug doar
transportul anterior, nu se mai genereaza un alt transport la a2-a cifra ZCB
sum[8] = 1'b0;
end
end
else begin
sum[7:4] = num1[7:4] + num2[7:4];
if (sum[7:4] >= 4'b1010) begin
sum[7:4] = sum[7:4] + 4'b0110;
sum[8] = 1'b1;
end
else begin
sum[8] = 1'b0;
end
end
//metoda pe if-uri pentru a lua orice caz, are hibe, nu stiu de
ce

pos = pos+1; // dupa 3 transportul se pierde si voi ajunge la 0


din nou
case(pos)
2'b00:
begin
an0 = 0;
an1 = 1;
an2 = 1;
an3 = 1;
case(sum[3:0])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
end
2'b01:
begin
an0 = 1;
an1 = 0;
an2 = 1;
an3 = 1;
case(sum[7:4])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
end
2'b10:
begin
an0 = 1;
an1 = 1;
an2 = 0;
an3 = 1;
case(sum[8])
1'b1 : {cg,cf,ce,cd,cc,cb,ca} =
7'b1111001;
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
end
2'b11:
begin
an0 = 1;
an1 = 1;
an2 = 1;
an3 = 0;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
endcase
end
//complementi yee woo hoo fml
else if (bt4) begin
if (num2 > 10) begin
complement[7:0] = 100 - num2[7:0];
end
if (num2 <= 10) begin
complement[7:0] = 10 - num2[7:0];
end
// Am scris 2 variante de complement pentru ca nu suntem siguri care
ar merge - pareri impartite in echipa
//complement[7:0] = ones[7:0] - num2[7:0];
sum[8:0] = num1[7:0] + complement[7:0]; //conform pdful de pe cs
curs
//bitul 9 din sum (adica sum[8]) e carry, nu ne pasa
//probabil e mai multa filosofie pe aici, nu iti trebuie if-uri,
doar algoritmul bun

pos = pos+1;
case(pos)
2'b00:
begin
an0 = 0;
an1 = 1;
an2 = 1;
an3 = 1;
case(sum[3:0])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
end
2'b01:
begin
an0 = 1;
an1 = 0;
an2 = 1;
an3 = 1;
case(sum[7:4])
1 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111001;
2 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0100100;
3 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0110000;
4 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0011001;
5 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0010010;
6 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000010;
7 : {cg,cf,ce,cd,cc,cb,ca} = 7'b1111000;
8 : {cg,cf,ce,cd,cc,cb,ca} = 7'b0000000;
4'b1001 : {cg,cf,ce,cd,cc,cb,ca} =
7'b0010000; // 9
default : {cg,cf,ce,cd,cc,cb,ca} =
7'b1000000;
endcase
end
2'b10:
begin
an0 = 1;
an1 = 1;
an2 = 1;
an3 = 0;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
2'b11:
begin
an0 = 1;
an1 = 1;
an2 = 1;
an3 = 0;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
endcase
end
else begin
pos = pos+1;
case(pos)
2'b00:
begin
an0 = 0;
an1 = 1;
an2 = 1;
an3 = 1;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
2'b01:
begin
an0 = 1;
an1 = 0;
an2 = 1;
an3 = 1;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
2'b10:
begin
an0 = 1;
an1 = 1;
an2 = 0;
an3 = 1;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
2'b11:
begin
an0 = 1;
an1 = 1;
an2 = 1;
an3 = 0;
ca = 1;
cb = 1;
cc = 1;
cd = 1;
ce = 1;
cf = 1;
cg = 1;
end
endcase
end
end
endmodule