---
title: 'CAD & シスコのメモ'
disqus: hackmd
---
CADのメモ & シスコのメモ
===
## Table of Contents
[TOC]
## 論理合成
手順
テストベンチではなくメインの回路をset as topに
synthesisする
Open Synthesized DesignをOK
schematic
生成
## 注意点等
1. 複数の入力が1になった場合はどうするか?
A. 指定されていないのでドントケア(2’bxx)
2. FPGAにおける面積
論理ブロック(スライス)数/LUT数/配線経路など
その他, 乗算器(DSP)などの専用回路もいくつか存在する
**一般的には、case文が回路設計に適していると言われている**
3. FPGAの構成
任意の真理値表を持つ4入力LUT(Look Up Table) と
配線経路の制御によって任意の論理関数を実現している
最近のものでは6入力のLUTを持っていたりする
## 検証
1. ブラックボックステスト
プログラムの中身が分からない状態で行うテストのこと
入出力仕様からテストパターンを選択する
同値分割法や境界値分析など
fulladd
2. ホワイトボックステスト
プログラムの中身が分かっている状態で行うテストのこと
プログラムの構造に基づいてテストパターンを選択する
命令を網羅する(ラインカバレッジ)
分岐を網羅する(ブランチカバレッジ) など
alu...
## 構文
### assign
特定の端子の間にAND回路、OR回路、NOT回路などを配置した状態を記述するための文
### always
記憶回路、すなわちフリップフロップやレジスタの要素が回路の中に含まれる場合はVerilog HDLではassign文の代わりにalways文を使います。
### reg
最後に出力
always文を用いるために必要
記憶機能を持ったフリップフロップ
やレジスタを定義するときに使用される文法
reg文で生成されたレジスタや
出力端子はassign文の左辺には使えなくなります。
reg文で生成されたレジスタや出力端子に何らかの信号を接続して値を記憶する場合には必ずalways文を使用しなければならないというルールがあります。
always文の後に続く”begin”から”end”で囲われた範囲の中でだけ記憶回路への代入が可能です。
### wire
保持してるモジュールに渡す
### dontcare
レジスタには必要
## verilog code
### fulladder
```
module fulladd(A, B, Carryin, Q, Carryout);
input A, B, Carryin;
output Q, Carryout;
assign Q = A^B^Carryin;
assign Carryout = (A&B)|(Carryin&B)|(Carryin&A);
endmodule
```
### Subtractor_ripple
```
module subtractor_ripple(A, B, Q);
input [15:0] A, B;
output [15:0] Q;
wire [15:0] C,SUB;
assign SUB = ~B + 1;
fulladd add0(.A(A[0]), .B(SUB[0]), .Carryin(1'b0), .Q(Q[0]), .Carryout(C[0]));
fulladd add1(.A(A[1]), .B(SUB[1]), .Carryin(C[0]), .Q(Q[1]), .Carryout(C[1]));
fulladd add2(.A(A[2]), .B(SUB[2]), .Carryin(C[1]), .Q(Q[2]), .Carryout(C[2]));
fulladd add3(.A(A[3]), .B(SUB[3]), .Carryin(C[2]), .Q(Q[3]), .Carryout(C[3]));
fulladd add4(.A(A[4]), .B(SUB[4]), .Carryin(C[3]), .Q(Q[4]), .Carryout(C[4]));
fulladd add5(.A(A[5]), .B(SUB[5]), .Carryin(C[4]), .Q(Q[5]), .Carryout(C[5]));
fulladd add6(.A(A[6]), .B(SUB[6]), .Carryin(C[5]), .Q(Q[6]), .Carryout(C[6]));
fulladd add7(.A(A[7]), .B(SUB[7]), .Carryin(C[6]), .Q(Q[7]), .Carryout(C[7]));
fulladd add8(.A(A[8]), .B(SUB[8]), .Carryin(C[7]), .Q(Q[8]), .Carryout(C[8]));
fulladd add9(.A(A[9]), .B(SUB[9]), .Carryin(C[8]), .Q(Q[9]), .Carryout(C[9]));
fulladd add10(.A(A[10]), .B(SUB[10]), .Carryin(C[9]), .Q(Q[10]), .Carryout(C[10]));
fulladd add11(.A(A[11]), .B(SUB[11]), .Carryin(C[10]), .Q(Q[11]), .Carryout(C[11]));
fulladd add12(.A(A[12]), .B(SUB[12]), .Carryin(C[11]), .Q(Q[12]), .Carryout(C[12]));
fulladd add13(.A(A[13]), .B(SUB[13]), .Carryin(C[12]), .Q(Q[13]), .Carryout(C[13]));
fulladd add14(.A(A[14]), .B(SUB[14]), .Carryin(C[13]), .Q(Q[14]), .Carryout(C[14]));
fulladd add15(.A(A[15]), .B(SUB[15]), .Carryin(C[14]), .Q(Q[15]), .Carryout(C[15]));
endmodule
```
### ALU
```
module alu_op44(Ain, Bin,Carryin, op_sel, Carryout, alu_out);
input [7:0] Ain, Bin;
input Carryin;
input [2:0] op_sel;
output Carryout;
output [7:0] alu_out;
reg Carryout;
reg [7:0] alu_out;
always @(Ain or Bin or Carryin or op_sel)
begin
case(op_sel)
3'b000: {Carryout, alu_out} = Ain + Bin;
3'b001: {Carryout, alu_out} = Ain + Bin + Carryin;
3'b010: {Carryout, alu_out} = Ain - Bin;
3'b011: begin
alu_out = Ain * Bin;
Carryout = |((Ain * Bin)>>8);
end
3'b100: alu_out = Ain && Bin;
3'b101: alu_out = Ain || Bin;
3'b110: alu_out = ~Ain;
3'b111: alu_out = Ain ^ Bin;
default: {Carryout, alu_out} = 9'bx_xxxx_xxxx;
endcase
end
endmodule
```
### MUX4 & MUX3
**multiplexer 4**
```
module mux4(Bin, Cin, Din, alu_out, reg_sel4, Aout);
input [7:0] Bin, Cin, Din;
input [7:0] alu_out;
input [1:0] reg_sel4;
output [7:0] Aout;
reg [7:0] Aout;
always@(Bin or Cin or Din or alu_out or reg_sel4)
begin
case(reg_sel4)
2'b00: Aout = alu_out;
2'b01: Aout = Bin;
2'b10: Aout = Cin;
2'b11: Aout = Din;
default: Aout = 8'hxx;
endcase;
end
endmodule
```
**multiplexer 3**
```
module mux3(Ain,Cin,Din,reg_sel3,Bout);
input [7:0] Ain, Cin, Din;
input [1:0] reg_sel3;
output [7:0] Bout;
reg [7:0] Bout;
always @(Ain or Cin or Din or reg_sel3)
begin
case(reg_sel3)
2'b01: Bout = Ain;
2'b10: Bout = Cin;
2'b11: Bout = Din;
default: Bout = 8'hxx;
endcase;
end
endmodule
```
### Decoder
```
module decoder(din, dout);
input [1:0] din;
output [3:0] dout;
reg [3:0]dout;
always@(din)
begin
case(din)
2'b00:dout = 4'b0001;
2'b01:dout = 4'b0010;
2'b10:dout = 4'b0100;
2'b11:dout = 4'b1000;
endcase;
end
endmodule
```
### OP_Decoder
```
module op_decode(op, op_sel, reg_sel4, reg_sel3);
//inputs
input [3:0] op;
//outputs
output [2:0] op_sel;
output [1:0] reg_sel4;
output [1:0] reg_sel3;
//registors
reg [2:0] op_sel;
reg [1:0] reg_sel4;
reg [1:0] reg_sel3;
always @(op or op_sel or reg_sel4 or reg_sel3)
begin
case(op)
4'b0000: begin op_sel=3'b000; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0001: begin op_sel=3'b001; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0010: begin op_sel=3'b010; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0011: begin op_sel=3'b011; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0100: begin op_sel=3'b100; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0101: begin op_sel=3'b101; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0110: begin op_sel=3'b110; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b0111: begin op_sel=3'b111; reg_sel4=2'b00; reg_sel3=2'bxx; end
4'b1000: begin op_sel=3'bxxx; reg_sel4=2'b01; reg_sel3=2'bxx; end
4'b1001: begin op_sel=3'bxxx; reg_sel4=2'b10; reg_sel3=2'bxx; end
4'b1010: begin op_sel=3'bxxx; reg_sel4=2'b11; reg_sel3=2'bxx; end
4'b1011: begin op_sel=3'bxxx; reg_sel4=2'bxx; reg_sel3=2'b01; end
4'b1100: begin op_sel=3'bxxx; reg_sel4=2'bxx; reg_sel3=2'b10; end
4'b1101: begin op_sel=3'bxxx; reg_sel4=2'bxx; reg_sel3=2'b11; end
4'b1110: begin op_sel=3'bxxx; reg_sel4=2'bxx; reg_sel3=2'bxx; end
4'b1111: begin op_sel=3'bxxx; reg_sel4=2'bxx; reg_sel3=2'bxx; end
endcase;
end
endmodule
```
### CPU
```
module cpu_comb(op,Ain,Bin,Cin,Din,Carryin,Aout,Bout,Cout,Dout,Carryout);
//inputs
input [3:0] op;
input [7:0] Ain, Bin, Cin, Din;
input Carryin;
//outputs
output [7:0] Aout, Bout, Cout, Dout;
output Carryout;
//moduleoutput
wire [2:0] op_sel;
wire [1:0] reg_sel4, reg_sel3;
wire [7:0] alu_out;
//cpuoutputs
reg[7:0] Cout, Dout;
mux4 mux4(.Bin(Bin), .Cin(Cin), .Din(Din), .alu_out(alu_out), .reg_sel4(reg_sel4), .Aout(Aout));
mux3 mux3(.Ain(Ain), .Cin(Cin), .Din(Din), .reg_sel3(reg_sel3), .Bout(Bout));
op_decode decode(.op(op), .op_sel(op_sel), .reg_sel4(reg_sel4), .reg_sel3(reg_sel3));
alu_op44 alu(.Ain(Ain), .Bin(Bin), .Carryin(Carryin), .op_sel(op_sel), .Carryout(Carryout), .alu_out(alu_out));
always @(Ain or op)
begin
case(op)
4'b1110: begin Cout=Ain; Dout=8'hxx; end
4'b1111: begin Cout=8'hxx; Dout=Ain; end
default: begin Cout=8'hxx; Dout=8'hxx; end
endcase
end
endmodule
```
### 非同期DFF
```
module DFF(D,reset,clk,Q);
input D, reset, clk;
output Q;
reg Q;
always @(posedge clk or negedge reset)
begin
if(reset == 1'b0)
Q <= 1'b0;
else
Q <= D;
end
endmodule
```
### 非同期8bitレジスタ
```
module register8(din, load, clk, reset, dout);
input [7:0] din;
input load, clk, reset;
output [7:0] dout;
reg [7:0] dout;
wire [7:0] din; //必要
always @(posedge clk or negedge reset)
begin
if(reset==1'b0)
dout <= 8'h00;
else if(load==1'b0)
dout <= dout;
else if(load==1'b1)
dout <= din;
else
dout <= 8'hxx;
end
endmodule
```
### 自販機
```
module vending_machine(clock, reset, coin, toy);
input clock, reset, coin;
output toy;
reg [1:0] state;
reg toy;
always @(posedge clock or negedge reset)
begin
if(reset==1'b0)
state <= 2'b00;
else
case(state)
2'b00: state <= (coin==1'b0) ? 2'b00 : 2'b01;
2'b01: state <= (coin==1'b0) ? 2'b01 : 2'b10;
2'b10: state <= (coin==1'b0) ? 2'b00 : 2'b01;
default: state <= 2'bxx;
endcase
end
always @(state)
begin
case(state)
2'b00: toy <= 1'b0;
2'b01: toy <= 1'b0;
2'b10: toy <= 1'b1;
default: toy <= 1'bx;
endcase
end
endmodule
```
### kinko org
```
module safe_machine(key, clk, rst, unlock);
input [3:0] key;
input clk, rst;
output unlock;
reg [2:0] state;
reg unlock;
always @(posedge clk or negedge rst)
begin
if(rst==1'b0)
state <= 3'h0;
else
case(state)
3'h0: state <= (key==4'h0) ? 3'h1 : 3'h0;
3'h1: begin
// if(key!=3'h0||key!=3'h7)
// state <= 3'h0;
// else if(key==3'h0)
// state <= 3'h1;
// else if(key==3'h7)
// state <= 3'h2;
case(key)
4'h0: state <= 3'h1;
4'h7: state <= 3'h2;
default: state <= 3'h0;
endcase
end
3'h2: begin
case(key)
4'h0: state <= 3'h1;
4'h4: state <= 3'h3;
default: state <= 3'h0;
endcase
end
3'h3: begin
case(key)
4'h0: state <= 3'h1;
4'h3: state <= 3'h4;
default: state <= 3'h0;
endcase
end
3'h4: state <= (key==4'h0) ? 3'h1 : 3'h0;
default: state <= 3'hx;
endcase
end
always @(state)
begin
case(state)
3'h0: unlock <= 0;
3'h1: unlock <= 0;
3'h2: unlock <= 0;
3'h3: unlock <= 0;
3'h4: unlock <= 1;
default: unlock <= 1'bx;
endcase
end
endmodule
```
### GCD
```
module gcd(A, B, clock, reset, Y, ready);
input [7:0] A, B;
input clock, reset;
output [7:0] Y;
output ready;
reg [7:0] regA, regB;
reg [7:0] nextA, nextB;
reg ready;
reg [1:0] state;
parameter S0 = 2'b00, S1 = 2'b01, S2 = 2'b10, S3 = 2'b11;
always @(posedge clock or negedge reset)
begin
if(reset == 1'b0)
begin
state <= S0;
regA <= 8'h00;
regB <= 8'h00;
end
else
begin
if(nextA == nextB)
state <= S1;
else if(nextA > nextB)
state <= S2;
else
state <= S3;
regA <= nextA;
regB <= nextB;
end
end
always@ ( state or A or B or regA or regB )
begin
case ( state )
S0:
begin
nextA <= A;
nextB <= B;
ready <= 1'b0;
end
S1:
begin
nextA <= regA;
nextB <= regB;
ready <= 1'b1;
end
S2:
begin
nextA <= regA-regB;
nextB <= regB;
ready <= 1'b0;
end
S3:
begin
nextA <= regA;
nextB <= regB-regA;
ready <= 1'b0;
end
default:
begin
nextA <= 8'hxx;
nextB <= 8'hxx;
ready <= 1'bx;
end
endcase //state
end //state
assign Y = regA;
endmodule
```
### div (除算回路)
```
module div(
A,
B,
clock,
reset,
Y,
ready
);
input [7:0] A, B;
input clock, reset;
output [7:0] Y;
output ready;
reg [7:0] regA, regB, regY;
reg [7:0] nextA, nextB, nextY;
reg ready;
reg [1:0] state;
parameter S0 = 2'b00, S1 = 2'b01, S2 = 2'b10;
always @(posedge clock or negedge reset)
begin
if(reset == 1'b0)
begin
state <= S0;
regA <= 8'h00;
regB <= 8'h00;
regY <= 8'h00;
end
else
begin
if(nextA >= nextB)
state <= S2;
else
state <= S1;
regA <= nextA;
regB <= nextB;
regY <= nextY; //
end
end
always @(state or A or B or regA or regB or regY)
begin
case (state)
S0:
begin
nextA <= A;
nextB <= B;
nextY <= regY;
ready <= 0;
end
S1:
begin
nextA <= regA;
nextB <= regB;
nextY <= regY;
ready <= 1;
end
S2:
begin
nextA <= regA - regB;
nextB <= regB;
nextY <= regY + 1;
ready <= 0;
end
default:
begin
nextA <= 8'hxx;
nextB <= 8'hxx;
nextY <= 8'hxx;
ready <= 1'bx;
end
endcase
end
assign Y = nextY;
endmodule
```
### バレルシフタ
dinの値をsftで指定したビットだけ左シフト
✓下位ビットには0を挿入する
◆ 例 : din=4(0000 0100),sft=3(011)のとき、
dout=32(0010 0000)
```
module barrel_shifter( din, sft, dout );
input[7:0] din;
input[2:0] sft;
output[7:0] dout;
assign dout = din << sft;
endmodule
```
### multi
```
module multiplication( a, b, q );
input[3:0] a, b;
output[7:0] q;
assign q = a * b;
endmodule
```
### encoder
```
module encoder( din, dout);
input [3:0] din;
output [1:0] dout;
reg[3:0] dout;
always @(din)
begin
case(din)
4'b0001: dout = 2'b00;
4'b0010: dout = 2'b01;
4'b0100: dout = 2'b10;
4'b1000: dout = 2'b11;
endcase
end
endmodule
```
### 比較
一致比較回路 a=b なら q=1(comp_EQ)
```
module comp_EQ( a, b, q );
input[7:0] a, b;
output[1:0] q;
assign q = ( a == b ) ? 1'b1 : 1'b0;
endmodule
```
### dinの自乗(ROM)
乗算回路で設計することも可能(p4で作ったのと機能は同じ)
```
module square( din, dout );
input[3:0] din;
output[7:0] dout;
reg[7:0] dout;
always@(din)
begin
case(din)
4'd0: dout=8'd0;
4'd1: dout=8'd1;
4'd2: dout=8'd4;
4'd3: dout=8'd9;
4'd4: dout=8'd16;
4'd5: dout=8'd25;
4'd6: dout=8'd36;
4'd7: dout=8'd49;
4'd8: dout=8'd64;
4'd9: dout=8'd81;
4'd10: dout=8'd100;
4'd11: dout=8'd121;
4'd12: dout=8'd144;
4'd13: dout=8'd169;
4'd14: dout=8'd196;
4'd15: dout=8'd225;
default: dout=8'hxx;
endcase
end
endmodule
```
### nbitレジスタ
load 0なら以前のデータを保持, 1ならdinの値を読み込む
```
module nbit_register(clk, din, load, reset ,dout);
input clk,load,reset;
input [7:0] din;
output [7:0] dout;
reg [7:0] dout;
always @( posedge clk or negedge reset)
begin
if( reset == 1'b0)
dout <= 8'b0000_0000;
else if( load == 1'b0)
dout <= dout;
else if( load == 1'b1)
dout <= din;
else
dout <= 8'bxxxx_xxxx;
end
endmodule
```
### register_file
```
module register_file(
input clk,
input reset,
input [7:0] din,
input [2:0] write,
input [2:0] read,
output [7:0] dout
);
wire [7:0] load;
decode3to8 dec(.din(write), .dout(load));
wire [7:0] regAout, regBout, regCout, regDout, regEout, regFout, regGout, regHout;
register regA(.din(din), .dout(regAout), .load(load[0]), .clk(clk), .reset(reset));
register regB(.din(din), .dout(regBout), .load(load[1]), .clk(clk), .reset(reset));
register regC(.din(din), .dout(regCout), .load(load[2]), .clk(clk), .reset(reset));
register regD(.din(din), .dout(regDout), .load(load[3]), .clk(clk), .reset(reset));
register regE(.din(din), .dout(regEout), .load(load[4]), .clk(clk), .reset(reset));
register regF(.din(din), .dout(regFout), .load(load[5]), .clk(clk), .reset(reset));
register regG(.din(din), .dout(regGout), .load(load[6]), .clk(clk), .reset(reset));
register regH(.din(din), .dout(regHout), .load(load[7]), .clk(clk), .reset(reset));
mux8 mux(.read(read), .din0(regAout), .din1(regBout), .din2(regCout), .din3(regDout), .din4(regEout), .din5(regFout), .din6(regGout), .din7(regHout), .dout(dout) );
endmodule
```
### register 8bit
```
module register(
input [7:0] din,
input load,
input clk,
input reset,
output [7:0] dout
);
reg [7:0] dout;
always @(posedge clk or negedge reset)
begin
if(reset == 1'b0)
dout <= 8'b0;
else if(load == 1'b0)
dout <= dout;
else if(load == 1'b1)
dout <= din;
else
dout <= 8'bx;
end
endmodule
```
### mux 8
```
module mux8(
input [2:0] read,
output [7:0] dout,
input [7:0] din0,
input [7:0] din1,
input [7:0] din2,
input [7:0] din3,
input [7:0] din4,
input [7:0] din5,
input [7:0] din6,
input [7:0] din7
);
reg [7:0] dout;
always @(read or din0 or din1 or din2 or din3 or din4 or din5 or din6 or din7)
begin
case(read)
3'b000: dout = din0;
3'b001: dout = din1;
3'b010: dout = din2;
3'b011: dout = din3;
3'b100: dout = din4;
3'b101: dout = din5;
3'b110: dout = din6;
3'b111: dout = din7;
default: dout = 8'bx;
endcase
end
endmodule
```
### decode 3bit to 8 bit
```
module decode3to8(
input [2:0] din,
output [7:0] dout
);
reg [7:0] dout;
always @ (din)
begin
case(din)
3'b000: dout = 8'bxxxxxxx1;
3'b001: dout = 8'bxxxxxx1x;
3'b010: dout = 8'bxxxxx1xx;
3'b011: dout = 8'bxxxx1xxx;
3'b100: dout = 8'bxxx1xxxx;
3'b101: dout = 8'bxx1xxxxx;
3'b110: dout = 8'bx1xxxxxx;
3'b111: dout = 8'b1xxxxxxx;
default: dout = 8'hxx;
endcase
end
endmodule
```
### kinko
```
module kinko(
input[3:0] key,
input clk,
input reset,
output unlock
);
reg unlock;
reg [2:0] state;
parameter S0 = 3'b000, S1 = 3'b001, S2 = 3'b010, S3 = 2'b011, S4 = 3'b100;
always @(posedge clk or negedge reset)
begin
if(reset == 0)
state <= S0;
else
case(state)
S0: state <= (key==0) ? S1 : S0;
S1:
begin
if(key==0)
state <= S1;
else if(key==3'd7)
state <= S2;
else
state <= S0;
end
S2:
begin
if(key==0)
state <= S1;
else if(key==3'd4)
state <= S3;
else
state <= S0;
end
S3:
begin
if(key==0)
state <= S1;
else if(key==3'd3)
state <= S4;
else
state <= S0;
end
S4:
state <= (key==0) ? S1 : S0;
endcase
end
always @(state)
begin
case(state)
S0: unlock <= 0;
S1: unlock <= 0;
S2: unlock <= 0;
S3: unlock <= 0;
S4: unlock <= 1;
default: unlock <= 1'bx;
endcase
end
endmodule
```
## TestBench code
### ALU
```
module alu_op44_tb;
reg [7:0] Ain, Bin;
reg Carryin;
reg [2:0] op_sel;
wire Carryout;
wire [7:0] alu_out;
alu_op44 uut(
.Ain(Ain),
.Bin(Bin),
.op_sel(op_sel),
.Carryin(Carryin),
.Carryout(Carryout),
.alu_out(alu_out)
);
initial begin
op_sel=3'b000;Ain=8'h0f; Bin=8'h01; Carryin=1'b0;
#10 Ain=8'hc8; Bin=8'h64;
#10 Ain=8'ha2; Bin=8'h0b;
#10 op_sel=3'b001; Ain=8'hf0; Bin=8'h0f; Carryin=1'b1;
// #10 Ain=8'b00000001; Bin=8'b0000001;
#10 Ain=8'h01; Bin=8'h01;
#10 op_sel=3'b010; Ain=8'hf0; Bin=8'h0f; Carryin=1'b0;
#10 Ain=8'h6f; Bin=8'h2e;
#10 Ain=8'hff; Bin=8'hff;
#10 op_sel=3'b011; Ain=8'h04; Bin=8'h02;
#10 Ain=8'hff; Bin=8'h00;
#10 Ain=8'h1e; Bin=8'h0a;
#10 op_sel=3'b100; Ain=8'hff; Bin=8'h68;
#10 Ain=8'h0a; Bin=8'h00;
#10 Ain=8'ha0; Bin=8'h0a;
#10 op_sel=3'b101; Ain=8'h0a; Bin=8'hf0;
#10 Ain=8'hff; Bin=8'h68;
#10 Ain=8'h18; Bin=8'h00;
#10 op_sel=3'b110; Ain=8'h0a; Bin=8'hxx;
#10 Ain=8'h00;
#10 Ain=8'hff;
#10 op_sel=3'b111; Ain=8'h0a; Bin=8'h05;
#10 Ain=8'hff; Bin=8'h00;
#10 Ain=8'hff; Bin=8'hff;
#10 $finish;
end
endmodule
```
### OP_Decoder
```
module op_decode_tb;
//inputs
reg [3:0] op;
//outputs
wire [2:0] op_sel;
wire [1:0] reg_sel4;
wire [1:0] reg_sel3;
op_decode uut(
.op(op),
.op_sel(op_sel),
.reg_sel4(reg_sel4),
.reg_sel3(reg_sel3)
);
initial begin
#10 op = 4'b0000;
#10 op = 4'b0001;
#10 op = 4'b0010;
#10 op = 4'b0011;
#10
#10 op = 4'b0100;
#10 op = 4'b0101;
#10 op = 4'b0110;
#10 op = 4'b0111;
#10
#10 op = 4'b1000;
#10 op = 4'b1001;
#10 op = 4'b1010;
#10 op = 4'b1011;
#10
#10 op = 4'b1100;
#10 op = 4'b1101;
#10
#10 op = 4'b1110;
#10 op = 4'b1111;
#10 $finish;
end
endmodule
```
### 8bit非同期レジスタ
```
module register8_tb;
reg [7:0] din;
reg load, clk, reset;
wire [7:0] dout;
register8 uut(.din(din), .load(load), .reset(reset), .clk(clk), .dout(dout));
always begin
clk = 1'b0; #50;
clk = 1'b1; #50;
end
initial begin
din = 8'h00; load = 0; reset = 0;
#10 din = 8'h00; reset = 1; load = 1;
#100 din = 8'hff;
#100 din = 8'h00; load = 0;
#100 din = 8'hff;
#100 reset = 0;
#100 $finish;
end
endmodule
```
### 自販機
```
module vmachine_tb;
reg clock, reset, coin;
wire toy;
vending_machine uut(.clock(clock), .reset(reset), .coin(coin), .toy(toy));
always begin
clock = 1'b0; #50;
clock = 1'b1; #50;
end
initial begin
coin = 0; reset = 0;
#100 coin = 0; reset = 1;
#100 coin = 1;
#100 coin = 1;
#100 coin = 0;
#100 coin = 0;
#100 coin = 0;
#100 coin = 1;
#100 coin = 0;
#100 coin = 1;
#100 coin = 0;
#100 coin = 1;
#100 coin = 0;
#100 coin = 1;
#100 coin = 0;
#100 coin = 0;
#100 coin = 0;
#100 reset = 0;
#100 $finish; //適当です
end
endmodule
```
```
module safemachine_tb;
reg [3:0] key;
reg clk, rst;
wire unlock;
safe_machine uut(.key(key), .clk(clk), .rst(rst), .unlock(unlock));
always begin
clk = 1'b0; #50;
clk = 1'b1; #50;
end
initial begin
key = 4'h0; rst = 0;
#100 key = 4'h0; rst = 1;
#100 key = 4'h7;
#100 key = 4'h4;
#100 key = 4'h3;
#100;
#100 rst = 0;
#100 $finish;
end
endmodule
```
### gcd
```
module gcd_tb;
reg [7:0] A, B;
reg clock, reset;
wire [7:0] Y;
wire ready;
gcd uut(.A(A), .B(B), .clock(clock), .reset(reset), .Y(Y), .ready(ready));
always begin
clock = 1'b0; #50;
clock = 1'b1; #50;
end
initial begin
A=12; B=8; reset = 0;
#10 reset = 1;
#300
#10 reset = 0; A=9; B=5;
#10 reset = 1;
#500
#100 $finish;
end
endmodule
```

