[关闭]
@byjr-k 2016-01-06T13:38:13.000000Z 字数 6988 阅读 2925

EEE339 Final Exam

大学学习


Content

EEE339 2014-15


Question 2

  1. module q2(A, B, C, D, S1, S2, OUT);
  2. input A, B, C, D, S1, S2;
  3. output OUT;
  4. wire w1, w2, w3, w4;
  5. assign w1 = A & B & S1;
  6. assign w2 = ~(S1 | C);
  7. assign w3 = ~(S2 ^ B ^ S1);
  8. assign w4 = ~(S1 & s2 & D);
  9. assign OUT = w1 | w2 | w3 | w4;
  10. endmodule

Question 3

Asynchronous

  1. module ff(clk, reset, out);
  2. input clk, reset;
  3. outout reg out;
  4. integer i = 0;
  5. initial
  6. out = 0;
  7. always@(posedge clk or negedge reset)
  8. begin
  9. if (reset) begin
  10. i = 0;
  11. out = 0;
  12. end
  13. else begin
  14. if (i >= 250) begin
  15. out = 1;
  16. i = 0;
  17. end
  18. else
  19. i = i + 1;
  20. end
  21. end
  22. endmodule

Synchronous

  1. module ff(clk, reset, out);
  2. input clk, reset;
  3. outout reg out;
  4. integer i = 0;
  5. initial
  6. out = 0;
  7. always@(posedge clk)
  8. begin
  9. if (reset) begin
  10. i = 0;
  11. out = 0;
  12. end
  13. else begin
  14. if (i >= 250) begin
  15. out = 1;
  16. i = 0;
  17. end
  18. else
  19. i = i + 1;
  20. end
  21. end
  22. endmodule

Question 4

Mealy

  1. module mealy(clk, reset, x, z);
  2. input clk, reset, x;
  3. output reg z;
  4. integer y, Y;
  5. parameter [2:0] S0 = 0, S1 = 1, S2 = 2, S3 = 3, S4 = 4;
  6. always @(x, y)
  7. begin
  8. case (y)
  9. S0: begin
  10. if (x) begin
  11. z = 0;
  12. Y = S1;
  13. end
  14. else begin
  15. z = 0;
  16. Y = S0;
  17. end
  18. end
  19. S1: begin
  20. if (x) begin
  21. z = 0;
  22. Y = S2;
  23. end
  24. else begin
  25. z = 0;
  26. Y = S0;
  27. end
  28. end
  29. S2: begin
  30. if (x) begin
  31. z = 0;
  32. Y = S3;
  33. end
  34. else begin
  35. z = 0;
  36. Y = S0;
  37. end
  38. end
  39. S3: begin
  40. if (x) begin
  41. z = 1;
  42. Y = S4;
  43. end
  44. else begin
  45. z = 0;
  46. Y = S0;
  47. end
  48. end
  49. S4: begin
  50. if (x) begin
  51. z = 1;
  52. Y = S4;
  53. end
  54. else begin
  55. z = 0;
  56. Y = S0;
  57. end
  58. end
  59. endcase
  60. end
  61. always @(posedge clk)
  62. begin
  63. if (reset == 1) y <= S0;
  64. else y <= Y;
  65. end
  66. endmodule

Question 5

Type A B C
CPI 1.1 2.7 1.9

(a)

A 32%, B 27%, C 41%
Avg. CPI = 1.1*0.32 + 2.7*0.27 + 1.9*0.41
         = 1.86

A 29%, B 17%, C 54%
Avg. CPI = 1.1*0.29 + 2.7*0.17 + 1.9*0.54
         = 1.804

(b)

CPU Time = Instruction Count x CPI / Clock Rate

P1 CPU Time = 100 * 1.86  / 2800000000
P2 CPU Time = 100 * 1.804 / 2800000000

P1 / P2 = 1.86 / 1.804 = 1.031

Ans: P2 is 1.031 times faster than P2

Question 6

(a) Forwarding (in a pipeline)

Part 2 Lecture 13

(b) PC-relative addressing

Part 2 Lecture 5

(c) Single-cycle datapath

(d) Pipelining

Part 2 Lecture 12

(e) Exception

Part 2 Lecture 10-11 Page 21


Question 8

