PROGRAMOWANIE STRUTUR CYFROWYCH PicoBlaze - projekty c Dr inż. Ignacy Pardyka UNIWERSYTET JANA OCHANOWSIEGO w ielcach 1 Rok akad. 2014/2015 1 Instytut Fizyki, Zakład Informatyki, e-mail: ignacy.pardyka@ujk.edu.pl c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 1 / 91 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 2 / 91 Program dla CPSM3 onwencje dla asemblera CPSM3 Etykiety adresowe zakończone znakiem :, np. forever: omentarz w linii po znaku ; Heksadecymalne literały liczbowe, np. FA c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 3 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 4 / 91
Program dla CPSM3 Program dla CPSM3 Program - założenia Program: dyrektywy Program ma wyznaczać wartość wyrażenia: a 2 + b 2 Wynik ma być wyświetlony na 8 diodach Wartości a, b mają być odczytane z przełączników: a: sw[7:4] b: sw[3:0] Wykorzystanie pamięci danych ; Square circuit with simple I/O interface ;Program operation: ; - read switch to a (4 MSBs) and b (4 LSBs) ; - calculate a*a + b*b ; - display data on 8 leds ; Dataconstant constant UP_NIBBLE_MAS, 0F ;00001111 ; Dataram address alias constant a_lsb, 00 ; lower byte of a constant b_lsb, 02 ; lower byte of b constant aa_lsb, 04 ; lower byte of a*a constant aa_msb, 05 ; upper byte of a*a constant bb_lsb, 06 ; lower byte of b*b constant bb_msb, 07 ; upper byte of b*b constant aabb_lsb, 08 ; lower byte of a*a + b*b constant aabb_msb, 09 ; upper byte of a*a + b*b constant aabb_cout, 0A ; carry of a*a + b*b c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 5 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 6 / 91 Program dla CPSM3 Program dla CPSM3 Program: dyrektywy Program główny ; Register alias ;commonly used local variables namereg s0, data ;reg for temporary data namereg s1, addr ;reg for temporary mem & i/o port addr namereg s2, i ;general-purpose loop index ;global variables namereg sf, sw_in ; Port alias ;------------input port definitions--------------------- constant sw_port, 01 ;8-bit switches ;------------output port definitions--------------------- constant led_port, 05 ; Main program ;Calling hierarchy: ; ;main ; - clr_data_mem ; - read_switch ; - get_upper_nibble ; - get_lower_nibble ; - square ; - mult_soft ; - write_led ; call clr_data_mem forever: call read_switch call square call write_led jump forever c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 7 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 8 / 91
Program dla CPSM3 Program dla CPSM3 ;routine: clr_data_mem ; function: clear data ram ; temp register: data, i clr_data_mem: load i, 40 ;unitize loop index to 64 load data, 00 clr_mem_loop: sub i, 01 ;dec loop index store data, (i) jump nz, clr_mem_loop ;repeat until i=0 ;routine: read switch ; function: obatin two nibbles from input ; input register: sw_in ; temp register: data read_switch: input sw_in, sw_port ;read switch input load data, sw_in call get_lower_nibble store data, a_lsb ;store a to data ram load data, sw_in call get_upper_nibble store data, b_lsb ;store b to data ram c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 9 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 10 / 91 Program dla CPSM3 Program dla CPSM3 ;routine: get_lower_nibble ; function: get lower 4-bit of data ; input register: data ; output register: data get_lower_nibble: and data, UP_NIBBLE_MAS ;clear upper nibble ;routine: get_lupper_nible ; function: get upper 4-bit of in_data ; input register: data ; output register: data get_upper_nibble: sr0 data ;right shift 4 times sr0 data sr0 data sr0 data ;routine: write_led ; function: output 8 LSBs of result to 8 leds ; temp register: data write_led: fetch data, aabb_lsb output data, led_port c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 11 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 12 / 91
Program dla CPSM3 Program dla CPSM3 ;routine: square ; function: calculate a*a + b*b ; data/result stored in ram started w/ SQ_BASE_ADDR ; temp register: s3, s4, s5, s6, data square: ;calculate a*a fetch s3, a_lsb ;load a fetch s4, a_lsb ;load a call mult_soft ;calculate a*a store s6, aa_lsb ;store lower byte of a*a store s5, aa_msb ;store upper byte of a*a ;calculate b*b fetch s3, b_lsb ;load b fetch s4, b_lsb ;load b call mult_soft ;calculate b*b store s6, bb_lsb ;store lower byte of b*b store s5, 07 ;store upper byte of b*b ;calculate a*a+b*b fetch data, aa_lsb ;get lower byte of a*a add data, s6 ;add lower byte of a*a+b*b store data, aabb_lsb ;store lower byte of a*a+b*b fetch data, aa_msb ;get upper byte of a*a addcy data, s5 ;add upper byte of a*a+b*b store data, aabb_msb ;store upper byte of a*a+b*b load data, 00 ;clear data, but keep carry addcy data, 00 ;get carry-out from previous + store data, aabb_cout ;store carry-out of a*a+b*b c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 13 / 91 ;routine: mult_soft ; function: 8-bit unsigned multiplier using ; shift-and-add algorithm ; input register: ; s3: multiplicand ; s4: multiplier ; output register: ; s5: upper byte of product ; s6: lower byte of product ; temp register: i mult_soft: load s5, 00 ;clear s5 load i, 08 ;initialize loop index mult_loop: sr0 s4 ;shift lsb to carry jump nc, shift_prod ;lsb is 0 add s5, s3 ;lsb is 1 shift_prod: sra s5 ;shift upper byte right, ;carry to MSB, LSB to carry sra s6 ;shift lower byte right, ;lsb of s5 to MSB of s6 sub i, 01 ;dec loop index jump nz, mult_loop ;repeat until i=0 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 14 / 91 Program dla CPSM3 Program dla CPSM3 Symulacja w pblaze IDE Różnice: CPSM3 - pblaze c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 15 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 16 / 91
Program dla CPSM3 Program dla CPSM3 Różnice: CPSM3 - pblaze Różnice: CPSM3 - pblaze c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 17 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 18 / 91 module pico_sio ( input wire clk, reset, input wire [7:0] sw, output wire [7:0] Led ); // signal declaration // CPSM3/ROM signals wire [9:0] address; wire [17:0] instruction; wire [7:0] port_id, in_port, out_port; wire write_strobe; // register signals reg [7:0] led_reg; // CPSM and ROM instantiation kcpsm3 proc_unit (.clk(clk),.reset(reset),.address(address),.instruction(instruction),.port_id(),.write_strobe(write_strobe),.out_port(out_port),.read_strobe(),.in_port(in_port),.interrupt(1 b0),.interrupt_ack()); sio_rom rom_unit (.clk(clk),.address(address),.instruction(instruction)); c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 19 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 20 / 91
Testbench timescale 1ns / 1ps module pico_sio_tb; // output interface always @(posedge clk) if (write_strobe) led_reg <= out_port; assign Led = led_reg; // input interface assign in_port = sw; endmodule reg clk, reset; reg [7:0] sw; wire [7:0] Led; localparam T = 20; pico_sio uut (.clk(clk),.reset(reset),.sw(sw),.led(led) ); always begin #(T/2) clk <= ~clk; end initial begin clk = 0; reset = 1; #(T) reset = 0; sw[7:4] = 4 b0100; // a sw[3:0] = 4 b0011; // b end endmodule c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 21 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 22 / 91 Symulacja w Xilinx ISE Symulacja w Xilinx ISE c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 23 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 24 / 91
Symulacja w Xilinx ISE Implementacja w FPGA NET "clk" NET "reset" NET "Led<0>" NET "Led<1>" NET "Led<2>" NET "Led<3>" NET "Led<4>" NET "Led<5>" NET "Led<6>" NET "Led<7>" NET "sw<0>" NET "sw<1>" NET "sw<2>" NET "sw<3>" NET "sw<4>" NET "sw<5>" NET "sw<6>" NET "sw<7>" LOC = "B8"; LOC = "H13"; LOC = "J14"; LOC = "J15"; LOC = "15"; LOC = "14"; LOC = "E17"; LOC = "P15"; LOC = "F4"; LOC = "R4"; LOC = "G18"; LOC = "H18"; LOC = "18"; LOC = "17"; LOC = "L14"; LOC = "L13"; LOC = "N17"; LOC = "R17"; c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 25 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 26 / 91 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 27 / 91 Wejścia c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 28 / 91
Wyjścia Dekoder adresów wyjść c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 29 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 30 / 91 Dekoder adresów wyjść w HDL Porty wyjść w HDL // decoding circuit for enable signals always @* if (write_strobe) case (port_id[1:0]) 2 b00: en_d = 4 b0001; 2 b01: en_d = 4 b0010; 2 b10: en_d = 4 b0100; 2 b11: en_d = 4 b1000; endcase else en_d = 4 b0000; // outport port id: // 0x00: ds0 // 0x01: ds1 // 0x02: ds2 // 0x03: ds3 // registers always @(posedge clk) begin if (en_d[0]) ds0_reg <= out_port; if (en_d[1]) ds1_reg <= out_port; if (en_d[2]) ds2_reg <= out_port; if (en_d[3]) ds3_reg <= out_port; end c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 31 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 32 / 91
Urządzenia wejściowe i wyjściowe na płycie Nexys2 Program: dyrektywy Program dla CPSM3 Przełączniki ustawiające 8-bitowe wartości: a, b Przycisk button 0: zerowanie pamięci danych Przycisk button 1: zapis wartości (a, b) - ustawianie flagi Wskaźniki 7-segmentowe (dp, 4 x 4 bit): 17-bit wynik hex ; Square circuit with 7-seg LED interface ;Program operation: ; - read a and b from switch ; - calculate a*a + b*b ; - display data on 7-seg led ; Data ram address alias constant a_lsb, 00 constant b_lsb, 02 constant aa_lsb, 04 constant aa_msb, 05 constant bb_lsb, 06 constant bb_msb, 07 constant aabb_lsb, 08 constant aabb_msb, 09 constant aabb_cout, 0A constant led0, 10 constant led1, 11 constant led2, 12 constant led3, 13 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 33 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 34 / 91 Program dla CPSM3 Program dla CPSM3 Program: dyrektywy Program główny ; Register alias ;commonly used local variables namereg s0, data ;reg for temporary data namereg s1, addr ;reg for temporary mem & i/o port addr namereg s2, i ;general-purpose loop index ;global variables namereg sf, switch_a_b ;ram offset for current switch input ; Port alias ;------------input port definitions--------------------- constant rd_flag_port, 00 ;2 flags (xxxxxxsc): constant sw_port, 01 ;8-bit switch ;------------output port definitions--------------------- constant sseg0_port, 00 ;7-seg led 0 constant sseg1_port, 01 ;7-seg led 1 constant sseg2_port, 02 ;7-seg led 2 constant sseg3_port, 03 ;7-seg led 3 ; Main program ;Calling hierarchy: ; ;main ; - init ; - proc_btn ; - init ; - square ; - mult_soft ; - load_led_pttn ; - get_lower_nibble ; - get_upper_nibble ; - hex_to_led ; - disp_led ; ; ========================================================= c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 35 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 36 / 91
Program dla CPSM3 Program dla CPSM3 Program główny call init forever: ;main loop body call proc_btn call square call load_led_pttn call disp_led jump forever ;initialization ;check & process buttons ;calculate square ;store led patterns to ram ;output led pattern ;routine: init ; function: perform initialization, clear register/ram ; output register: ; switch_a_b: cleared to 0 ; temp register: data, i init: ;clear memory load i, 40 ;unitize loop index to 64 load data, 00 clr_mem_loop: store data, (i) sub i, 01 ;dec loop index jump nz, clr_mem_loop ;repeat until i=0 ;clear register load switch_a_b, 00 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 37 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 38 / 91 Program dla CPSM3 Program dla CPSM3 ;routine: proc_btn ; function: check two buttons and process the display ; input reg: ; switch_a_b: ram offset (0 for a and 2 for b) ; output register: ; s3: store input port flag ; switch_a_b: may be toggled ; temp register used: data, addr proc_btn: input s3, rd_flag_port ;get flag ;check and process c button test s3, 01 ;check c button flag jump z, chk_btns ;flag not set call init ;flag set, clear jump proc_btn_done chk_btns: ;check and process s button test s3, 02 ;check s button flag jump z, proc_btn_done ;flag not set input data, sw_port ;get switch load addr, a_lsb ;get addr of a add addr, switch_a_b ;add offset store data, (addr) ;write data to ram ;update current disp position xor switch_a_b, 02 ;toggle between 00, 02 proc_btn_done: routine: load_led_pttn ; function: read 3 LSBs of switch input and convert the ; desired values to four led patterns and ; load them to ram ; switch: 000:a; 001:b; 010:a^2; 011:b^2; ; others: a^2 + b^2 ; tmp register used: data, addr ; s6: data from sw input port load_led_pttn: input s6, sw_port ;get switch sl0 s6 ;*2 to obtain addr offset compare s6, 08 ;sw>100? jump c, sw_ok ;no load s6, 08 ;yes, sw error, make default sw_ok: ;process byte 0, lower nibble load addr, a_lsb add addr, s6 ;get lower addr fetch data, (s6) ;get lower byte call get_lower_nibble ;get lower nibble call hex_to_led ;convert to led pattern store data, led0 ;process byte 0, upper nibble fetch data, (addr) call get_upper_nibble c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 39 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 40 / 91
Program dla CPSM3 Program dla CPSM3 call hex_to_led store data, led1 ;process byte 1, lower nibble add addr, 01 ;get upper addr fetch data, (addr) call get_lower_nibble call hex_to_led store data, led2 ;process byte 1, upper nibble fetch data, (addr) call get_upper_nibble call hex_to_led ;check for sw=100 to process carry as led dp compare s6, 08 ;display final result? jump nz, led_done ;no add addr, 01 ;get carry addr fetch s6, (addr) ;s6 to store carry test s6, 01 ;carry=1? jump z, led_done ;no and data, 7F ;yes, assert msb (dp) to 0 led_done: store data, led3 ;routine: disp_led ; function: output four led patterns ; tmp register used: data disp_led: fetch data, led0 output data, sseg0_port fetch data, led1 output data, sseg1_port fetch data, led2 output data, sseg2_port fetch data, led3 output data, sseg3_port c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 41 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 42 / 91 Program dla CPSM3 Program dla CPSM3 ;routine: hex_to_led ; function: convert a hex digit to 7-seg led pattern ; input register: data ; output register: data hex_to_led: compare data, 00 jump nz, comp_hex_1 load data, 81 ;7seg pattern 0 comp_hex_1: compare data, 01 jump nz, comp_hex_2 load data, CF ;7seg pattern 1 comp_hex_2: compare data, 02 jump nz, comp_hex_3 load data, 92 ;7seg pattern 2 comp_hex_3: compare data, 03 jump nz, comp_hex_4 load data, 86 ;7seg pattern 3 comp_hex_4: compare data, 04 jump nz, comp_hex_5 load data, CC ;7seg pattern 4 comp_hex_5: compare data, 05 jump nz, comp_hex_6 load data, A4 ;7seg pattern 5 comp_hex_6: compare data, 06 jump nz, comp_hex_7 load data, A0 ;7seg pattern 6 comp_hex_7: compare data, 07 jump nz, comp_hex_8 load data, 8F ;7seg pattern 7 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 43 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 44 / 91
Program dla CPSM3 Program dla CPSM3 comp_hex_8: compare data, 08 jump nz, comp_hex_9 load data, 80 ;7seg pattern 8 comp_hex_9: compare data, 09 jump nz, comp_hex_a load data, 84 ;7seg pattern 9 comp_hex_a: compare data, 0A jump nz, comp_hex_b load data, 88 ;7seg pattern a comp_hex_b: compare data, 0B jump nz, comp_hex_c load data, E0 ;7seg pattern b comp_hex_c: compare data, 0C jump nz, comp_hex_d load data, B1 comp_hex_d: compare data, 0D jump nz, comp_hex_e load data, C2 comp_hex_e: compare data, 0E jump nz, comp_hex_f load data, B0 comp_hex_f: load data, B8 hex_done: ;7seg pattern C ;7seg pattern d ;7seg pattern E ;7seg pattern f c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 45 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 46 / 91 Program dla CPSM3 Program dla CPSM3 ;routine: get_lower_nibble ; function: get lower 4-bit of data ; input register: data ; output register: data get_lower_nibble: and data, 0F ;clear upper nibble ;routine: get_lupper_nible ; function: get upper 4-bit of in_data ; input register: data ; output register: data get_upper_nibble: sr0 data ;right shift 4 times sr0 data sr0 data sr0 data routine: square ; function: calculate a*a + b*b ; data/result stored in ram started w/ SQ_BASE_ADDR ; temp register: s3, s4, s5, s6, data square: ;calculate a*a fetch s3, a_lsb ;load a fetch s4, a_lsb ;load a call mult_soft ;calculate a*a store s6, aa_lsb ;store lower byte of a*a store s5, aa_msb ;store upper byte of a*a ;calculate b*b fetch s3, b_lsb ;load b fetch s4, b_lsb ;load b call mult_soft ;calculate b*b store s6, bb_lsb ;store lower byte of b*b store s5, bb_msb ;store upper byte of b*b c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 47 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 48 / 91
Program dla CPSM3 Program dla CPSM3 ;calculate a*a+b*b fetch data, aa_lsb ;get lower byte of a*a add data, s6 ;add lower byte of a*a+b*b store data, aabb_lsb ;store lower byte of a*a+b*b fetch data, aa_msb ;get upper byte of a*a addcy data, s5 ;add upper byte of a*a+b*b store data, aabb_msb ;store upper byte of a*a+b*b load data, 00 ;clear data, but keep carry addcy data, 00 ;get carry from previous + store data, aabb_cout ;store carry of a*a+b*b routine: mult_soft ; function: 8-bit unsigned multiplier using ; shift-and-add algorithm ; input register: ; s3: multiplicand ; s4: multiplier ; output register: ; s5: upper byte of product ; s6: lower byte of product ; temp register: i mult_soft: load s5, 00 ;clear s5 load i, 08 ;initialize loop index mult_loop: sr0 s4 ;shift lsb to carry jump nc, shift_prod ;lsb is 0 add s5, s3 ;lsb is 1 shift_prod: sra s5 ;shift upper byte right, ;carry to MSB, LSB to carry sra s6 ;shift lower byte right, ;lsb of s5 to MSB of s6 sub i, 01 ;dec loop index jump nz, mult_loop ;repeat until i=0 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 49 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 50 / 91 module pico_btn ( input wire clk, reset, input wire [7:0] sw, input wire [1:0] btn, output wire [3:0] an, output wire [7:0] sseg ); // signal declaration // CPSM3/ROM signals wire [9:0] address; wire [17:0] instruction; wire [7:0] port_id, out_port; reg [7:0] in_port; wire write_strobe, read_strobe; // I/O port signals // output enable reg [3:0] en_d; // four-digit seven-segment led display reg [7:0] ds3_reg, ds2_reg, ds1_reg, ds0_reg; // two pushbuttons reg btnc_flag_reg, btns_flag_reg; wire btnc_flag_next, btns_flag_next; wire set_btnc_flag, set_btns_flag, clr_btn_flag; // I/O modules disp_mux disp_unit (.clk(clk),.reset(reset),.in3(ds3_reg),.in2(ds2_reg),.in1(ds1_reg),.in0(ds0_reg),.an(an),.sseg(sseg)); debounce btnc_unit (.clk(clk),.reset(reset),.sw(btn[0]),.db_level(),.db_tick(set_btnc_flag)); debounce btns_unit (.clk(clk),.reset(reset),.sw(btn[1]),.db_level(),.db_tick(set_btns_flag)); // CPSM and ROM instantiation kcpsm3 proc_unit (.clk(clk),.reset(1 b0),.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(1 b0),.interrupt_ack()); btn_rom rom_unit (.clk(clk),.address(address),.instruction(instruction)); c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 51 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 52 / 91
// output interface // outport port id: // 0x00: ds0 // 0x01: ds1 // 0x02: ds2 // 0x03: ds3 // registers always @(posedge clk) begin if (en_d[0]) ds0_reg <= out_port; if (en_d[1]) ds1_reg <= out_port; if (en_d[2]) ds2_reg <= out_port; if (en_d[3]) ds3_reg <= out_port; end // decoding circuit for enable signals always @* if (write_strobe) case (port_id[1:0]) 2 b00: en_d = 4 b0001; 2 b01: en_d = 4 b0010; 2 b10: en_d = 4 b0100; 2 b11: en_d = 4 b1000; endcase else en_d = 4 b0000; c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 53 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 54 / 91 // input interface // input port id // 0x00: flag // 0x01: switch // input register (for flags) always @(posedge clk) begin btnc_flag_reg <= btnc_flag_next; btns_flag_reg <= btns_flag_next; end assign btnc_flag_next = (set_btnc_flag)? 1 b1 : (clr_btn_flag)? 1 b0 : btnc_flag_reg; assign btns_flag_next = (set_btns_flag)? 1 b1 : (clr_btn_flag)? 1 b0 : btns_flag_reg; // decoding circuit for clear signals assign clr_btn_flag = read_strobe && (port_id[0]==1 b0); // input multiplexing always @* case(port_id[0]) 1 b0: in_port = {6 b0, btns_flag_reg, btnc_flag_reg}; 1 b1: in_port = sw; endcase endmodule c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 55 / 91 z przerwaniami 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 56 / 91
z przerwaniami z przerwaniami Jedno źródło przerwań Dwa źródła przerwań c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 57 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 58 / 91 z przerwaniami System z przerwaniem cyklicznym Obsługa przerwania z przerwaniami Program dla CPSM3 ;routine: interrupt service routine ; function: increment 16-bit counter ; input register: ; count_msb, count_lsb: timer count ; output register: ; count_msb, count_lsb: incremented int_service_routine: add count_lsb, 01 ;inc 16-bit counter addcy count_msb, 00 i enable ;interrupt vector address 3FF jump int_service_routine c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 59 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 60 / 91
z przerwaniami Program dla CPSM3 z przerwaniami Program dla CPSM3 Alisy rejestrów Alisy portów ;commonly used local variables namereg s0, data ;reg for temporary data namereg s1, addr ;reg for temporary mem & i/o port addr namereg s2, i ;general-purpose loop index ;global variables namereg sc, switch_a_b ;ram offset for current switch input namereg sb, led_pos ;led disp position (0, 1, 2 or 3) namereg se, count_msb ;timer tick count 8 MSBs namereg sf, count_lsb ;timer tick count 8 LSBs ; Port alias ;------------input port definitions--------------------- constant rd_flag_port, 00 ; 2 flags (xxxxxxsc): constant sw_port, 01 ;8-bit switches ;------------output port definitions--------------------- constant an_port, 00 constant sseg_port, 01 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 61 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 62 / 91 z przerwaniami Program dla CPSM3 z przerwaniami Program dla CPSM3 Program główny : init ;main ; - init ; - proc_btn ; - init ; - proc_uart ; - square ; - mult_soft ; - load_led_pttn ; - get_lower_nibble ; - get_upper_nibble ; - hex_to_led ; - display_mux_out ; ========================================================= call init ;initialization forever: ;main loop body call proc_btn ;check & process buttons call square ;calculate square call load_led_pttn ;store led patterns to ram call display_mux_out ; multiplex led patterns jump forever ;routine: init ; function: perform initialization, clear register/ram ; output register: ; switch_a_b: cleared to 0 ; temp register: data, i init: enable interrupt ;clear memory load i, 40 ;unitize loop index to 64 load data, 00 clr_mem_loop: store data, (i) sub i, 01 ;dec loop index jump nz, clr_mem_loop ;repeat until i=0 ;clear registers load switch_a_b, 00 load led_pos, 00 load count_msb, 00 load count_lsb, 00 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 63 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 64 / 91
z przerwaniami Program dla CPSM3 : sterowanie wyświetlaczami z przerwaniami Program dla CPSM3 : sterowanie wyświetlaczami ;routine: display_mux_out ; function: generate enable pulse & led pattern ; for 4-digit 7-segment led display ; input register: ; count_msb, count_lsb: timer count ; led_pos: current led position ; output register: ; led_pos: updated led position ; tmp register: data, addr display_mux_out: compare count_msb, 02 ;count=00000100_00000000 jump c, mux_out_done ;clear time counter (count > 20) load count_lsb, 00 load count_msb, 00 ;update 7-segment led position add led_pos, 01 compare led_pos, 04 jump nz, gen_an_signal load led_pos, 00 ;led_pos wraps around gen_an_signal: ;generate 4-bit anode enable signal load data, 0E ;xxxx_1110 compare led_pos, 00 jump z, shift_an_0 compare led_pos, 01 jump z, shift_an_1 compare led_pos, 02 jump z, shift_an_2 sl1 data ;shift 1110 3 times shift_an_2: sl1 data ;shift 1110 2 times shift_an_1: sl1 data ;shift 1110 1 times shift_an_0: output data, an_port ;output 7-seg led pattern load addr, led0 add addr, led_pos fetch data, (addr) output data, sseg_port mux_out_done: c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 65 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 66 / 91 z przerwaniami Program dla CPSM3 Symulacja programu w środowisku pblaze IDE z przerwaniami Program dla CPSM3 Symulacja programu w środowisku pblaze IDE c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 67 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 68 / 91
z przerwaniami z przerwaniami module pico_int ( input wire clk, reset, input wire [7:0] sw, input wire [1:0] btn, output wire [3:0] an, output wire [7:0] sseg ); // signal declaration // CPSM3/ROM signals wire [9:0] address; wire [17:0] instruction; wire [7:0] port_id, out_port; reg [7:0] in_port; wire write_strobe, read_strobe; wire interrupt, interrupt_ack; // I/O port signals // output enable reg [1:0] en_d; // four-digit seven-segment led display reg [7:0] sseg_reg; reg [3:0] an_reg; // two pushbuttons reg btnc_flag_reg, btns_flag_reg; wire btnc_flag_next, btns_flag_next; wire set_btnc_flag, set_btns_flag, clr_btn_flag; // interrupt related signals reg [8:0] timer_reg; wire [8:0] timer_next; wire ten_us_tick; reg timer_flag_reg; wire timer_flag_next; c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 69 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 70 / 91 z przerwaniami z przerwaniami // I/O modules debounce btnc_unit (.clk(clk),.reset(reset),.sw(btn[0]),.db_level(),.db_tick(set_btnc_flag)); debounce btns_unit (.clk(clk),.reset(reset),.sw(btn[1]),.db_level(),.db_tick(set_btns_flag)); // CPSM and ROM instantiation kcpsm3 proc_unit (.clk(clk),.reset(1 b0),.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)); int_rom rom_unit (.clk(clk),.address(address),.instruction(instruction)); // output interface // outport port id: // 0x00: an // 0x01: ssg // registers always @(posedge clk) begin if (en_d[0]) an_reg <= out_port[3:0]; if (en_d[1]) sseg_reg <= out_port; end assign an = an_reg; assign sseg = sseg_reg; // decoding circuit for enable signals always @* if (write_strobe) case (port_id[0]) 1 b0: en_d = 2 b01; 1 b1: en_d = 2 b10; endcase else en_d = 2 b00; c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 71 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 72 / 91
z przerwaniami z przerwaniami // input interface // input port id // 0x00: flag // 0x01: switch // input register (for flags) always @(posedge clk) begin btnc_flag_reg <= btnc_flag_next; btns_flag_reg <= btns_flag_next; end assign btnc_flag_next = (set_btnc_flag)? 1 b1 : (clr_btn_flag)? 1 b0 : btnc_flag_reg; assign btns_flag_next = (set_btns_flag)? 1 b1 : (clr_btn_flag)? 1 b0 : btns_flag_reg; // decoding circuit for clear signals assign clr_btn_flag = read_strobe && (port_id[0]==1 b0); // input multiplexing always @* case(port_id[0]) 1 b0: in_port = {6 b0, btns_flag_reg, btnc_flag_reg}; 1 b1: in_port = sw; endcase // interrupt interface // 10 us counter always @(posedge clk) timer_reg <= timer_next; assign ten_us_tick = (timer_reg==499); assign timer_next = ten_us_tick? 0 : timer_reg + 1; // 10 us tick flag always @(posedge clk) timer_flag_reg <= timer_flag_next; assign timer_flag_next = (ten_us_tick)? 1 b1 : (interrupt_ack)? 1 b0 : timer_flag_reg; // interrupt request assign interrupt = timer_flag_reg; endmodule c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 73 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 74 / 91 z przerwaniami z przerwaniami Implementacja w FPGA Implementacja w FPGA c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 75 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 76 / 91
z przerwaniami z przerwaniami Implementacja w FPGA Implementacja w FPGA c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 77 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 78 / 91 z przerwaniami z przerwaniami Implementacja w FPGA Implementacja w FPGA c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 79 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 80 / 91
z przerwaniami z przerwaniami Implementacja w FPGA Implementacja w FPGA c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 81 / 91 c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 82 / 91 z przerwaniami Implementacja w FPGA c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 83 / 91 Interfejsy standardowe 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 84 / 91
Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 85 / 91 Interfejsy standardowe FPGA: Interfejs RS232 i zastosowania 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 86 / 91 Interfejsy standardowe FPGA: Interfejs VGA i zastosowania 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 87 / 91 Interfejsy standardowe FPGA: Interfejs PS/2 - klawiatura uniwersalna 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 88 / 91
Interfejsy standardowe FPGA: Interfejs PS/2 - mysz 1 Program dla CPSM3 2 Program dla CPSM3 3 z przerwaniami Program dla CPSM3 4 Interfejsy standardowe FPGA: omunikacja PC z Nexys2 przez USB i zastosowania FPGA: Interfejs RS232 i zastosowania FPGA: Interfejs VGA i zastosowania FPGA: Interfejs PS/2 - klawiatura uniwersalna FPGA: Interfejs PS/2 - mysz 5 Literatura c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 89 / 91 cdn. Interfejsy standardowe cdn. FPGA: Interfejs PS/2 - mysz c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 90 / 91 Literatura Literatura D.M. Harris, S.L. Harris, Digital Design and Computer Architecture, Elsevier, 2013. Z. Hajduk, Wprowadzenie do języka Verilog, BTC, 2009. M. Pawłowski, A. Skorupski, Projektowanie złożonych układów cyfrowych, WiŁ, 2010. P. Pong. Chu, FPGA Prototyping by Verilog examples, Wiley, 2008. IEEE Standard Verilog R Hardware Description Language, IEEE, 2001. Xilinx ISE WebPack: http://www.xilinx.com. Digilent Nexys2: http://www.digilentinc.com. OpenCores: http://www.opencores.org. c Dr inż. Ignacy Pardyka (Inf U J ) programowanie struktur cyfrowych Rok akad. 2014/2015 91 / 91