### div
```
module div_tb;
reg[7:0] A, B;
reg clock, reset;
wire [7:0] Y;
wire ready;
div uut(.A(A), .B(B), .clock(clock), .reset(reset), .Y(Y), .ready(ready));
always begin
clock = 1'b0; #50;
clock = 1'b1; #50;
end
initial begin
A=6; B=3; reset = 0;
#50 reset = 1;
#300
//#50 reset = 0; A=8; B=2;
//#50 reset = 1;
//#200
#50 reset = 0; A=9; B=2;
#50 reset = 1;
#400
#100 $finish;
end
endmodule
```
### kinko
```
module kinko_tb;
reg [3:0] key;
reg clock, reset;
wire unlock;
kinko uut(.key(key), .clk(clock), .reset(reset), .unlock(unlock));
always begin
clock = 0; #25;
clock = 1; #25;
end
initial begin
reset = 0; key = 0;
#25 reset = 1;
// #50 key = 0;
#50 key = 7;
#50 key = 4;
#50 key = 3;
#50 $finish;
end
endmodule
```

# CISCO
---
メモ
EIGRPとRIPとSTATICを使ってる時
+ ```D 192.168.4.0/24 [90/30720] via ...``` Administrative Distance / Metric
+ ルーティンプロトコルの優先順位
+ eigrpの設定で大きくすると優先される
+ ```Router(config-router) #distance 130 0.0.0.0 255.255.255.255```130より大きい値
+ すべてのアドレスに適用
+ 故障した際の代替経路に使用??
TELNET
+ ルータログインするとき 管理者用のパスワードがついてないとき enできない
+ 回線番号 0から始まる 1だと2になる
+ ```line con 0```~~telnetパスワード設定~~ コンソールパスワード
+ ```line vty 0``` virtual T lineパスワード設定
+ telnet
+ ```password cisco```
+ 管理者用(特権exec用)
``` enable secret```
+ 具体例パスワード設定
```Router(config)#enable secret XXXXXX```
172.16.1.1/24のIPアドレス eigrp
---
~~設定例 1 : (config-router)# network 172.16.1.0 0.0.0.255~~
~~設定例 2 : (config-router)# network 172.16.1.1 0.0.0.0~~
(config-router)# network 172.16.1.0
---
IPアドレスはPCのIPアドレス
MACもそう
デフォルトゲートウェイはルーターのPCと接続している側のIPアドレス
スイッチと繋がっているケーブルはストレートケーブル(実線)
それ以外はクロスケーブル(破線)
---
Router>enable
Router#counfigure terminal(conf tでも可)
Router(config)#interface(intでも可) fa x/x //x/xは接続するポート
Router(config-if)#ip add xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy //xはPCと接続する側のIPアドレス yはMACアドレス
Router(config-if)#no shutdown
Router(config-if)#end
---
保存
Router#copy running-config startup-config (copy run start)
---
Router#configure terminal
Router(config)#ip route xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy zzz.zzz.zzz.zzz
xは接続相手のルーターとそこにつながっているPCとのネットワークアドレス(末尾は0)
yはそのサブネットマスク
zは接続相手の迎え入れるIPアドレス
---
PCどうしをつなげるとき,必ず通る必要があるルーターがある場合(T字ネットワークの交点など)では
そのルーターはすべてのネットワークにつなぐ(ip routeを一個一個設定)
そのルーターにつながっているルーターはip route 0.0.0.0 0.0.0.0 zzz.zzz.zzz.zzzで接続可能
tracert xxx.xxx.xxx //ルート確認
---
//ルータの名前変更
```
Router#conf t
Router(config)#hostname R1
R1(config)
```
---
show ip route
show interfaces
show version
---
### RIPの設定
```
Router(config)#router rip
//RIPで分割ネットワークを接続するとき (最初にしないと死ぬ)
Router(config-router)# ver 2 (バージョン2に変更)
Router(config-router)# no auto-summary
Router(config-router)# network(net) xxx.xxx.xxx.0(自分の持っているネットワークアドレス)
```
---
### EIGRPの設定
```
Router(config)#router eigrp aaa aは任意の数字(ネットワーク内で同じのを使う)
Router(config-router)# network(net) xxx.xxx.xxx.0(自分の持っているネットワークアドレス)
↑持っているアドレスの数だけやる
```
summaryの削除(全部のルータにやる)
```
Router(config)#router eigrp aaa
Router(config-router)# no auto-summary
```
もともと1つのネットワークだったものを分割したネットワークが
離れた状態で置かれたネットワークではauto-summaryを消すこと