module CPU (clock, reset);
    parameter R_Type = 6'b0, LW = 6'b100011, SW = 6'b101011, BEQ = 6'b000100, J = 6'd2
    input clock, reset; // external inputs
    reg [31:0] PC, Regs[0:31], Memory [0:31], IR, ALUOut, MDR, A, B;
    reg [2:0] state; // processor state
    wire [5:0]] opcode;
    wire [31:0] SignExtend, PCOffset;
    assign opcode = IR[31:26]; // opcode is upper 6 bits
    assign SignExtend = {16{IR[15]}, IR[15:0]};
    assign PCOffset = SignExtend << 2; // PC offset is shifted
    // set the PC to 0 and start the control in state 1
    initial begin
        PC = 0;
        state = 1;
    end

    always @(posedge clock or posedge reset) begin
        Regs[0] = 0; // make sure R0 is always 0

        if (reset) begin
            PC = 0;
            state = 1;
            // words 0-15  used for instruction memory
            // words 16-31 used for data memory
            Memory[16] = 32'h5;
            Memory[17] = 32'hB;
        end

        case (state) // action depends on the state
            1: begin // first step: fetch the instruction, increment PC, go to next state
                IR <= Memory[PC>>2];
                PC <= PC + 4;
                state = 2; // next state
            end

            2: begin // second step
                A <= Regs[IR[25:21]];
                B <= Regs[IR[20:16]];
                ALUOut <= PC + PCOffset;
                state = 3;
            end

            3: begin // third step: load/sotre execution, ALU execution, branch completion
                state = 4; // default next state
                if ((opcode == LW) | (opcode == SW))
                    ALUOut <= A + SignExtend; //                         A
                else if (opcode == R_Type)
                    case (IR[5:0])
                        32: ALUOut = A + B; // add operation
                        default: ALUOut = A; // other operations
                    endcase
                else if (opcode == BEQ) begin
                    PC <= (A == B) ? ALUOut : PC; //                     B
                    state = 1;
                end
                else if (opcode == J)
                    PC <= {PC[31:28], IR[25:0], 2'b00}; //               C
                    state = 1;
                end
            end

            4: begin
                if (opcode == R_Type) begin // ALU Operation
                    Regs[IR[15:11]] <= ALUOut; //                        D
                    state = 1;
                end
                else if (opcode == LW) begin // load instruction
                    MDR <= Memory[ALUOut>>2]; // read the memory
                    state = 5; // next state
                end
                else if (opcode == SW) begin
                    Memory[ALUOut>>2] <= B; // write the memory
                    state = 1; // return to state 1
                end
            end

            5: begin // LW instruction
                Regs[IR[20:16]] <= MDR; //                               E
                state = 1;
            end // complete an LW instruction
        endcase
    end
endmodule

EEE305 2013-14


Question 1

  1. A task always has to define I/O False
  2. Port order doesn't matter when using port name instantiations True
  3. output is a keyword when defining a function False
  4. Non-blocking assignments are used when referring combinational logic False
  5. A scalar wire is the default data type in Verilog True
  6. The integer data type is unsigned False
  7. In the statement assign a = b both a and b have to be reg data type False
  8. (4'b00x0 == 4'b0x00) would give the result: 1'bx True
  9. (4'b1100 | 4'b0000) would give the result: 4'b1111 False
  10. (4'b1100 || 4'b0000) would give the result: 4'b1111 False
Index Reason
1 task can have no I/O
2 only port order instantiations does matter with port order
3 function doesn't have output, only has return value
4 sequential
5
6 signed
7 reg is needed for always block
8
9 4'b1100
10 1'b1

Question 2

  1. 1BD命名格式错误
  2. assign赋值的变量不能是reg
  3. module后面括号的内容必须全部定义为port
  4. always块中赋值的变量必须为reg,必须不能为wire
  5. andgate使用错误

Question 3

  1. module question3(A, B, C, D, E, F, G, H, I, J, K, L, SEL1, SEL2, CLK, RESET, Q1, Q2, Q3, Q4);
  2. input A, B, C, D, E, F, G, H, I, J, K, L, SEL1, SEL2, CLK, RESET;
  3. output reg Q1, Q2, Q3, Q4;
  4. wire D1, D2, D3, D4;
  5. initial begin
  6. Q1 = 0;
  7. Q2 = 0;
  8. Q3 = 0;
  9. Q4 = 0;
  10. end
  11. assign D1 = (A & B) ^ C ^ D,
  12. D2 = SEL1 ? D : (E | F),
  13. D3 = SEL2 ? I : (G & H),
  14. D4 = (~(J & K)) ~^ L;
  15. always @(posedge CLK) begin
  16. if (reset) begin
  17. Q1 = 0;
  18. Q2 = 0;
  19. Q3 = 0;
  20. Q4 = 0;
  21. end
  22. else begin
  23. Q1 = D1;
  24. Q2 = D2;
  25. Q3 = D3;
  26. Q4 = D4;
  27. end
  28. end
  29. endmodule

Question 4

  1. module question4(data, clock, reset, count);
  2. input data, clock, reset;
  3. output reg [6:0] count; // max = 128 > 100
  4. integer cycle;
  5. reg [3:0] value;
  6. initial begin
  7. count = 0;
  8. value = 4'b0000; //'
  9. end
  10. always @(posedge clock) begin
  11. if (!reset) begin
  12. cycle = 0;
  13. count = 0;
  14. value = 0;
  15. end
  16. else begin
  17. cycle = cycle + 1;
  18. if (cycle > 100) begin
  19. cycle = 0;
  20. count = 0;
  21. value = 0;
  22. end
  23. else begin
  24. value << 1;
  25. value[0] = data;
  26. if (value == 11) begin // 11(10) = 1011(2)
  27. count = count + 1;
  28. end
  29. end
  30. end
  31. end
  32. endmodule

Question 5

Moore

  1. module question5(clock, resetn, w, z);
  2. input clock, resetn, w;
  3. output z;
  4. reg [1:0] y;
  5. parameter [1:0] S0 = 0, S1 = 1, S2 = 2; // 00 01 10
  6. always @(posedge clock or negedge reset) begin
  7. if (resetn == 0) y <= S0;
  8. else begin
  9. case (y)
  10. S0: if (w == 0) y <= S0;
  11. else y <= S1;
  12. S1: if (w == 0) y <= S1;
  13. else y <= S2;
  14. S2: y <= S0;
  15. default: y <= 2'bxx; // 'x会自动向左填充
  16. endcase
  17. end
  18. end
  19. assign z = (y == S1 || y == S2);
  20. endmodule

EEE305 2012-13


Question 2

  1. Verilog is case sensitive True
  2. Verilog synthesizers treat the white space ' ' and carriage returns differently False
  3. beginmodule and endmodule are reserved words in Verilog False
  4. 2'b1x == 2'b1x has a true return value False
  5. The semantics of an & operator depends on the number of operands True
  6. An if statement must always be inside of an always block False
  7. Verilog permits module ports to be unconnected True
  8. The use of the initial keyword is to model circuit behaviour at time 0 and possibly beyond False
  9. The left argument of a statement in an initial or always block may be a wire False
  10. It is NOT necessary for a task to have inputs and outputs True
Index Reason
1
2 空格、制表符、回车全都当作一个空格(貌似……)
3
4 return value is x
5 bitwise/reduction operators
6 还可以在task、function中
7
8 不能beyond(貌似……)
9
10

Question 3

module test(W, Y);
    input [7:0] W;
    output reg [2:0] Y;
    always @(W)
        case (W)
            8'b00000001: Y = 3'b000;
            8'b00000010: Y = 3'b001;
            8'b00000100: Y = 3'b010;
            8'b00001000: Y = 3'b011;
            8'b00010000: Y = 3'b100;
            8'b00100000: Y = 3'b101;
            8'b01000000: Y = 3'b110;
            8'b10000000: Y = 3'b111;
            default: Y = 3'bxxx;
        endcase
endmodule

(b)

module test(W, Y);
    input [7:0] W;
    output reg [2:0] Y;
    always @(W)
        case (W)
            8'b00000001: Y = 3'b000;
            8'b0000001x: Y = 3'b001;
            8'b000001xx: Y = 3'b010;
            8'b00001xxx: Y = 3'b011;
            8'b0001xxxx: Y = 3'b100;
            8'b001xxxxx: Y = 3'b101;
            8'b01xxxxxx: Y = 3'b110;
            8'b1xxxxxxx: Y = 3'b111;
            default: Y = 3'bxxx;
        endcase
endmodule

(c)

module test(W, Y);
    input [7:0] W;
    wire [7:0] data;
    reg [3:0] count = 0;
    output reg [2:0] Y;
    always @(W) begin
        Y = 7;
        for (count = 0; count < 8; count = count + 1)
            if (!(data && 8'b10000000)) begin
                data << 1;
                count = count + 1;
                Y = Y - 1;
            end
            else count = 8;
        end
        if (count == 8) Y = 3'bxxx;
    end
endmodule

EEE304 2013-14


Question 2

int x[20], i;
for i = 0 to 19
if x[i] < 17
    x[i] = 18;
  1. Loop: sll $t1, $s1, 2
  2. add $t1, $t1, $t0
  3. lw $t0, 0($t1)
  4. beq $s1, 20, Exit
  5. addi $1, $1, 1
  6. slti $t2, $t0, 17
  7. beq $t2, 1, Do
  8. j Loop
  9. addi $t2, $zero, 18
  10. Do: sw $t2, 0($t0)
  11. j Loop
  12. Exit: ...
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注