picoblaze的学习使用

`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer:
//
// Create Date:    11:15:20 12/11/2011
// Design Name:
// Module Name:    uclock
// Project Name:
// Target Devices:
// Tool versions:
// Description:
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module uclock(
    clk,
    reset,
     rs232_tx,
     rs232_rx,
     led,
     switch,
     Anodes,
     Cathodes
    );
    
//
//////////////////////////////////////////////////////////////////////
//
// Define ports
//
//////////////////////////////////////////////////////////////////////
//
input  clk;
input  reset;
output rs232_tx;
input  rs232_rx;
output [7:0] led;
input  [7:0] switch;
output [3:0] Anodes;
output [7:0] Cathodes;

//
//////////////////////////////////////////////////////////////////////
//
// Define signals used later
//
//////////////////////////////////////////////////////////////////////
//

/*wire reset;
assign reset = ~rst;*/
reg    [7:0] led;

wire   [9:0] address;
wire  [17:0] instruction;
wire         write_strobe;
wire         read_strobe;
wire   [7:0] port_id;
wire   [7:0] out_port;
reg    [7:0] in_port;
reg          interrupt;
wire         interrupt_ack;

//Instantiate kcpsm3
kcpsm3 kcpsm3(
    .address(address),
    .instruction(instruction),
    .port_id(port_id),
    .write_strobe(write_strobe),
    .out_port(out_port),
    .read_strobe(read_strobe),
    .in_port(in_port),
    .interrupt(interrupt),
    .interrupt_ack(interrupt_ack),
    .reset(reset),
    .clk(clk)
    );
//Instantialte program
program program(
    .address(address),
     .instruction(instruction),
     .clk(clk)
     );
     
//1us interrupt
reg  [9:0] int_count;
reg  interrupt_event;

always @(posedge clk or posedge reset) begin
    if(reset) begin
        int_count <= 0;
        interrupt_event <= 0;
    end
    else if(int_count == 99) begin
            int_count <= 0;
            interrupt_event <= 1;
    end
    else begin
        int_count <= int_count + 1;
        interrupt_event <= 0;
    end
end
//
//interrupt design
//
always @(posedge clk or posedge interrupt_ack) begin
    if(interrupt_ack) begin
        interrupt <= 0;
    end
    else if(interrupt_event) begin
        interrupt <= 1;
    end
    else begin
       interrupt <= interrupt;
    end
end
             
//
//Input ports design
//
reg  read_from_uart;
wire rx_buffer_full;
wire rx_buffer_half_full;
wire tx_buffer_full;
wire tx_buffer_half_full;
wire data_present;
wire [7:0] uart_present;
wire [7:0] data_rx;

assign uart_present = {3'b000,data_present,rx_buffer_full,rx_buffer_half_full,tx_buffer_full,tx_buffer_half_full};


always @(posedge clk or posedge reset) begin
    if(reset) begin
        in_port <= 0;
        read_from_uart <= 0;
    end
    else begin
        case(port_id)
            8'h00: in_port <= switch;
            8'h02: in_port <= data_rx;
            8'h04: in_port <= uart_present;
            default : in_port <= 8'h00;
        endcase
        read_from_uart <= read_strobe && port_id == 8'h02;
    end
end

//
//generate 16 times of baudrate of 9600
//

reg     [9:0] baud_count;
reg           en_16_x_baud;

always @(posedge clk or posedge reset)begin
  if (reset) begin
    baud_count <= 0;
    en_16_x_baud <= 0;
  end
  else begin
    if (baud_count == 651) begin
      baud_count <= 0;
      en_16_x_baud <= 1;
    end
    else begin
      baud_count <= baud_count + 1;
      en_16_x_baud <= 0;
    end
  end
end
 
//
//Output ports design
// 
wire write_to_uart;
wire write_to_leds;
reg  [7:0] seg_num1;
reg  [7:0] seg_num2;
reg  [7:0] seg_num3;
reg  [7:0] seg_num4;
wire write_to_seg_num1;
wire write_to_seg_num2;
wire write_to_seg_num3;
wire write_to_seg_num4;

assign write_to_leds = (port_id == 8'h01 && write_strobe);
assign write_to_uart = (port_id == 8'h03 && write_strobe);
assign write_to_seg_num1 = (port_id == 8'h05 && write_strobe);
assign write_to_seg_num2 = (port_id == 8'h06 && write_strobe);
assign write_to_seg_num3 = (port_id == 8'h07 && write_strobe);
assign write_to_seg_num4 = (port_id == 8'h08 && write_strobe);

always @(posedge clk) begin
    if(write_to_leds == 1) begin
        led <= out_port;
    end
end

always @(posedge clk) begin
    if(write_to_seg_num1) begin
        seg_num1 <= out_port;
    end
end

always @(posedge clk) begin
    if(write_to_seg_num2) begin
        seg_num2 <= out_port;
    end
end

always @(posedge clk) begin
    if(write_to_seg_num3) begin
        seg_num3 <= out_port;
    end
end

always @(posedge clk) begin
    if(write_to_seg_num4) begin
        seg_num4 <= out_port;
    end
end

// Instantiate uart_rx module
uart_rx uart_rx (
    .serial_in(rs232_rx),
    .data_out(data_rx),
    .read_buffer(read_from_uart),
    .reset_buffer(reset),
    .en_16_x_baud(en_16_x_baud),
    .buffer_data_present(data_present),
    .buffer_full(rx_buffer_full),
    .buffer_half_full(rx_buffer_half_full),
    .clk(clk)
    );

     
// Instantiate uart_tx module
uart_tx uart_tx(
    .data_in(out_port),
    .write_buffer(write_to_uart),
    .reset_buffer(reset),
    .en_16_x_baud(en_16_x_baud),
    .serial_out(rs232_tx),
    .buffer_full(tx_buffer_full),
    .buffer_half_full(tx_buffer_half_full),
    .clk(clk)
    );
     
// Instantiate seg_disp module
seg_disp seg_disp (
    .seg_num1(seg_num1),
    .seg_num2(seg_num2),
    .seg_num3(seg_num3),
    .seg_num4(seg_num4),
    .clk(clk),
    .reset(reset),
    .Anodes(Anodes),
    .Cathodes(Cathodes)
    );
     
endmodule
 

 

psm软件部分程序

KCPSM3 Assembler log file for program 'program.psm'.
Generated by KCPSM3 version 1.31
Ken Chapman (Xilinx Ltd) 2005.
16Dec2011-18:21:13

 Addr Code

 000                      ;================================================================
 000                      ; Port address definitions...
 000                      ;================================================================
 000                      CONSTANT switch_in, 00                       ; Switch read port
 000                      CONSTANT leds_out, 01                        ; LED write port
 000                      CONSTANT uart_data_rx, 02                    ; UART receive read port
 000                      CONSTANT uart_data_tx, 03                    ; UART transmit write port
 000                      CONSTANT data_present, 04                    ; UART stat {3'b000,data_present,rx_buffer_full,
 000                      ;           rx_buffer_full,tx_buffer_full,tx_buffer_half_full}
 000                      CONSTANT seg_num1, 05                        ; 7_segment Led1
 000                      CONSTANT seg_num2, 06                        ; 7_segment Led2
 000                      CONSTANT seg_num3, 07                        ; 7_segment Led3
 000                      CONSTANT seg_num4, 08                        ; 7_segment Led4
 000                      ;================================================================
 000                      ; Useful constant declarations...
 000                      ;================================================================
 000                      CONSTANT all_clear, 00                       ; define zero
 000                      CONSTANT ascii_NUL, 00                       ; ascii code (null character)
 000                      CONSTANT ascii_SOH, 01                       ; ascii code (start of header)
 000                      CONSTANT ascii_STX, 02                       ; ascii code (start of text)
 000                      CONSTANT ascii_ETX, 03                       ; ascii code (end of text)
 000                      CONSTANT ascii_EOT, 04                       ; ascii code (end of xmit)
 000                      CONSTANT ascii_ENQ, 05                       ; ascii code (enquiry)
 000                      CONSTANT ascii_ACK, 06                       ; ascii code (acknowledge)
 000                      CONSTANT ascii_BEL, 07                       ; ascii code (bell)
 000                      CONSTANT ascii_BS, 08                        ; ascii code (backspace)
 000                      CONSTANT ascii_HT, 09                        ; ascii code (horiz tab)
 000                      CONSTANT ascii_LF, 0A                        ; ascii code (line feed)
 000                      CONSTANT ascii_VT, 0B                        ; ascii code (vert tab)
 000                      CONSTANT ascii_FF, 0C                        ; ascii code (form feed)
 000                      CONSTANT ascii_CR, 0D                        ; ascii code (carriage return)
 000                      CONSTANT ascii_SO, 0E                        ; ascii code (shift out)
 000                      CONSTANT ascii_SI, 0F                        ; ascii code (shift in)
 000                      CONSTANT ascii_DLE, 10                       ; ascii code (data link esc)
 000                      CONSTANT ascii_DC1, 11                       ; ascii code (device ctrl 1, xon)
 000                      CONSTANT ascii_DC2, 12                       ; ascii code (device ctrl 2)
 000                      CONSTANT ascii_DC3, 13                       ; ascii code (device ctrl 3, xoff)
 000                      CONSTANT ascii_DC4, 14                       ; ascii code (device ctrl 4)
 000                      CONSTANT ascii_NAK, 15                       ; ascii code (negative acknowledge)
 000                      CONSTANT ascii_SYN, 16                       ; ascii code (sync idle)
 000                      CONSTANT ascii_ETB, 17                       ; ascii code (end xmit block)
 000                      CONSTANT ascii_CAN, 18                       ; ascii code (cancel)
 000                      CONSTANT ascii_EM, 19                        ; ascii code (end of medium)
 000                      CONSTANT ascii_SUB, 1A                       ; ascii code (substitute)
 000                      CONSTANT ascii_ESC, 1B                       ; ascii code (escape)
 000                      CONSTANT ascii_FS, 1C                        ; ascii code (file separator)
 000                      CONSTANT ascii_GS, 1D                        ; ascii code (group separator)
 000                      CONSTANT ascii_RS, 1E                        ; ascii code (record separator)
 000                      CONSTANT ascii_US, 1F                        ; ascii code (unit space)
 000                      CONSTANT ascii_SPACE, 20                     ; ascii code
 000                      CONSTANT ascii_EXCLAIM, 21                   ; ascii code
 000                      CONSTANT ascii_DBLQUOT, 22                   ; ascii code
 000                      CONSTANT ascii_NUMSIGN, 23                   ; ascii code
 000                      CONSTANT ascii_DOLLAR, 24                    ; ascii code
 000                      CONSTANT ascii_PERCENT, 25                   ; ascii code
 000                      CONSTANT ascii_AMP, 26                       ; ascii code
 000                      CONSTANT ascii_SINQUOT, 27                   ; ascii code
 000                      CONSTANT ascii_LPAREN, 28                    ; ascii code
 000                      CONSTANT ascii_RPAREN, 29                    ; ascii code
 000                      CONSTANT ascii_ASTERISK, 2A                  ; ascii code
 000                      CONSTANT ascii_PLUS, 2B                      ; ascii code
 000                      CONSTANT ascii_COMMA, 2C                     ; ascii code
 000                      CONSTANT ascii_MINUS, 2D                     ; ascii code
 000                      CONSTANT ascii_PERIOD, 2E                    ; ascii code
 000                      CONSTANT ascii_FWDSLASH, 2F                  ; ascii code
 000                      CONSTANT ascii_0, 30                         ; ascii code
 000                      CONSTANT ascii_1, 31                         ; ascii code
 000                      CONSTANT ascii_2, 32                         ; ascii code
 000                      CONSTANT ascii_3, 33                         ; ascii code
 000                      CONSTANT ascii_4, 34                         ; ascii code
 000                      CONSTANT ascii_5, 35                         ; ascii code
 000                      CONSTANT ascii_6, 36                         ; ascii code
 000                      CONSTANT ascii_7, 37                         ; ascii code
 000                      CONSTANT ascii_8, 38                         ; ascii code
 000                      CONSTANT ascii_9, 39                         ; ascii code
 000                      CONSTANT ascii_COLON, 3A                     ; ascii code
 000                      CONSTANT ascii_SEMI, 3B                      ; ascii code
 000                      CONSTANT ascii_LESS, 3C                      ; ascii code
 000                      CONSTANT ascii_EQUAL, 3D                     ; ascii code
 000                      CONSTANT ascii_GREATER, 3E                   ; ascii code
 000                      CONSTANT ascii_QUESTION, 3F                  ; ascii code
 000                      CONSTANT ascii_CIRCAT, 40                    ; ascii code
 000                      CONSTANT ascii_A, 41                         ; ascii code
 000                      CONSTANT ascii_B, 42                         ; ascii code
 000                      CONSTANT ascii_C, 43                         ; ascii code
 000                      CONSTANT ascii_D, 44                         ; ascii code
 000                      CONSTANT ascii_E, 45                         ; ascii code
 000                      CONSTANT ascii_F, 46                         ; ascii code
 000                      CONSTANT ascii_G, 47                         ; ascii code
 000                      CONSTANT ascii_H, 48                         ; ascii code
 000                      CONSTANT ascii_I, 49                         ; ascii code
 000                      CONSTANT ascii_J, 4A                         ; ascii code
 000                      CONSTANT ascii_K, 4B                         ; ascii code
 000                      CONSTANT ascii_L, 4C                         ; ascii code
 000                      CONSTANT ascii_M, 4D                         ; ascii code
 000                      CONSTANT ascii_N, 4E                         ; ascii code
 000                      CONSTANT ascii_O, 4F                         ; ascii code
 000                      CONSTANT ascii_P, 50                         ; ascii code
 000                      CONSTANT ascii_Q, 51                         ; ascii code
 000                      CONSTANT ascii_R, 52                         ; ascii code
 000                      CONSTANT ascii_S, 53                         ; ascii code
 000                      CONSTANT ascii_T, 54                         ; ascii code
 000                      CONSTANT ascii_U, 55                         ; ascii code
 000                      CONSTANT ascii_V, 56                         ; ascii code
 000                      CONSTANT ascii_W, 57                         ; ascii code
 000                      CONSTANT ascii_X, 58                         ; ascii code
 000                      CONSTANT ascii_Y, 59                         ; ascii code
 000                      CONSTANT ascii_Z, 5A                         ; ascii code
 000                      CONSTANT ascii_LBKT, 5B                      ; ascii code
 000                      CONSTANT ascii_BKSLASH, 5C                   ; ascii code
 000                      CONSTANT ascii_RBKT, 5D                      ; ascii code
 000                      CONSTANT ascii_CARET, 5E                     ; ascii code
 000                      CONSTANT ascii_UNDER, 5F                     ; ascii code
 000                      CONSTANT ascii_TICK, 60                      ; ascii code
 000                      CONSTANT ascii_a, 61                         ; ascii code
 000                      CONSTANT ascii_b, 62                         ; ascii code
 000                      CONSTANT ascii_c, 63                         ; ascii code
 000                      CONSTANT ascii_d, 64                         ; ascii code
 000                      CONSTANT ascii_e, 65                         ; ascii code
 000                      CONSTANT ascii_f, 66                         ; ascii code
 000                      CONSTANT ascii_g, 67                         ; ascii code
 000                      CONSTANT ascii_h, 68                         ; ascii code
 000                      CONSTANT ascii_i, 69                         ; ascii code
 000                      CONSTANT ascii_j, 6A                         ; ascii code
 000                      CONSTANT ascii_k, 6B                         ; ascii code
 000                      CONSTANT ascii_l, 6C                         ; ascii code
 000                      CONSTANT ascii_m, 6D                         ; ascii code
 000                      CONSTANT ascii_n, 6E                         ; ascii code
 000                      CONSTANT ascii_o, 6F                         ; ascii code
 000                      CONSTANT ascii_p, 70                         ; ascii code
 000                      CONSTANT ascii_q, 71                         ; ascii code
 000                      CONSTANT ascii_r, 72                         ; ascii code
 000                      CONSTANT ascii_s, 73                         ; ascii code
 000                      CONSTANT ascii_t, 74                         ; ascii code
 000                      CONSTANT ascii_u, 75                         ; ascii code
 000                      CONSTANT ascii_v, 76                         ; ascii code
 000                      CONSTANT ascii_w, 77                         ; ascii code
 000                      CONSTANT ascii_x, 78                         ; ascii code
 000                      CONSTANT ascii_y, 79                         ; ascii code
 000                      CONSTANT ascii_z, 7A                         ; ascii code
 000                      CONSTANT ascii_LBRACE, 7B                    ; ascii code
 000                      CONSTANT ascii_VBAR, 7C                      ; ascii code
 000                      CONSTANT ascii_RBRACE, 7D                    ; ascii code
 000                      CONSTANT ascii_TILDE, 7E                     ; ascii code
 000                      CONSTANT ascii_DEL, 7F                       ; ascii code
 000                      CONSTANT time_cnt_lsb_c, E8
 000                      CONSTANT time_cnt_msb_c, 03
 000                      CONSTANT number_turn_c, 0A
 000                      ;================================================================
 000                      ; Useful NAMEREG declarations...
 000                      ;================================================================
 000                      ;namereg sc                , number_disp   ;   number displayed in 7_seg led
 000                      NAMEREG sD, time_count_lsb                   ;   lower 8_bit
 000                      NAMEREG sE, time_count_msb                   ;   upper 8_bit
 000                      ;
 000                      ;Scratch Pad Memory
 000                      ;
 000                      CONSTANT time_cnt_lsb_spm, 00
 000                      CONSTANT time_cnt_msb_spm, 01
 000                      CONSTANT number_disp_spm, 02                 ;   number displayed in 7_seg led
 000                      ;================================================================
 000                      ; Actual assembly program goes here...
 000                      ;================================================================
 000  00000   cold_start: LOAD s0, all_clear[00]                       ; zero out reg s0
 001  01D00               LOAD time_count_lsb[sD], s0
 002  01E00               LOAD time_count_msb[sE], s0
 003  2E000               STORE s0, time_cnt_lsb_spm[00]
 004  2E001               STORE s0, time_cnt_msb_spm[01]
 005  18001               ADD s0, 01
 006  2E002               STORE s0, number_disp_spm[02]
 007  2C005               OUTPUT s0, seg_num1[05]
 008  2C006               OUTPUT s0, seg_num2[06]
 009  2C007               OUTPUT s0, seg_num3[07]
 00A  2C008               OUTPUT s0, seg_num4[08]
 00B  3C001               ENABLE INTERRUPT                             ; enable interrupt
 00C  00000         loop: LOAD s0, all_clear[00]                       ; zero out reg s0 (nop)
 00D  1CDE8  test_1000ms: SUB time_count_lsb[sD], time_cnt_lsb_c[E8]
 00E  1EE03               SUBCY time_count_msb[sE], time_cnt_msb_c[03]
 00F  35812               JUMP C, updata_s[012]
 010  18001               ADD s0, 01
 011  3400D               JUMP test_1000ms[00D]
 012  18DE8     updata_s: ADD time_count_lsb[sD], time_cnt_lsb_c[E8]
 013  1AE03               ADDCY time_count_msb[sE], time_cnt_msb_c[03]
 014  06202               FETCH s2, number_disp_spm[02]
 015  19200               ADD s2, s0
 016  1420A     test_10s: COMPARE s2, number_turn_c[0A]
 017  35819               JUMP C, disp_num[019]
 018  1C20A               SUB s2, number_turn_c[0A]
 019  2E202     disp_num: STORE s2, number_disp_spm[02]
 01A  2C205               OUTPUT s2, seg_num1[05]
 01B  2C206               OUTPUT s2, seg_num2[06]
 01C  2C207               OUTPUT s2, seg_num3[07]
 01D  2C208               OUTPUT s2, seg_num4[08]
 01E  00600   switch_led: LOAD s6, all_clear[00]
 01F  04600               INPUT s6, switch_in[00]                      ; read data from switch port
 020  2C601               OUTPUT s6, leds_out[01]                      ; write data to led port
 021  00700   rs232_echo: LOAD s7, all_clear[00]                       ; zero out reg s1 (nop)
 022  04704               INPUT s7, data_present[04]                   ; LAB TASK #3
 023  14710               COMPARE s7, 10                               ; Write code to check if a byte has been
 024  3540C               JUMP NZ, loop[00C]                           ; received by the uart.  If so, write it
 025  04702               INPUT s7, uart_data_rx[02]                   ; back to the uart transmit port.  If not
 026  2C703               OUTPUT s7, uart_data_tx[03]                  ; don't do anything and just...
 027  3400C               JUMP loop[00C]                               ; loop again
 028  18D01          ISR: ADD time_count_lsb[sD], 01
 029  1AE00               ADDCY time_count_msb[sE], 00
 02A  38001               RETURNI ENABLE
 3FF                      ADDRESS 3FF
 3FF  34028               JUMP ISR[028]
 3FF                      ;================================================================
 3FF                      ;
 3FF                      ;================================================================

你可能感兴趣的