```
Router(config-router)# distance bbb bはアドミニストレーティブディスタンス(優先度)
```
RIP接続の優先度の変更(EIGRPは90,スタティックルートは1)
---
### RIPのネットワークとEIGRPのネットワークを接続
ネットワーク同士をつなぐルータのeigrp側で,
```
Router(config)#router rip
Router(config-router)# redistribute eigrp 100 metric 5
Router(config-router)# ex
Router(config)#router eigrp 100
Router(config-router)# redistribute rip metric 100000 1 255 25 1500
Router(config)#router eigrp aaa (もしくはRIP)
Router(config-router)# redistribute static
```
sumarryは割となしでも動いた -> 隣接してるから必要なかっただけ
```
Router(config)#router rip
Router(config-router)#redistribute eigrp 100 metric 5
Router(config-router)#ex
Router(config)#router eigrp 100
Router(config-router)#redistribute rip metric ?
<1-4294967295> Bandwidth metric in Kbits per second
Router(config-router)#redistribute rip metric 100000 ?
<0-4294967295> EIGRP delay metric, in 10 microsecond units
Router(config-router)#redistribute rip metric 100000 1 ?
<0-255> EIGRP reliability metric where 255 is 100% reliable
Router(config-router)#redistribute rip metric 100000 1 255 ?
<1-255> EIGRP Effective bandwidth metric (Loading) where 255 is 100% loaded
Router(config-router)#redistribute rip metric 100000 1 255 25 ?
<1-65535> EIGRP MTU of the path
Router(config-router)#redistribute rip metric 100000 1 255 25 1500
```
---
### 計算
つめて置くやつ 最後のPCのIPアドレス
(例)
172.16.110.225
+ ネットワーク:172.16.64.0
+ 4000台 + 8000台
+ 4001 + 8000 = 12001
+ 0010 1110 1110 0001
+ +64 = 0110 1110 1110 0001
+ 110.225
172.16.186.153
+ ネットワーク:172.16.128.0
+ 5000台 + 10000台
+ 5001 + 10000 = 15001
+ 0011 1010 1001 1001
+ +128 = 1011 1010 1001 1001
+ 186.153
---
## サブネットマスク
| /32 255.255.255.255
---
| /31 255.255.255.254
| /30 255.255.255.252
| /29 255.255.255.248
| /28 255.255.255.240
| /27 255.255.255.224
| /26 255.255.255.192
| /25 255.255.255.128
| /24 255.255.255.0
| /23 255.255.254.0
| /22 255.255.252.0
| /21 255.255.248.0
| /20 255.255.240.0
| /19 255.255.224.0
| /18 255.255.192.0
| /17 255.255.128.0
| /16 255.255.0.0
| /15 255.254.0.0
| /14 255.252.0.0
| /13 255.248.0.0
| /12 255.240.0.0
| /11 255.224.0.0
| /10 255.192.0.0
| /9 255.128.0.0
| /8 255.0.0.0
| /7 254.0.0.0
| /6 252.0.0.0
| /5 248.0.0.0
| /4 240.0.0.0
| /3 224.0.0.0
| /2 192.0.0.0
| /1 128.0.0.0
| /0 0.0.0.0