├── .gitignore ├── LICENSE ├── README.md ├── presentation ├── FPGA 101 Workshop - Hackaday Belgrade 2018.pdf └── FPGA 101 Workshop - Hackaday Belgrade 2018.pptx ├── projects └── galaksija │ ├── apio.ini │ ├── font.py │ ├── font_rom.v │ ├── gal.vid │ ├── galchr.mem │ ├── galrom1.bin.mem │ ├── galrom2.bin.mem │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── prebuilt │ ├── Makefile │ └── hardware.bin │ ├── ram_memory.v │ ├── rom_memory.v │ ├── top.v │ ├── tv80_alu.v │ ├── tv80_core.v │ ├── tv80_mcode.v │ ├── tv80_reg.v │ ├── tv80n.v │ ├── uart_rx.v │ └── video.v ├── tests ├── led │ ├── apio.ini │ ├── led.v │ ├── led_tb.v │ └── pinout.pcf └── riscv │ ├── Makefile │ ├── firmware.c │ ├── sections.c │ ├── sections.lds │ └── start.s └── tutorials ├── 00-IceStudio ├── led.ice └── trigger.ice ├── 01-Basics ├── apio.ini ├── led.v └── pinout.pcf ├── 02-LED ├── apio.ini ├── pinout.pcf ├── rgb.v ├── rgb_tb.gtkw └── rgb_tb.v ├── 03-Buttons ├── apio.ini ├── button_rgb.v └── pinout.pcf ├── 04-Audio ├── apio.ini ├── audio.v └── pinout.pcf ├── 05-Video ├── 00-Basic │ ├── apio.ini │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ └── video.v └── 01-KeyTest │ ├── apio.ini │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ └── video.v ├── 06-StateMachine ├── apio.ini ├── lcd_pll.v ├── pinout.pcf ├── top.v └── video.v ├── 07-Memory ├── 00-Basic │ ├── apio.ini │ ├── font_rom.v │ ├── font_vga.mem │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ └── video.v ├── 01-Buffer │ ├── apio.ini │ ├── font_rom.v │ ├── font_vga.mem │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── text.dat │ ├── text.mem │ ├── text.py │ ├── text_rom.v │ ├── top.v │ └── video.v └── 02-Terminal │ ├── apio.ini │ ├── font_rom.v │ ├── font_vga.mem │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ ├── uart_rx.v │ └── video.v ├── 08-MovingSprite ├── apio.ini ├── lcd_pll.v ├── pinout.pcf ├── sprite.mem ├── sprite_rom.v ├── top.v └── video.v ├── 09-Pong ├── 00-Move │ ├── apio.ini │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ └── video.v ├── 01-Ball │ ├── apio.ini │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ └── video.v ├── 02-Game │ ├── apio.ini │ ├── lcd_pll.v │ ├── numbers.list │ ├── pinout.pcf │ ├── top.v │ └── video.v └── 03-Wall │ ├── apio.ini │ ├── lcd_pll.v │ ├── pinout.pcf │ ├── top.v │ └── video.v ├── 10-CPU ├── README.md ├── alu.v ├── apio.ini ├── boot.mem ├── grom_computer.v ├── grom_computer_tb.gtkw ├── grom_computer_tb.v ├── grom_cpu.v ├── grom_top.v ├── pinout.pcf └── ram_memory.v ├── 11-Computer ├── altair.v ├── apio.ini ├── basic4k32.bin.mem ├── board.pcf ├── i8080.v ├── jmp_boot.v ├── mc6850.v ├── ram_memory.v ├── rom.py ├── rom_memory.v ├── simpleuart.v ├── top_altair.v └── turnmon.bin.mem └── 12-RiscV ├── 01-Simple ├── Makefile ├── firmware.c ├── sections.c ├── sections.lds └── start.s ├── 02-Micropython ├── LICENSE ├── README.md ├── docs │ └── conf.py ├── drivers │ ├── README.md │ └── bus │ │ ├── qspi.h │ │ ├── softqspi.c │ │ ├── softspi.c │ │ └── spi.h ├── extmod │ ├── machine_i2c.c │ ├── machine_i2c.h │ ├── machine_mem.c │ ├── machine_mem.h │ ├── machine_pinbase.c │ ├── machine_pinbase.h │ ├── machine_pulse.c │ ├── machine_pulse.h │ ├── machine_signal.c │ ├── machine_signal.h │ ├── machine_spi.c │ ├── machine_spi.h │ ├── misc.h │ ├── modbtree.c │ ├── modframebuf.c │ ├── modlwip.c │ ├── modonewire.c │ ├── modubinascii.c │ ├── modubinascii.h │ ├── moductypes.c │ ├── moduhashlib.c │ ├── moduheapq.c │ ├── modujson.c │ ├── modurandom.c │ ├── modure.c │ ├── moduselect.c │ ├── modussl_axtls.c │ ├── modussl_mbedtls.c │ ├── modutimeq.c │ ├── moduzlib.c │ ├── modwebrepl.c │ ├── modwebsocket.c │ ├── modwebsocket.h │ ├── uos_dupterm.c │ ├── utime_mphal.c │ ├── utime_mphal.h │ ├── vfs.c │ ├── vfs.h │ ├── vfs_fat.c │ ├── vfs_fat.h │ ├── vfs_fat_diskio.c │ ├── vfs_fat_file.c │ ├── vfs_reader.c │ ├── virtpin.c │ └── virtpin.h ├── lib │ ├── README.md │ ├── embed │ │ └── abort_.c │ ├── mp-readline │ │ ├── readline.c │ │ └── readline.h │ └── utils │ │ ├── interrupt_char.c │ │ ├── interrupt_char.h │ │ ├── printf.c │ │ ├── pyexec.c │ │ ├── pyexec.h │ │ ├── stdout_helpers.c │ │ └── sys_stdio_mphal.c ├── ports │ └── picorv32 │ │ ├── .gitignore │ │ ├── Makefile │ │ ├── board.c │ │ ├── board.h │ │ ├── main.c │ │ ├── modpyb.c │ │ ├── modpyb.h │ │ ├── modpyblcd.c │ │ ├── modpybled.c │ │ ├── modpybswitch.c │ │ ├── mpconfigport.h │ │ ├── mphalport.h │ │ ├── qstrdefsport.h │ │ ├── sections.c │ │ ├── sections.lds │ │ └── start.s └── py │ ├── __pycache__ │ └── makeqstrdata.cpython-35.pyc │ ├── argcheck.c │ ├── asmarm.c │ ├── asmarm.h │ ├── asmbase.c │ ├── asmbase.h │ ├── asmthumb.c │ ├── asmthumb.h │ ├── asmx64.c │ ├── asmx64.h │ ├── asmx86.c │ ├── asmx86.h │ ├── asmxtensa.c │ ├── asmxtensa.h │ ├── bc.c │ ├── bc.h │ ├── bc0.h │ ├── binary.c │ ├── binary.h │ ├── builtin.h │ ├── builtinevex.c │ ├── builtinhelp.c │ ├── builtinimport.c │ ├── compile.c │ ├── compile.h │ ├── emit.h │ ├── emitbc.c │ ├── emitcommon.c │ ├── emitglue.c │ ├── emitglue.h │ ├── emitinlinethumb.c │ ├── emitinlinextensa.c │ ├── emitnative.c │ ├── formatfloat.c │ ├── formatfloat.h │ ├── frozenmod.c │ ├── frozenmod.h │ ├── gc.c │ ├── gc.h │ ├── grammar.h │ ├── lexer.c │ ├── lexer.h │ ├── makeqstrdata.py │ ├── makeqstrdata.pyc │ ├── makeqstrdefs.py │ ├── makeversionhdr.py │ ├── malloc.c │ ├── map.c │ ├── misc.h │ ├── mkenv.mk │ ├── mkrules.mk │ ├── modarray.c │ ├── modbuiltins.c │ ├── modcmath.c │ ├── modcollections.c │ ├── modgc.c │ ├── modio.c │ ├── modmath.c │ ├── modmicropython.c │ ├── modstruct.c │ ├── modsys.c │ ├── modthread.c │ ├── moduerrno.c │ ├── mpconfig.h │ ├── mperrno.h │ ├── mphal.h │ ├── mpprint.c │ ├── mpprint.h │ ├── mpstate.c │ ├── mpstate.h │ ├── mpthread.h │ ├── mpz.c │ ├── mpz.h │ ├── nativeglue.c │ ├── nlr.c │ ├── nlr.h │ ├── nlrsetjmp.c │ ├── nlrthumb.c │ ├── nlrx64.c │ ├── nlrx86.c │ ├── nlrxtensa.c │ ├── obj.c │ ├── obj.h │ ├── objarray.c │ ├── objarray.h │ ├── objattrtuple.c │ ├── objbool.c │ ├── objboundmeth.c │ ├── objcell.c │ ├── objclosure.c │ ├── objcomplex.c │ ├── objdeque.c │ ├── objdict.c │ ├── objenumerate.c │ ├── objexcept.c │ ├── objexcept.h │ ├── objfilter.c │ ├── objfloat.c │ ├── objfun.c │ ├── objfun.h │ ├── objgenerator.c │ ├── objgenerator.h │ ├── objgetitemiter.c │ ├── objint.c │ ├── objint.h │ ├── objint_longlong.c │ ├── objint_mpz.c │ ├── objlist.c │ ├── objlist.h │ ├── objmap.c │ ├── objmodule.c │ ├── objmodule.h │ ├── objnamedtuple.c │ ├── objnamedtuple.h │ ├── objnone.c │ ├── objobject.c │ ├── objpolyiter.c │ ├── objproperty.c │ ├── objrange.c │ ├── objreversed.c │ ├── objset.c │ ├── objsingleton.c │ ├── objslice.c │ ├── objstr.c │ ├── objstr.h │ ├── objstringio.c │ ├── objstringio.h │ ├── objstrunicode.c │ ├── objtuple.c │ ├── objtuple.h │ ├── objtype.c │ ├── objtype.h │ ├── objzip.c │ ├── opmethods.c │ ├── parse.c │ ├── parse.h │ ├── parsenum.c │ ├── parsenum.h │ ├── parsenumbase.c │ ├── parsenumbase.h │ ├── persistentcode.c │ ├── persistentcode.h │ ├── py.mk │ ├── pystack.c │ ├── pystack.h │ ├── qstr.c │ ├── qstr.h │ ├── qstrdefs.h │ ├── reader.c │ ├── reader.h │ ├── repl.c │ ├── repl.h │ ├── ringbuf.h │ ├── runtime.c │ ├── runtime.h │ ├── runtime0.h │ ├── runtime_utils.c │ ├── scheduler.c │ ├── scope.c │ ├── scope.h │ ├── sequence.c │ ├── showbc.c │ ├── smallint.c │ ├── smallint.h │ ├── stackctrl.c │ ├── stackctrl.h │ ├── stream.c │ ├── stream.h │ ├── unicode.c │ ├── unicode.h │ ├── vm.c │ ├── vmentrytable.h │ ├── vstr.c │ └── warning.c ├── apio.ini ├── font_rom.v ├── font_vga.mem ├── lcd_pll.v ├── picosoc.v ├── pinout.pcf ├── prebuilt ├── Makefile ├── firmware.bin └── picosoc.bin ├── simpleuart.v ├── spimemio.v ├── text_rom.v ├── top.v ├── up_spram.v ├── video.v └── z_picorv32.v /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | *.dblite 3 | *.asc 4 | *.bin 5 | *.blif 6 | *.out 7 | *.vcd 8 | *.elf 9 | *.json 10 | *.rpt 11 | abc.history -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Miodrag Milanović 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /presentation/FPGA 101 Workshop - Hackaday Belgrade 2018.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/presentation/FPGA 101 Workshop - Hackaday Belgrade 2018.pdf -------------------------------------------------------------------------------- /presentation/FPGA 101 Workshop - Hackaday Belgrade 2018.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/presentation/FPGA 101 Workshop - Hackaday Belgrade 2018.pptx -------------------------------------------------------------------------------- /projects/galaksija/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /projects/galaksija/font.py: -------------------------------------------------------------------------------- 1 | import binascii 2 | def split_every(n, s): 3 | return [ s[i:i+n] for i in xrange(0, len(s), n) ] 4 | 5 | filename = 'galchr.bin' 6 | with open(filename, 'rb') as f: 7 | content = f.read() 8 | 9 | list = split_every(2, binascii.hexlify(content)) 10 | for i in list: 11 | print(i) 12 | -------------------------------------------------------------------------------- /projects/galaksija/font_rom.v: -------------------------------------------------------------------------------- 1 | module font_rom( 2 | input clk, 3 | input [10:0] addr, 4 | output [7:0] data_out 5 | ); 6 | 7 | reg [7:0] store[0:2047] /* verilator public_flat */; 8 | 9 | initial 10 | begin 11 | $readmemh("galchr.mem", store); 12 | end 13 | 14 | always @(posedge clk) 15 | data_out <= store[addr]; 16 | endmodule 17 | -------------------------------------------------------------------------------- /projects/galaksija/gal.vid: -------------------------------------------------------------------------------- 1 | > @'READY > @'READY > @'READY > @'READY > @'READY > @'READY >LIST 10 PRINT "TEST" @'READY >_ -------------------------------------------------------------------------------- /projects/galaksija/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 12.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0000), // DIVR = 0 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /projects/galaksija/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn mreq_n 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn ser_rx 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /projects/galaksija/prebuilt/Makefile: -------------------------------------------------------------------------------- 1 | ICEPROG = iceprog 2 | 3 | all: upload 4 | 5 | upload: 6 | $(ICEPROG) hardware.bin 7 | 8 | .PHONY: upload 9 | 10 | -------------------------------------------------------------------------------- /projects/galaksija/prebuilt/hardware.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/projects/galaksija/prebuilt/hardware.bin -------------------------------------------------------------------------------- /projects/galaksija/ram_memory.v: -------------------------------------------------------------------------------- 1 | module ram_memory( 2 | input clk, 3 | input [ADDR_WIDTH-1:0] addr, 4 | input [7:0] data_in, 5 | input rd, 6 | input we, 7 | output reg [7:0] data_out 8 | ); 9 | parameter integer ADDR_WIDTH = 8; 10 | 11 | reg [7:0] ram[0:(2 ** ADDR_WIDTH)-1] /* verilator public_flat */; 12 | 13 | parameter FILENAME = ""; 14 | 15 | initial 16 | begin 17 | if (FILENAME!="") 18 | $readmemh(FILENAME, ram); 19 | end 20 | 21 | always @(posedge clk) 22 | begin 23 | if (we) 24 | ram[addr] <= data_in; 25 | if (rd) 26 | data_out <= ram[addr]; 27 | end 28 | endmodule 29 | -------------------------------------------------------------------------------- /projects/galaksija/rom_memory.v: -------------------------------------------------------------------------------- 1 | module rom_memory( 2 | input clk, 3 | input [ADDR_WIDTH-1:0] addr, 4 | input rd, 5 | output reg [7:0] data_out 6 | ); 7 | parameter FILENAME = ""; 8 | 9 | parameter integer ADDR_WIDTH = 8; 10 | 11 | reg [7:0] rom[0:(2 ** ADDR_WIDTH)-1] /* verilator public_flat */; 12 | 13 | initial 14 | begin 15 | if (FILENAME!="") 16 | $readmemh(FILENAME, rom); 17 | end 18 | 19 | always @(posedge clk) 20 | begin 21 | if (rd) 22 | data_out <= rom[addr]; 23 | end 24 | endmodule 25 | -------------------------------------------------------------------------------- /projects/galaksija/uart_rx.v: -------------------------------------------------------------------------------- 1 | /* 2 | * PicoSoC - A simple example SoC using PicoRV32 3 | * 4 | * Copyright (C) 2017 Clifford Wolf 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | * 18 | */ 19 | 20 | module uart_rx ( 21 | input clk, 22 | input resetn, 23 | 24 | input ser_rx, 25 | 26 | input [31:0] cfg_divider, 27 | 28 | output [7:0] data, 29 | output reg valid, 30 | output reg starting 31 | ); 32 | 33 | reg [3:0] recv_state; 34 | reg [31:0] recv_divcnt; 35 | reg [7:0] recv_pattern; 36 | reg [7:0] recv_buf_data; 37 | 38 | assign data = valid ? recv_buf_data : ~0; 39 | 40 | always @(posedge clk) begin 41 | if (!resetn) begin 42 | recv_state <= 0; 43 | recv_divcnt <= 0; 44 | recv_pattern <= 0; 45 | recv_buf_data <= 0; 46 | valid <= 0; 47 | end else begin 48 | recv_divcnt <= recv_divcnt + 1; 49 | 50 | valid <= 0; 51 | starting <= 0; 52 | case (recv_state) 53 | 0: begin 54 | if (!ser_rx) 55 | recv_state <= 1; 56 | recv_divcnt <= 0; 57 | end 58 | 1: begin 59 | starting <= 1; 60 | if (2*recv_divcnt > cfg_divider) begin 61 | recv_state <= 2; 62 | recv_divcnt <= 0; 63 | end 64 | end 65 | 10: begin 66 | if (recv_divcnt > cfg_divider) begin 67 | recv_buf_data <= recv_pattern; 68 | valid <= 1; 69 | recv_state <= 0; 70 | end 71 | end 72 | default: begin 73 | if (recv_divcnt > cfg_divider) begin 74 | recv_pattern <= {ser_rx, recv_pattern[7:1]}; 75 | recv_state <= recv_state + 1; 76 | recv_divcnt <= 0; 77 | end 78 | end 79 | endcase 80 | end 81 | end 82 | endmodule 83 | -------------------------------------------------------------------------------- /tests/led/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tests/led/led.v: -------------------------------------------------------------------------------- 1 | module led ( 2 | output LED_B 3 | ); 4 | assign LED_B = 1'b0; 5 | endmodule 6 | -------------------------------------------------------------------------------- /tests/led/led_tb.v: -------------------------------------------------------------------------------- 1 | `default_nettype none 2 | `timescale 100 ns / 10 ns 3 | 4 | module led_tb(); 5 | 6 | reg clk = 0; 7 | 8 | wire led; 9 | 10 | always 11 | #5 clk = ~clk; 12 | 13 | led unit(.LED_B(led)); 14 | 15 | 16 | initial begin 17 | $dumpfile("led_tb.vcd"); 18 | $dumpvars(0, led_tb); 19 | 20 | #100 $display("finish"); 21 | $finish; 22 | end 23 | 24 | endmodule 25 | -------------------------------------------------------------------------------- /tests/led/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_D7 6 # output 11 | set_io -nowarn LCD_D6 44 # output 12 | set_io -nowarn LCD_D5 4 # output 13 | set_io -nowarn LCD_D4 3 # output 14 | set_io -nowarn LCD_D3 48 # output 15 | set_io -nowarn LCD_D2 45 # output 16 | set_io -nowarn LCD_D1 47 # output 17 | set_io -nowarn LCD_D0 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tests/riscv/Makefile: -------------------------------------------------------------------------------- 1 | RISCV_TOOLS_PREFIX = riscv64-unknown-elf- 2 | CXX = $(RISCV_TOOLS_PREFIX)g++ 3 | CC = $(RISCV_TOOLS_PREFIX)gcc 4 | AS = $(RISCV_TOOLS_PREFIX)gcc 5 | ICEPROG = iceprog 6 | 7 | all: firmware.bin 8 | 9 | # ---- iCE40 UP5k Breakout Board ---- 10 | 11 | upload: firmware.bin 12 | $(ICEPROG) -o 1M firmware.bin 13 | 14 | # ---- Example Firmware ---- 15 | firmware.elf: sections.lds start.s firmware.c sections.c 16 | $(CC) -O3 -nostartfiles -mabi=ilp32 -march=rv32ic -Wl,-Bstatic,-T,sections.lds,--strip-debug -ffreestanding -o firmware.elf start.s sections.c firmware.c -lgcc 17 | 18 | firmware.bin: firmware.elf 19 | $(RISCV_TOOLS_PREFIX)objcopy -O binary firmware.elf firmware.bin 20 | 21 | # ---- Clean ---- 22 | 23 | clean: 24 | rm -f firmware.elf firmware.bin 25 | 26 | .PHONY: upload clean 27 | 28 | -------------------------------------------------------------------------------- /tests/riscv/sections.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern uint32_t _sidata, _sdata, _edata; 5 | 6 | void executable_init_sections() 7 | { 8 | for (uint32_t *src = &_sidata, *dest = &_sdata; dest < &_edata;) { 9 | *dest++ = *src++; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tutorials/01-Basics/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/01-Basics/led.v: -------------------------------------------------------------------------------- 1 | module led ( 2 | output LED_B 3 | ); 4 | assign LED_B = 1'b0; 5 | endmodule 6 | -------------------------------------------------------------------------------- /tutorials/01-Basics/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_D7 6 # output 11 | set_io -nowarn LCD_D6 44 # output 12 | set_io -nowarn LCD_D5 4 # output 13 | set_io -nowarn LCD_D4 3 # output 14 | set_io -nowarn LCD_D3 48 # output 15 | set_io -nowarn LCD_D2 45 # output 16 | set_io -nowarn LCD_D1 47 # output 17 | set_io -nowarn LCD_D0 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/02-LED/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/02-LED/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_D7 6 # output 11 | set_io -nowarn LCD_D6 44 # output 12 | set_io -nowarn LCD_D5 4 # output 13 | set_io -nowarn LCD_D4 3 # output 14 | set_io -nowarn LCD_D3 48 # output 15 | set_io -nowarn LCD_D2 45 # output 16 | set_io -nowarn LCD_D1 47 # output 17 | set_io -nowarn LCD_D0 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/02-LED/rgb.v: -------------------------------------------------------------------------------- 1 | module rgb ( 2 | input CLK, 3 | output LED_R, 4 | output LED_G, 5 | output LED_B, 6 | output LCD_PWM 7 | ); 8 | assign LCD_PWM = 1'b0; 9 | 10 | reg [32:0] clk_cnt; 11 | 12 | initial 13 | begin 14 | clk_cnt = 0; 15 | end 16 | 17 | always @(posedge CLK) 18 | begin 19 | clk_cnt <= clk_cnt + 1; 20 | end 21 | 22 | assign LED_R = !clk_cnt[2]; 23 | assign LED_G = !clk_cnt[3]; 24 | assign LED_B = !clk_cnt[4]; 25 | endmodule 26 | -------------------------------------------------------------------------------- /tutorials/02-LED/rgb_tb.gtkw: -------------------------------------------------------------------------------- 1 | [*] 2 | [*] GTKWave Analyzer v3.3.66 (w)1999-2015 BSI 3 | [*] Sat May 12 10:24:54 2018 4 | [*] 5 | [dumpfile] "/home/micko/src/fpga101-workshop/tutorials/02-LED/rgb_tb.vcd" 6 | [dumpfile_mtime] "Sat May 12 10:24:30 2018" 7 | [dumpfile_size] 3798 8 | [savefile] "/home/micko/src/fpga101-workshop/tutorials/02-LED/rgb_tb.gtkw" 9 | [timestart] 0 10 | [size] 1000 600 11 | [pos] -1 -1 12 | *-18.000000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 13 | [sst_width] 196 14 | [signals_width] 86 15 | [sst_expanded] 1 16 | [sst_vpaned_height] 150 17 | @28 18 | rgb_tb.clk 19 | rgb_tb.led_b 20 | rgb_tb.led_g 21 | rgb_tb.led_pwm 22 | rgb_tb.led_r 23 | [pattern_trace] 1 24 | [pattern_trace] 0 25 | -------------------------------------------------------------------------------- /tutorials/02-LED/rgb_tb.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns/1ps 2 | module rgb_tb(); 3 | reg clk = 0; 4 | wire led_r; 5 | wire led_g; 6 | wire led_b; 7 | wire led_pwm; 8 | 9 | rgb leds(.CLK(clk),.LED_R(led_r),.LED_G(led_g),.LED_B(led_b),.LCD_PWM(led_pwm)); 10 | 11 | always 12 | #(5) clk <= !clk; 13 | 14 | initial 15 | begin 16 | $dumpfile("rgb_tb.vcd"); 17 | $dumpvars(0,rgb_tb); 18 | #1000 19 | $finish; 20 | end 21 | endmodule 22 | -------------------------------------------------------------------------------- /tutorials/03-Buttons/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/03-Buttons/button_rgb.v: -------------------------------------------------------------------------------- 1 | module button_rgb ( 2 | input CLK, 3 | output LED_R, 4 | output LED_G, 5 | output LED_B, 6 | output LCD_PWM, 7 | input B0, 8 | input B1, 9 | input B2 10 | ); 11 | assign LCD_PWM = 1'b0; 12 | 13 | assign LED_R = !B0; 14 | assign LED_G = !B1; 15 | assign LED_B = !B2; 16 | endmodule 17 | -------------------------------------------------------------------------------- /tutorials/03-Buttons/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_D7 6 # output 11 | set_io -nowarn LCD_D6 44 # output 12 | set_io -nowarn LCD_D5 4 # output 13 | set_io -nowarn LCD_D4 3 # output 14 | set_io -nowarn LCD_D3 48 # output 15 | set_io -nowarn LCD_D2 45 # output 16 | set_io -nowarn LCD_D1 47 # output 17 | set_io -nowarn LCD_D0 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/04-Audio/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/04-Audio/audio.v: -------------------------------------------------------------------------------- 1 | module audio( 2 | input CLK, 3 | output LCD_PWM, 4 | input B0, 5 | input B1, 6 | input B2, 7 | input B3, 8 | input B4, 9 | input B5, 10 | output reg AUDIO); 11 | 12 | parameter TONE_A4 = 12000000/440/2; 13 | parameter TONE_B4 = 12000000/494/2; 14 | parameter TONE_C5 = 12000000/523/2; 15 | parameter TONE_D5 = 12000000/587/2; 16 | parameter TONE_E5 = 12000000/659/2; 17 | parameter TONE_F5 = 12000000/698/2; 18 | parameter TONE_G5 = 12000000/783/2; 19 | 20 | reg [14:0] counter; 21 | 22 | always @(posedge CLK) 23 | if(counter==0) counter <= (B0 ? TONE_A4-1 : 24 | B1 ? TONE_B4-1 : 25 | B2 ? TONE_C5-1 : 26 | B3 ? TONE_D5-1 : 27 | B4 ? TONE_E5-1 : 28 | B5 ? TONE_F5-1 : 29 | 0); else counter <= counter-1; 30 | 31 | always @(posedge CLK) 32 | if(counter==0) 33 | AUDIO <= ~AUDIO; 34 | 35 | assign LCD_PWM = 1'b0; 36 | endmodule 37 | -------------------------------------------------------------------------------- /tutorials/04-Audio/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/05-Video/00-Basic/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/05-Video/00-Basic/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/05-Video/00-Basic/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/05-Video/00-Basic/top.v: -------------------------------------------------------------------------------- 1 | module top( 2 | output LCD_CLK, 3 | output [7:0] LCD_DAT, 4 | output LCD_HS, 5 | output LCD_VS, 6 | output LCD_DEN, 7 | output LCD_RST, 8 | output LCD_PWM); 9 | 10 | /* ------------------------ 11 | Clock generator 12 | ------------------------*/ 13 | 14 | wire clkhf; 15 | wire locked; 16 | wire pixclk; 17 | 18 | (* ROUTE_THROUGH_FABRIC=1 *) 19 | SB_HFOSC #(.CLKHF_DIV("0b01")) hfosc_i ( 20 | .CLKHFEN(1'b1), 21 | .CLKHFPU(1'b1), 22 | .CLKHF(clkhf) 23 | ); 24 | 25 | pll pll_i(.clock_in(clkhf), .clock_out(pixclk), .locked(locked)); 26 | 27 | /* ---------------------------- 28 | Video signal generator 29 | ----------------------------*/ 30 | 31 | reg [23:0] rgb_data; 32 | wire [9:0] h_pos; 33 | wire [9:0] v_pos; 34 | 35 | assign LCD_CLK = pixclk; 36 | assign LCD_RST = 1'b1; 37 | assign LCD_PWM = 1'b1; 38 | video generator ( .clk(pixclk), // pixel clock in 39 | .resetn(locked), 40 | .lcd_dat(LCD_DAT), 41 | .lcd_hsync(LCD_HS), 42 | .lcd_vsync(LCD_VS), 43 | .lcd_den(LCD_DEN), 44 | .h_pos(h_pos), 45 | .v_pos(v_pos), 46 | .rgb_data(rgb_data)); 47 | 48 | 49 | /* ---------------------------- 50 | Custom video generator 51 | ----------------------------*/ 52 | 53 | assign rgb_data = 24'hff0000; 54 | 55 | endmodule 56 | -------------------------------------------------------------------------------- /tutorials/05-Video/00-Basic/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/05-Video/01-KeyTest/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/05-Video/01-KeyTest/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/05-Video/01-KeyTest/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/05-Video/01-KeyTest/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/06-StateMachine/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/06-StateMachine/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/06-StateMachine/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/06-StateMachine/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/07-Memory/00-Basic/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/07-Memory/00-Basic/font_rom.v: -------------------------------------------------------------------------------- 1 | module font_rom( 2 | input clk, 3 | input [11:0] addr, 4 | output [7:0] data_out 5 | ); 6 | 7 | reg [7:0] store[0:4095]; 8 | 9 | initial 10 | begin 11 | $readmemh("font_vga.mem", store); 12 | end 13 | 14 | always @(posedge clk) 15 | data_out <= store[addr]; 16 | endmodule 17 | -------------------------------------------------------------------------------- /tutorials/07-Memory/00-Basic/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/07-Memory/00-Basic/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/07-Memory/00-Basic/top.v: -------------------------------------------------------------------------------- 1 | module top( 2 | output LCD_CLK, 3 | output [7:0] LCD_DAT, 4 | output LCD_HS, 5 | output LCD_VS, 6 | output LCD_DEN, 7 | output LCD_RST, 8 | output LCD_PWM); 9 | 10 | /* ------------------------ 11 | Clock generator 12 | ------------------------*/ 13 | 14 | wire clkhf; 15 | wire locked; 16 | wire pixclk; 17 | 18 | (* ROUTE_THROUGH_FABRIC=1 *) 19 | SB_HFOSC #(.CLKHF_DIV("0b01")) hfosc_i ( 20 | .CLKHFEN(1'b1), 21 | .CLKHFPU(1'b1), 22 | .CLKHF(clkhf) 23 | ); 24 | 25 | pll pll_i(.clock_in(clkhf), .clock_out(pixclk), .locked(locked)); 26 | 27 | /* ---------------------------- 28 | Video signal generator 29 | ----------------------------*/ 30 | 31 | reg [23:0] rgb_data; 32 | wire [9:0] h_pos; 33 | wire [9:0] v_pos; 34 | 35 | assign LCD_CLK = pixclk; 36 | assign LCD_RST = 1'b1; 37 | assign LCD_PWM = 1'b1; 38 | video generator ( .clk(pixclk), // pixel clock in 39 | .resetn(locked), 40 | .lcd_dat(LCD_DAT), 41 | .lcd_hsync(LCD_HS), 42 | .lcd_vsync(LCD_VS), 43 | .lcd_den(LCD_DEN), 44 | .h_pos(h_pos), 45 | .v_pos(v_pos), 46 | .rgb_data(rgb_data)); 47 | 48 | 49 | /* ---------------------------- 50 | Custom video generator 51 | ----------------------------*/ 52 | wire [7:0] data_out; 53 | 54 | font_rom vga_font( 55 | .clk(pixclk), 56 | .addr({ 8'd65, v_pos[3:0] }), 57 | .data_out(data_out) 58 | ); 59 | 60 | assign rgb_data = data_out[7-h_pos[2:0]+1] ? 24'hffffff : 24'h0000aa; // +1 for sync 61 | 62 | endmodule 63 | -------------------------------------------------------------------------------- /tutorials/07-Memory/00-Basic/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/font_rom.v: -------------------------------------------------------------------------------- 1 | module font_rom( 2 | input clk, 3 | input [11:0] addr, 4 | output [7:0] data_out 5 | ); 6 | 7 | reg [7:0] store[0:4095]; 8 | 9 | initial 10 | begin 11 | $readmemh("font_vga.mem", store); 12 | end 13 | 14 | always @(posedge clk) 15 | data_out <= store[addr]; 16 | endmodule 17 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/text.dat: -------------------------------------------------------------------------------- 1 | _________ _____ _____ ____ 2 | (_ _____| __ \ / ___ \( ) 3 | ) (___ ) )_) ) / \_) /\ \ 4 | ( ___) ( ___( ( ___( (__) ) 5 | ) ( ) ) ( ( (__ ) ( 6 | ( ) ( ( \ \__/ / /\ \ 7 | \_/ /__\ \____/__( )__\ 8 | ____ ____ ____ 9 | / // __ \ / / 10 | / /) | ( ) )/ /) ) 11 | /_/( (( ( ) )_/( ( 12 | ) | ( ) ) ) ) 13 | ( (( (__) ) ( ( 14 | /__\\____/ /__\ 15 | HACKADAY BELGRADE MAY 26TH 2018 16 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/text.py: -------------------------------------------------------------------------------- 1 | import binascii 2 | def split_every(n, s): 3 | return [ s[i:i+n] for i in xrange(0, len(s), n) ] 4 | 5 | filename = 'text.dat' 6 | with open(filename, 'rb') as f: 7 | content = f.read() 8 | 9 | list = split_every(2, binascii.hexlify(content)) 10 | for i in list: 11 | if i != "0a": 12 | print(i) 13 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/text_rom.v: -------------------------------------------------------------------------------- 1 | module text_rom( 2 | input clk, 3 | input [9:0] addr, 4 | output [7:0] data_out 5 | ); 6 | 7 | reg [7:0] store[0:600]; 8 | 9 | initial 10 | begin 11 | $readmemh("text.mem", store); 12 | end 13 | 14 | always @(posedge clk) 15 | data_out <= store[addr]; 16 | endmodule 17 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/top.v: -------------------------------------------------------------------------------- 1 | module top( 2 | output LCD_CLK, 3 | output [7:0] LCD_DAT, 4 | output LCD_HS, 5 | output LCD_VS, 6 | output LCD_DEN, 7 | output LCD_RST, 8 | output LCD_PWM); 9 | 10 | /* ------------------------ 11 | Clock generator 12 | ------------------------*/ 13 | 14 | wire clkhf; 15 | wire locked; 16 | wire pixclk; 17 | 18 | (* ROUTE_THROUGH_FABRIC=1 *) 19 | SB_HFOSC #(.CLKHF_DIV("0b01")) hfosc_i ( 20 | .CLKHFEN(1'b1), 21 | .CLKHFPU(1'b1), 22 | .CLKHF(clkhf) 23 | ); 24 | 25 | pll pll_i(.clock_in(clkhf), .clock_out(pixclk), .locked(locked)); 26 | 27 | /* ---------------------------- 28 | Video signal generator 29 | ----------------------------*/ 30 | 31 | reg [23:0] rgb_data; 32 | wire [9:0] h_pos; 33 | wire [9:0] v_pos; 34 | 35 | assign LCD_CLK = pixclk; 36 | assign LCD_RST = 1'b1; 37 | assign LCD_PWM = 1'b1; 38 | video generator ( .clk(pixclk), // pixel clock in 39 | .resetn(locked), 40 | .lcd_dat(LCD_DAT), 41 | .lcd_hsync(LCD_HS), 42 | .lcd_vsync(LCD_VS), 43 | .lcd_den(LCD_DEN), 44 | .h_pos(h_pos), 45 | .v_pos(v_pos), 46 | .rgb_data(rgb_data)); 47 | 48 | 49 | /* ---------------------------- 50 | Custom video generator 51 | ----------------------------*/ 52 | wire [7:0] data_out; 53 | wire [7:0] char; 54 | 55 | font_rom vga_font( 56 | .clk(pixclk), 57 | .addr({ char, v_pos[3:0] }), 58 | .data_out(data_out) 59 | ); 60 | 61 | text_rom text_mem( 62 | .clk(pixclk), 63 | .addr({ v_pos[8:4],5'b00000} + { v_pos[9:4],3'b000 } + h_pos[9:3]), 64 | .data_out(char)); 65 | 66 | assign rgb_data = data_out[7-h_pos[2:0]+1] ? 24'hffffff : 24'h0000aa; // +1 for sync 67 | 68 | endmodule 69 | -------------------------------------------------------------------------------- /tutorials/07-Memory/01-Buffer/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/07-Memory/02-Terminal/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/07-Memory/02-Terminal/font_rom.v: -------------------------------------------------------------------------------- 1 | module font_rom( 2 | input clk, 3 | input [11:0] addr, 4 | output [7:0] data_out 5 | ); 6 | 7 | reg [7:0] store[0:4095] /* verilator public_flat */; 8 | 9 | initial 10 | begin 11 | $readmemh("font_vga.mem", store); 12 | end 13 | 14 | always @(posedge clk) begin 15 | data_out <= store[addr]; 16 | end 17 | endmodule 18 | -------------------------------------------------------------------------------- /tutorials/07-Memory/02-Terminal/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 12.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0000), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/07-Memory/02-Terminal/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn ser_rx 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn ser_tx 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/07-Memory/02-Terminal/uart_rx.v: -------------------------------------------------------------------------------- 1 | /* 2 | * PicoSoC - A simple example SoC using PicoRV32 3 | * 4 | * Copyright (C) 2017 Clifford Wolf 5 | * 6 | * Permission to use, copy, modify, and/or distribute this software for any 7 | * purpose with or without fee is hereby granted, provided that the above 8 | * copyright notice and this permission notice appear in all copies. 9 | * 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 | * 18 | */ 19 | 20 | module uart_rx ( 21 | input clk, 22 | input resetn, 23 | 24 | input ser_rx, 25 | 26 | input [31:0] cfg_divider, 27 | 28 | output [7:0] data, 29 | output reg valid 30 | ); 31 | 32 | reg [3:0] recv_state; 33 | reg [31:0] recv_divcnt; 34 | reg [7:0] recv_pattern; 35 | reg [7:0] recv_buf_data; 36 | 37 | assign data = valid ? recv_buf_data : ~0; 38 | 39 | always @(posedge clk) begin 40 | if (!resetn) begin 41 | recv_state <= 0; 42 | recv_divcnt <= 0; 43 | recv_pattern <= 0; 44 | recv_buf_data <= 0; 45 | valid <= 0; 46 | end else begin 47 | recv_divcnt <= recv_divcnt + 1; 48 | 49 | valid <= 0; 50 | 51 | case (recv_state) 52 | 0: begin 53 | if (!ser_rx) 54 | recv_state <= 1; 55 | recv_divcnt <= 0; 56 | end 57 | 1: begin 58 | if (2*recv_divcnt > cfg_divider) begin 59 | recv_state <= 2; 60 | recv_divcnt <= 0; 61 | end 62 | end 63 | 10: begin 64 | if (recv_divcnt > cfg_divider) begin 65 | recv_buf_data <= recv_pattern; 66 | valid <= 1; 67 | recv_state <= 0; 68 | end 69 | end 70 | default: begin 71 | if (recv_divcnt > cfg_divider) begin 72 | recv_pattern <= {ser_rx, recv_pattern[7:1]}; 73 | recv_state <= recv_state + 1; 74 | recv_divcnt <= 0; 75 | end 76 | end 77 | endcase 78 | end 79 | end 80 | endmodule 81 | -------------------------------------------------------------------------------- /tutorials/07-Memory/02-Terminal/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/08-MovingSprite/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/08-MovingSprite/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/08-MovingSprite/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/08-MovingSprite/sprite_rom.v: -------------------------------------------------------------------------------- 1 | module sprite_rom( 2 | input clk, 3 | input [11:0] addr, 4 | output [23:0] data_out 5 | ); 6 | 7 | reg [3:0] store[0:4095]; 8 | wire [23:0] palette[0:15]; 9 | 10 | assign palette[0] = 24'h000000; 11 | assign palette[1] = 24'h180002; 12 | assign palette[2] = 24'h240001; 13 | assign palette[3] = 24'h390100; 14 | assign palette[4] = 24'h530000; 15 | assign palette[5] = 24'h710000; 16 | assign palette[6] = 24'h8a0000; 17 | assign palette[7] = 24'ha20000; 18 | assign palette[8] = 24'hba0000; 19 | assign palette[9] = 24'hdd0002; 20 | assign palette[10] = 24'hff0000; 21 | assign palette[11] = 24'hff3031; 22 | assign palette[12] = 24'hff5f62; 23 | assign palette[13] = 24'hff8d8b; 24 | assign palette[14] = 24'hffcccb; 25 | assign palette[15] = 24'hffffff; 26 | 27 | initial 28 | begin 29 | $readmemh("sprite.mem", store); 30 | end 31 | 32 | always @(posedge clk) 33 | data_out <= palette[store[addr]]; 34 | endmodule 35 | -------------------------------------------------------------------------------- /tutorials/08-MovingSprite/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output [9:0] sprite_x, 8 | output [9:0] sprite_y, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | reg [9:0] h_pos = 0; 27 | reg [9:0] v_pos = 0; 28 | 29 | wire h_active, v_active, visible; 30 | wire [23:0] rgb_data; 31 | 32 | always @(posedge clk) 33 | begin 34 | if (resetn == 0) begin 35 | h_pos <= 10'b0; 36 | v_pos <= 10'b0; 37 | 38 | end else begin 39 | //Pixel counters 40 | if (channel == 2) begin 41 | channel <= 0; 42 | if (h_pos == h_total - 1) begin 43 | h_pos <= 0; 44 | if (v_pos == v_total - 1) begin 45 | v_pos <= 0; 46 | end else begin 47 | v_pos <= v_pos + 1; 48 | end 49 | end else begin 50 | h_pos <= h_pos + 1; 51 | end 52 | end else begin 53 | channel <= channel + 1; 54 | end 55 | lcd_den <= !visible; 56 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 57 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 58 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 59 | channel == 1 ? rgb_data[15:8] : 60 | rgb_data[7:0]; 61 | end 62 | end 63 | 64 | assign h_active = (h_pos < h_visible); 65 | assign v_active = (v_pos < v_visible); 66 | assign visible = h_active && v_active; 67 | 68 | 69 | //Compensate for latency 70 | assign sprite_x = h_active ? (h_pos + 3) : 0; 71 | assign sprite_y = h_active ? v_pos : (v_pos + 1); 72 | 73 | endmodule 74 | -------------------------------------------------------------------------------- /tutorials/09-Pong/00-Move/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/09-Pong/00-Move/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/09-Pong/00-Move/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/09-Pong/00-Move/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/09-Pong/01-Ball/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/09-Pong/01-Ball/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/09-Pong/01-Ball/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/09-Pong/01-Ball/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/09-Pong/02-Game/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/09-Pong/02-Game/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/09-Pong/02-Game/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/09-Pong/02-Game/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/09-Pong/03-Wall/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/09-Pong/03-Wall/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 24.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0001), // DIVR = 1 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/09-Pong/03-Wall/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_DAT[7] 6 # output 11 | set_io -nowarn LCD_DAT[6] 44 # output 12 | set_io -nowarn LCD_DAT[5] 4 # output 13 | set_io -nowarn LCD_DAT[4] 3 # output 14 | set_io -nowarn LCD_DAT[3] 48 # output 15 | set_io -nowarn LCD_DAT[2] 45 # output 16 | set_io -nowarn LCD_DAT[1] 47 # output 17 | set_io -nowarn LCD_DAT[0] 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/09-Pong/03-Wall/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | -------------------------------------------------------------------------------- /tutorials/10-CPU/alu.v: -------------------------------------------------------------------------------- 1 | module alu( 2 | input clk, 3 | input [7:0] A, 4 | input [7:0] B, 5 | input [3:0] operation, 6 | output reg [7:0] result, 7 | output reg CF, 8 | output reg ZF, 9 | output reg SF 10 | ); 11 | 12 | localparam ALU_OP_ADD /* verilator public_flat */ = 4'b0000; 13 | localparam ALU_OP_SUB /* verilator public_flat */ = 4'b0001; 14 | localparam ALU_OP_ADC /* verilator public_flat */ = 4'b0010; 15 | localparam ALU_OP_SBC /* verilator public_flat */ = 4'b0011; 16 | 17 | localparam ALU_OP_AND /* verilator public_flat */ = 4'b0100; 18 | localparam ALU_OP_OR /* verilator public_flat */ = 4'b0101; 19 | localparam ALU_OP_NOT /* verilator public_flat */ = 4'b0110; 20 | localparam ALU_OP_XOR /* verilator public_flat */ = 4'b0111; 21 | 22 | localparam ALU_OP_SHL /* verilator public_flat */ = 4'b1000; 23 | localparam ALU_OP_SHR /* verilator public_flat */ = 4'b1001; 24 | localparam ALU_OP_SAL /* verilator public_flat */ = 4'b1010; 25 | localparam ALU_OP_SAR /* verilator public_flat */ = 4'b1011; 26 | 27 | localparam ALU_OP_ROL /* verilator public_flat */ = 4'b1100; 28 | localparam ALU_OP_ROR /* verilator public_flat */ = 4'b1101; 29 | localparam ALU_OP_RCL /* verilator public_flat */ = 4'b1110; 30 | localparam ALU_OP_RCR /* verilator public_flat */ = 4'b1111; 31 | 32 | reg [8:0] tmp; 33 | 34 | always @(posedge clk) 35 | begin 36 | case (operation) 37 | ALU_OP_ADD : 38 | tmp = A + B; 39 | ALU_OP_SUB : 40 | tmp = A - B; 41 | ALU_OP_ADC : 42 | tmp = A + B + { 7'b0000000, CF }; 43 | ALU_OP_SBC : 44 | tmp = A - B - { 7'b0000000, CF }; 45 | ALU_OP_AND : 46 | tmp = {1'b0, A & B }; 47 | ALU_OP_OR : 48 | tmp = {1'b0, A | B }; 49 | ALU_OP_NOT : 50 | tmp = {1'b0, ~B }; 51 | ALU_OP_XOR : 52 | tmp = {1'b0, A ^ B}; 53 | ALU_OP_SHL : 54 | tmp = { A[7], A[6:0], 1'b0}; 55 | ALU_OP_SHR : 56 | tmp = { A[0], 1'b0, A[7:1]}; 57 | ALU_OP_SAL : 58 | // Same as SHL 59 | tmp = { A[7], A[6:0], 1'b0}; 60 | ALU_OP_SAR : 61 | tmp = { A[0], A[7], A[7:1]}; 62 | ALU_OP_ROL : 63 | tmp = { A[7], A[6:0], A[7]}; 64 | ALU_OP_ROR : 65 | tmp = { A[0], A[0], A[7:1]}; 66 | ALU_OP_RCL : 67 | tmp = { A[7], A[6:0], CF}; 68 | ALU_OP_RCR : 69 | tmp = { A[0], CF, A[7:1]}; 70 | endcase 71 | 72 | CF <= tmp[8]; 73 | ZF <= tmp[7:0] == 0; 74 | SF <= tmp[7]; 75 | 76 | result <= tmp[7:0]; 77 | end 78 | endmodule 79 | 80 | -------------------------------------------------------------------------------- /tutorials/10-CPU/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/10-CPU/grom_computer.v: -------------------------------------------------------------------------------- 1 | module grom_computer 2 | (input clk, // Main Clock 3 | input reset, // reset 4 | output hlt, 5 | output reg[7:0] display_out 6 | ); 7 | 8 | wire [11:0] addr; 9 | wire [7:0] memory_out; 10 | wire [7:0] memory_in; 11 | wire mem_enable; 12 | wire we; 13 | wire ioreq; 14 | 15 | grom_cpu cpu(.clk(clk),.reset(reset),.addr(addr),.data_in(memory_out),.data_out(memory_in),.we(we),.ioreq(ioreq),.hlt(hlt)); 16 | 17 | assign mem_enable = we & ~ioreq; 18 | 19 | ram_memory memory(.clk(clk),.addr(addr),.data_in(memory_in),.we(mem_enable),.data_out(memory_out)); 20 | 21 | always @(posedge clk) 22 | begin 23 | if(ioreq==1 && we==1) 24 | begin 25 | display_out <= memory_in; 26 | `ifdef DISASSEMBLY 27 | $display("Display output : %h", memory_in); 28 | `endif 29 | end 30 | end 31 | endmodule 32 | -------------------------------------------------------------------------------- /tutorials/10-CPU/grom_computer_tb.v: -------------------------------------------------------------------------------- 1 | `timescale 1ns/1ps 2 | module grom_computer_tb(); 3 | reg clk = 0; 4 | reg reset; 5 | wire [7:0] display_out; 6 | wire hlt; 7 | 8 | grom_computer computer(.clk(clk),.reset(reset),.hlt(hlt),.display_out(display_out)); 9 | 10 | always 11 | #(5) clk <= !clk; 12 | 13 | initial 14 | begin 15 | $dumpfile("grom_computer_tb.vcd"); 16 | $dumpvars(0,grom_computer_tb); 17 | reset = 1; 18 | #20 19 | reset = 0; 20 | #900 21 | $finish; 22 | end 23 | endmodule 24 | -------------------------------------------------------------------------------- /tutorials/10-CPU/grom_top.v: -------------------------------------------------------------------------------- 1 | module grom_top 2 | (input CLK, // Main Clock 3 | input B0, // button 4 | 5 | output LED_R, 6 | output LED_G, 7 | output LED_B, 8 | 9 | output LCD_PWM 10 | ); 11 | 12 | wire [7:0] display_out; 13 | wire hlt; 14 | 15 | grom_computer computer(.clk(CLK),.reset(B0),.hlt(hlt),.display_out(display_out)); 16 | 17 | assign LED_R = ~display_out[0]; 18 | assign LED_G = ~display_out[1]; 19 | assign LED_B = ~display_out[2]; 20 | 21 | assign LCD_PWM = 0; 22 | endmodule 23 | -------------------------------------------------------------------------------- /tutorials/10-CPU/pinout.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | set_io -nowarn CLK 35 # input 3 | 4 | # LCD connector 5 | set_io -nowarn LCD_CLK 18 # output 6 | set_io -nowarn LCD_DEN 19 # output 7 | set_io -nowarn LCD_VS 11 # output 8 | set_io -nowarn LCD_HS 9 # output 9 | set_io -nowarn LCD_RST 2 # output 10 | set_io -nowarn LCD_D7 6 # output 11 | set_io -nowarn LCD_D6 44 # output 12 | set_io -nowarn LCD_D5 4 # output 13 | set_io -nowarn LCD_D4 3 # output 14 | set_io -nowarn LCD_D3 48 # output 15 | set_io -nowarn LCD_D2 45 # output 16 | set_io -nowarn LCD_D1 47 # output 17 | set_io -nowarn LCD_D0 46 # output 18 | set_io -nowarn LCD_PWM 13 # output 19 | 20 | # Buttons 21 | set_io -nowarn B0 23 # input 22 | set_io -nowarn B1 25 # input 23 | set_io -nowarn B2 26 # input 24 | set_io -nowarn B3 27 # input 25 | set_io -nowarn B4 21 # input 26 | set_io -nowarn B5 12 # input 27 | 28 | # Audio jack 29 | set_io -nowarn AUDIO 37 # output 30 | 31 | # PMOD connector 32 | set_io -nowarn PMOD0 32 33 | set_io -nowarn PMOD1 31 34 | set_io -nowarn PMOD2 34 35 | set_io -nowarn PMOD3 43 36 | set_io -nowarn PMOD4 36 37 | set_io -nowarn PMOD5 42 38 | set_io -nowarn PMOD6 38 39 | set_io -nowarn PMOD7 28 40 | 41 | # RGB LED Driver 42 | set_io -nowarn LED_G 39 # output 43 | set_io -nowarn LED_B 40 # output 44 | set_io -nowarn LED_R 41 # output 45 | -------------------------------------------------------------------------------- /tutorials/10-CPU/ram_memory.v: -------------------------------------------------------------------------------- 1 | module ram_memory( 2 | input clk, 3 | input [11:0] addr, 4 | input [7:0] data_in, 5 | input we, 6 | output reg [7:0] data_out 7 | ); 8 | 9 | reg [7:0] store[0:4095] /* verilator public_flat */; 10 | 11 | initial 12 | begin 13 | $readmemh("boot.mem", store); 14 | `ifndef VERILATOR 15 | store[0] <= 8'b11100001; // MOV DS,2 16 | store[1] <= 8'b00000010; // 17 | store[2] <= 8'b01010100; // LOAD R1,[R0] 18 | store[3] <= 8'b00110001; // INC R1 19 | store[4] <= 8'b00110001; // INC R1 20 | store[5] <= 8'b01100001; // STORE [R0],R1 21 | store[6] <= 8'b11010001; // OUT [0],R1 22 | store[7] <= 8'b00000000; // 23 | store[8] <= 8'b00110001; // INC R1 24 | store[9] <= 8'b10100001; // CALL 0x100 25 | store[10] <= 8'b00000000; // 26 | store[11] <= 8'b01111111; // HLT 27 | 28 | 29 | store[256] <= 8'b11010001; // OUT [0],R1 30 | store[257] <= 8'b00000000; // 31 | store[258] <= 8'b01111110; // RET 32 | `endif 33 | end 34 | 35 | always @(posedge clk) 36 | if (we) 37 | store[addr] <= data_in; 38 | else 39 | data_out <= store[addr]; 40 | endmodule 41 | -------------------------------------------------------------------------------- /tutorials/11-Computer/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/11-Computer/board.pcf: -------------------------------------------------------------------------------- 1 | # 12 MHz clock 2 | 3 | set_io -nowarn tx 38 # PMOD6 4 | set_io -nowarn rx 36 # PMOD4 5 | 6 | set_io -nowarn LCD_PWM 13 # output 7 | -------------------------------------------------------------------------------- /tutorials/11-Computer/jmp_boot.v: -------------------------------------------------------------------------------- 1 | module jmp_boot( 2 | input clk, 3 | input reset, 4 | input rd, 5 | output reg [7:0] data_out, 6 | output reg valid 7 | ); 8 | reg [1:0] state = 0; 9 | reg prev_rd = 0; 10 | always @(posedge clk) 11 | begin 12 | if (reset) 13 | begin 14 | state <= 0; 15 | valid <= 1; 16 | end 17 | else 18 | begin 19 | if (rd && prev_rd==0) 20 | begin 21 | case (state) 22 | 2'b00 : begin 23 | data_out <= 8'b11000011; // JMP 0xfd00 24 | state <= 2'b01; 25 | end 26 | 2'b01 : begin 27 | data_out <= 8'h00; 28 | state <= 2'b10; 29 | end 30 | 2'b10 : begin 31 | data_out <= 8'hFD; 32 | state <= 2'b11; 33 | end 34 | 2'b11 : begin 35 | state <= 2'b11; 36 | valid <= 0; 37 | end 38 | endcase 39 | end 40 | prev_rd = rd; 41 | end 42 | end 43 | endmodule -------------------------------------------------------------------------------- /tutorials/11-Computer/mc6850.v: -------------------------------------------------------------------------------- 1 | module mc6850( 2 | input clk, 3 | input reset, 4 | input addr, 5 | input [7:0] data_in, 6 | input rd, 7 | input we, 8 | output reg [7:0] data_out, 9 | input ce, 10 | input rx, 11 | output tx 12 | ); 13 | wire valid; 14 | wire tdre; 15 | wire [7:0] uart_out; 16 | wire dat_wait; 17 | 18 | simpleuart uart( 19 | .clk(clk), 20 | .resetn(~reset), 21 | 22 | .ser_tx(tx), 23 | .ser_rx(rx), 24 | 25 | .cfg_divider(12000000/9600), 26 | 27 | .reg_dat_we(we && (addr==1'b1)), 28 | .reg_dat_re(rd && (addr==1'b1)), 29 | .reg_dat_di(data_in & 8'h7f), 30 | .reg_dat_do(uart_out), 31 | .reg_dat_wait(dat_wait), 32 | .recv_buf_valid(valid), 33 | .tdre(tdre) 34 | ); 35 | 36 | always @(posedge clk) 37 | begin 38 | if (rd) 39 | begin 40 | if (addr==1'b0) 41 | data_out <= { 2'b00,valid, 1'b0, 2'b00, tdre, valid }; 42 | else 43 | data_out <= uart_out; 44 | end 45 | end 46 | endmodule 47 | -------------------------------------------------------------------------------- /tutorials/11-Computer/ram_memory.v: -------------------------------------------------------------------------------- 1 | module ram_memory( 2 | input clk, 3 | input [ADDR_WIDTH-1:0] addr, 4 | input [7:0] data_in, 5 | input rd, 6 | input we, 7 | output reg [7:0] data_out 8 | ); 9 | parameter integer ADDR_WIDTH = 8; 10 | 11 | reg [7:0] ram[0:(2 ** ADDR_WIDTH)-1] /* verilator public_flat */; 12 | 13 | parameter FILENAME = ""; 14 | 15 | initial 16 | begin 17 | if (FILENAME!="") 18 | $readmemh(FILENAME, ram); 19 | end 20 | 21 | always @(posedge clk) 22 | begin 23 | if (we) 24 | ram[addr] <= data_in; 25 | if (rd) 26 | data_out <= ram[addr]; 27 | end 28 | endmodule 29 | -------------------------------------------------------------------------------- /tutorials/11-Computer/rom.py: -------------------------------------------------------------------------------- 1 | import binascii 2 | import sys 3 | 4 | def split_every(n, s): 5 | return [ s[i:i+n] for i in xrange(0, len(s), n) ] 6 | 7 | filename = sys.argv[1] 8 | 9 | with open(filename, 'rb') as f: 10 | content = f.read() 11 | 12 | list = split_every(2, binascii.hexlify(content)) 13 | for i in list: 14 | print(i) 15 | -------------------------------------------------------------------------------- /tutorials/11-Computer/rom_memory.v: -------------------------------------------------------------------------------- 1 | module rom_memory( 2 | input clk, 3 | input [ADDR_WIDTH-1:0] addr, 4 | input rd, 5 | output reg [7:0] data_out 6 | ); 7 | parameter FILENAME = ""; 8 | 9 | parameter integer ADDR_WIDTH = 8; 10 | 11 | reg [7:0] rom[0:(2 ** ADDR_WIDTH)-1] /* verilator public_flat */; 12 | 13 | initial 14 | begin 15 | if (FILENAME!="") 16 | $readmemh(FILENAME, rom); 17 | end 18 | 19 | always @(posedge clk) 20 | begin 21 | if (rd) 22 | data_out <= rom[addr]; 23 | end 24 | endmodule 25 | -------------------------------------------------------------------------------- /tutorials/11-Computer/top_altair.v: -------------------------------------------------------------------------------- 1 | module top 2 | ( 3 | input rx, 4 | output tx, 5 | output LCD_PWM 6 | ); 7 | wire clk; 8 | 9 | SB_HFOSC #(.CLKHF_DIV("0b10")) u_SB_HFOSC(.CLKHFPU(1), .CLKHFEN(1), .CLKHF(clk)); 10 | 11 | reg [5:0] reset_cnt = 0; 12 | wire resetn = &reset_cnt; 13 | 14 | always @(posedge clk) begin 15 | reset_cnt <= reset_cnt + !resetn; 16 | end 17 | 18 | altair machine(.clk(clk),.reset(~resetn),.rx(rx),.tx(tx)); 19 | 20 | assign LCD_PWM = 1'b0; 21 | endmodule 22 | -------------------------------------------------------------------------------- /tutorials/11-Computer/turnmon.bin.mem: -------------------------------------------------------------------------------- 1 | 3e 2 | 03 3 | d3 4 | 10 5 | 3e 6 | 11 7 | d3 8 | 10 9 | 31 10 | 00 11 | fc 12 | cd 13 | 9d 14 | fd 15 | 3e 16 | 2e 17 | cd 18 | f2 19 | fd 20 | cd 21 | e8 22 | fd 23 | fe 24 | 4d 25 | ca 26 | 29 27 | fd 28 | fe 29 | 44 30 | cc 31 | 4f 32 | fd 33 | fe 34 | 4a 35 | c2 36 | 08 37 | fd 38 | cd 39 | a7 40 | fd 41 | e9 42 | cd 43 | a7 44 | fd 45 | 3e 46 | 23 47 | cd 48 | 9d 49 | fd 50 | 54 51 | 5d 52 | cd 53 | c9 54 | fd 55 | 1a 56 | 67 57 | cd 58 | cf 59 | fd 60 | cd 61 | a8 62 | fd 63 | eb 64 | da 65 | 2d 66 | fd 67 | 77 68 | be 69 | ca 70 | 2d 71 | fd 72 | 3e 73 | 3f 74 | cd 75 | f2 76 | fd 77 | c3 78 | 08 79 | fd 80 | cd 81 | a7 82 | fd 83 | eb 84 | d4 85 | e3 86 | fd 87 | cd 88 | a7 89 | fd 90 | 3e 91 | 0d 92 | 06 93 | 3c 94 | cd 95 | f2 96 | fd 97 | 05 98 | c2 99 | 5d 100 | fd 101 | b8 102 | 78 103 | c2 104 | 5b 105 | fd 106 | 7d 107 | 93 108 | 6f 109 | 7c 110 | 9a 111 | 67 112 | 23 113 | 05 114 | 7c 115 | b7 116 | c2 117 | 77 118 | fd 119 | 45 120 | 3e 121 | 3c 122 | cd 123 | f2 124 | fd 125 | 78 126 | cd 127 | f2 128 | fd 129 | 0e 130 | 00 131 | 7b 132 | cd 133 | f2 134 | fd 135 | 7a 136 | cd 137 | f2 138 | fd 139 | 1a 140 | cd 141 | f2 142 | fd 143 | 13 144 | 2b 145 | 05 146 | c2 147 | 8a 148 | fd 149 | 79 150 | cd 151 | f2 152 | fd 153 | 7c 154 | b5 155 | c2 156 | 70 157 | fd 158 | 3e 159 | 0d 160 | cd 161 | f2 162 | fd 163 | 3e 164 | 0a 165 | c3 166 | f2 167 | fd 168 | 06 169 | 06 170 | 03 171 | 21 172 | 00 173 | 00 174 | cd 175 | e8 176 | fd 177 | 4f 178 | fe 179 | 20 180 | 37 181 | c8 182 | e6 183 | b8 184 | ee 185 | 30 186 | c2 187 | 47 188 | fd 189 | 79 190 | e6 191 | 07 192 | 29 193 | 29 194 | 29 195 | 85 196 | 6f 197 | 05 198 | c2 199 | ad 200 | fd 201 | c9 202 | 06 203 | 06 204 | af 205 | c3 206 | d6 207 | fd 208 | 06 209 | 03 210 | e6 211 | 29 212 | 17 213 | 29 214 | 17 215 | 29 216 | 17 217 | e6 218 | 07 219 | f6 220 | 30 221 | cd 222 | f2 223 | fd 224 | 05 225 | c2 226 | d2 227 | fd 228 | 3e 229 | 20 230 | c3 231 | f2 232 | fd 233 | db 234 | 10 235 | 0f 236 | d2 237 | e8 238 | fd 239 | db 240 | 11 241 | e6 242 | 7f 243 | f5 244 | 81 245 | 4f 246 | db 247 | 10 248 | 0f 249 | 0f 250 | d2 251 | f5 252 | fd 253 | f1 254 | d3 255 | 11 256 | c9 257 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/01-Simple/Makefile: -------------------------------------------------------------------------------- 1 | RISCV_TOOLS_PREFIX = riscv64-unknown-elf- 2 | CXX = $(RISCV_TOOLS_PREFIX)g++ 3 | CC = $(RISCV_TOOLS_PREFIX)gcc 4 | AS = $(RISCV_TOOLS_PREFIX)gcc 5 | ICEPROG = iceprog 6 | 7 | all: firmware.bin 8 | 9 | # ---- iCE40 UP5k Breakout Board ---- 10 | 11 | upload: firmware.bin 12 | $(ICEPROG) -o 1M firmware.bin 13 | 14 | # ---- Example Firmware ---- 15 | firmware.elf: sections.lds start.s firmware.c sections.c 16 | $(CC) -O3 -nostartfiles -mabi=ilp32 -march=rv32ic -Wl,-Bstatic,-T,sections.lds,--strip-debug -ffreestanding -o firmware.elf start.s sections.c firmware.c -lgcc 17 | 18 | firmware.bin: firmware.elf 19 | $(RISCV_TOOLS_PREFIX)objcopy -O binary firmware.elf firmware.bin 20 | 21 | # ---- Clean ---- 22 | 23 | clean: 24 | rm -f firmware.elf firmware.bin 25 | 26 | .PHONY: upload clean 27 | 28 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/01-Simple/sections.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern uint32_t _sidata, _sdata, _edata; 5 | 6 | void executable_init_sections() 7 | { 8 | for (uint32_t *src = &_sidata, *dest = &_sdata; dest < &_edata;) { 9 | *dest++ = *src++; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013, 2014 Damien P. George 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/drivers/README.md: -------------------------------------------------------------------------------- 1 | This directory contains drivers for specific hardware. The drivers are 2 | intended to work across multiple ports. 3 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/drivers/bus/qspi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017-2018 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_DRIVERS_BUS_QSPI_H 27 | #define MICROPY_INCLUDED_DRIVERS_BUS_QSPI_H 28 | 29 | #include "py/mphal.h" 30 | 31 | enum { 32 | MP_QSPI_IOCTL_INIT, 33 | MP_QSPI_IOCTL_DEINIT, 34 | MP_QSPI_IOCTL_BUS_ACQUIRE, 35 | MP_QSPI_IOCTL_BUS_RELEASE, 36 | }; 37 | 38 | typedef struct _mp_qspi_proto_t { 39 | int (*ioctl)(void *self, uint32_t cmd); 40 | void (*write_cmd_data)(void *self, uint8_t cmd, size_t len, uint32_t data); 41 | void (*write_cmd_addr_data)(void *self, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src); 42 | uint32_t (*read_cmd)(void *self, uint8_t cmd, size_t len); 43 | void (*read_cmd_qaddr_qdata)(void *self, uint8_t cmd, uint32_t addr, size_t len, uint8_t *dest); 44 | } mp_qspi_proto_t; 45 | 46 | typedef struct _mp_soft_qspi_obj_t { 47 | mp_hal_pin_obj_t cs; 48 | mp_hal_pin_obj_t clk; 49 | mp_hal_pin_obj_t io0; 50 | mp_hal_pin_obj_t io1; 51 | mp_hal_pin_obj_t io2; 52 | mp_hal_pin_obj_t io3; 53 | } mp_soft_qspi_obj_t; 54 | 55 | extern const mp_qspi_proto_t mp_soft_qspi_proto; 56 | 57 | #endif // MICROPY_INCLUDED_DRIVERS_BUS_QSPI_H 58 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/drivers/bus/spi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016-2018 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_DRIVERS_BUS_SPI_H 27 | #define MICROPY_INCLUDED_DRIVERS_BUS_SPI_H 28 | 29 | #include "py/mphal.h" 30 | 31 | enum { 32 | MP_SPI_IOCTL_INIT, 33 | MP_SPI_IOCTL_DEINIT, 34 | }; 35 | 36 | typedef struct _mp_spi_proto_t { 37 | int (*ioctl)(void *self, uint32_t cmd); 38 | void (*transfer)(void *self, size_t len, const uint8_t *src, uint8_t *dest); 39 | } mp_spi_proto_t; 40 | 41 | typedef struct _mp_soft_spi_obj_t { 42 | uint32_t delay_half; // microsecond delay for half SCK period 43 | uint8_t polarity; 44 | uint8_t phase; 45 | mp_hal_pin_obj_t sck; 46 | mp_hal_pin_obj_t mosi; 47 | mp_hal_pin_obj_t miso; 48 | } mp_soft_spi_obj_t; 49 | 50 | extern const mp_spi_proto_t mp_soft_spi_proto; 51 | 52 | int mp_soft_spi_ioctl(void *self, uint32_t cmd); 53 | void mp_soft_spi_transfer(void *self, size_t len, const uint8_t *src, uint8_t *dest); 54 | 55 | #endif // MICROPY_INCLUDED_DRIVERS_BUS_SPI_H 56 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/machine_mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_MEM_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_MEM_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _machine_mem_obj_t { 32 | mp_obj_base_t base; 33 | unsigned elem_size; // in bytes 34 | } machine_mem_obj_t; 35 | 36 | extern const mp_obj_type_t machine_mem_type; 37 | 38 | extern const machine_mem_obj_t machine_mem8_obj; 39 | extern const machine_mem_obj_t machine_mem16_obj; 40 | extern const machine_mem_obj_t machine_mem32_obj; 41 | 42 | #if defined(MICROPY_MACHINE_MEM_GET_READ_ADDR) 43 | uintptr_t MICROPY_MACHINE_MEM_GET_READ_ADDR(mp_obj_t addr_o, uint align); 44 | #endif 45 | #if defined(MICROPY_MACHINE_MEM_GET_WRITE_ADDR) 46 | uintptr_t MICROPY_MACHINE_MEM_GET_WRITE_ADDR(mp_obj_t addr_o, uint align); 47 | #endif 48 | 49 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_MEM_H 50 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/machine_pinbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H 28 | 29 | #include "py/obj.h" 30 | 31 | extern const mp_obj_type_t machine_pinbase_type; 32 | 33 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H 34 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/machine_pulse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H 28 | 29 | #include "py/obj.h" 30 | #include "py/mphal.h" 31 | 32 | mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us); 33 | 34 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj); 35 | 36 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H 37 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/machine_signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H 28 | 29 | #include "py/obj.h" 30 | 31 | extern const mp_obj_type_t machine_signal_type; 32 | 33 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H 34 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014-2016 Damien P. George 7 | * Copyright (c) 2016 Paul Sokolovsky 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #ifndef MICROPY_INCLUDED_EXTMOD_MISC_H 28 | #define MICROPY_INCLUDED_EXTMOD_MISC_H 29 | 30 | // This file contains cumulative declarations for extmod/ . 31 | 32 | #include 33 | #include "py/runtime.h" 34 | 35 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj); 36 | 37 | #if MICROPY_PY_OS_DUPTERM 38 | int mp_uos_dupterm_rx_chr(void); 39 | void mp_uos_dupterm_tx_strn(const char *str, size_t len); 40 | void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc); 41 | #else 42 | #define mp_uos_dupterm_tx_strn(s, l) 43 | #endif 44 | 45 | #endif // MICROPY_INCLUDED_EXTMOD_MISC_H 46 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/modubinascii.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MODUBINASCII_H 27 | #define MICROPY_INCLUDED_EXTMOD_MODUBINASCII_H 28 | 29 | extern mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args); 30 | extern mp_obj_t mod_binascii_unhexlify(mp_obj_t data); 31 | extern mp_obj_t mod_binascii_a2b_base64(mp_obj_t data); 32 | extern mp_obj_t mod_binascii_b2a_base64(mp_obj_t data); 33 | extern mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args); 34 | 35 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj); 36 | MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj); 37 | MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj); 38 | MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj); 39 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj); 40 | 41 | #endif // MICROPY_INCLUDED_EXTMOD_MODUBINASCII_H 42 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/modwebsocket.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_EXTMOD_MODWEBSOCKET_H 2 | #define MICROPY_INCLUDED_EXTMOD_MODWEBSOCKET_H 3 | 4 | #define FRAME_OPCODE_MASK 0x0f 5 | enum { 6 | FRAME_CONT, FRAME_TXT, FRAME_BIN, 7 | FRAME_CLOSE = 0x8, FRAME_PING, FRAME_PONG 8 | }; 9 | 10 | #endif // MICROPY_INCLUDED_EXTMOD_MODWEBSOCKET_H 11 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/utime_mphal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * Copyright (c) 2016 Paul Sokolovsky 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #ifndef MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H 28 | #define MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H 29 | 30 | #include "py/obj.h" 31 | 32 | MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_obj); 33 | MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj); 34 | MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj); 35 | MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj); 36 | MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj); 37 | MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj); 38 | MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj); 39 | MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj); 40 | 41 | #endif // MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H 42 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/virtpin.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "extmod/virtpin.h" 28 | 29 | int mp_virtual_pin_read(mp_obj_t pin) { 30 | mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(pin); 31 | mp_pin_p_t *pin_p = (mp_pin_p_t*)s->type->protocol; 32 | return pin_p->ioctl(pin, MP_PIN_READ, 0, NULL); 33 | } 34 | 35 | void mp_virtual_pin_write(mp_obj_t pin, int value) { 36 | mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(pin); 37 | mp_pin_p_t *pin_p = (mp_pin_p_t*)s->type->protocol; 38 | pin_p->ioctl(pin, MP_PIN_WRITE, value, NULL); 39 | } 40 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/extmod/virtpin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_VIRTPIN_H 27 | #define MICROPY_INCLUDED_EXTMOD_VIRTPIN_H 28 | 29 | #include "py/obj.h" 30 | 31 | #define MP_PIN_READ (1) 32 | #define MP_PIN_WRITE (2) 33 | #define MP_PIN_INPUT (3) 34 | #define MP_PIN_OUTPUT (4) 35 | 36 | // Pin protocol 37 | typedef struct _mp_pin_p_t { 38 | mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode); 39 | } mp_pin_p_t; 40 | 41 | int mp_virtual_pin_read(mp_obj_t pin); 42 | void mp_virtual_pin_write(mp_obj_t pin, int value); 43 | 44 | // If a port exposes a Pin object, it's constructor should be like this 45 | mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); 46 | 47 | #endif // MICROPY_INCLUDED_EXTMOD_VIRTPIN_H 48 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/README.md: -------------------------------------------------------------------------------- 1 | This directory contains standard, low-level C libraries with emphasis on 2 | being independent and efficient. They can be used by any port. 3 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/embed/abort_.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | NORETURN void abort_(void); 4 | 5 | NORETURN void abort_(void) { 6 | mp_raise_msg(&mp_type_RuntimeError, "abort() called"); 7 | } 8 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/mp-readline/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H 27 | #define MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H 28 | 29 | #define CHAR_CTRL_A (1) 30 | #define CHAR_CTRL_B (2) 31 | #define CHAR_CTRL_C (3) 32 | #define CHAR_CTRL_D (4) 33 | #define CHAR_CTRL_E (5) 34 | #define CHAR_CTRL_F (6) 35 | #define CHAR_CTRL_K (11) 36 | #define CHAR_CTRL_N (14) 37 | #define CHAR_CTRL_P (16) 38 | #define CHAR_CTRL_U (21) 39 | 40 | void readline_init0(void); 41 | int readline(vstr_t *line, const char *prompt); 42 | void readline_push_history(const char *line); 43 | 44 | void readline_init(vstr_t *line, const char *prompt); 45 | void readline_note_newline(const char *prompt); 46 | int readline_process_char(int c); 47 | 48 | #endif // MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H 49 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/utils/interrupt_char.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/obj.h" 28 | #include "py/mpstate.h" 29 | 30 | #if MICROPY_KBD_EXCEPTION 31 | 32 | int mp_interrupt_char; 33 | 34 | void mp_hal_set_interrupt_char(int c) { 35 | if (c != -1) { 36 | mp_obj_exception_clear_traceback(MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception))); 37 | } 38 | mp_interrupt_char = c; 39 | } 40 | 41 | void mp_keyboard_interrupt(void) { 42 | MP_STATE_VM(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_kbd_exception)); 43 | #if MICROPY_ENABLE_SCHEDULER 44 | if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) { 45 | MP_STATE_VM(sched_state) = MP_SCHED_PENDING; 46 | } 47 | #endif 48 | } 49 | 50 | #endif 51 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/utils/interrupt_char.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_LIB_UTILS_INTERRUPT_CHAR_H 27 | #define MICROPY_INCLUDED_LIB_UTILS_INTERRUPT_CHAR_H 28 | 29 | extern int mp_interrupt_char; 30 | void mp_hal_set_interrupt_char(int c); 31 | void mp_keyboard_interrupt(void); 32 | 33 | #endif // MICROPY_INCLUDED_LIB_UTILS_INTERRUPT_CHAR_H 34 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/utils/pyexec.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_LIB_UTILS_PYEXEC_H 27 | #define MICROPY_INCLUDED_LIB_UTILS_PYEXEC_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef enum { 32 | PYEXEC_MODE_RAW_REPL, 33 | PYEXEC_MODE_FRIENDLY_REPL, 34 | } pyexec_mode_kind_t; 35 | 36 | extern pyexec_mode_kind_t pyexec_mode_kind; 37 | 38 | // Set this to the value (eg PYEXEC_FORCED_EXIT) that will be propagated through 39 | // the pyexec functions if a SystemExit exception is raised by the running code. 40 | // It will reset to 0 at the start of each execution (eg each REPL entry). 41 | extern int pyexec_system_exit; 42 | 43 | #define PYEXEC_FORCED_EXIT (0x100) 44 | #define PYEXEC_SWITCH_MODE (0x200) 45 | 46 | int pyexec_raw_repl(void); 47 | int pyexec_friendly_repl(void); 48 | int pyexec_file(const char *filename); 49 | int pyexec_frozen_module(const char *name); 50 | void pyexec_event_repl_init(void); 51 | int pyexec_event_repl_process_char(int c); 52 | extern uint8_t pyexec_repl_active; 53 | 54 | MP_DECLARE_CONST_FUN_OBJ_1(pyb_set_repl_info_obj); 55 | 56 | #endif // MICROPY_INCLUDED_LIB_UTILS_PYEXEC_H 57 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/lib/utils/stdout_helpers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "py/mpconfig.h" 4 | #include "py/mphal.h" 5 | 6 | /* 7 | * Extra stdout functions 8 | * These can be either optimized for a particular port, or reference 9 | * implementation below can be used. 10 | */ 11 | 12 | // Send "cooked" string of given length, where every occurrence of 13 | // LF character is replaced with CR LF. 14 | void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { 15 | while (len--) { 16 | if (*str == '\n') { 17 | mp_hal_stdout_tx_strn("\r", 1); 18 | } 19 | mp_hal_stdout_tx_strn(str++, 1); 20 | } 21 | } 22 | 23 | // Send zero-terminated string 24 | void mp_hal_stdout_tx_str(const char *str) { 25 | mp_hal_stdout_tx_strn(str, strlen(str)); 26 | } 27 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/Makefile: -------------------------------------------------------------------------------- 1 | include ../../py/mkenv.mk 2 | 3 | CROSS = 1 4 | 5 | # qstr definitions (must come before including py.mk) 6 | QSTR_DEFS = qstrdefsport.h 7 | 8 | # include py core make definitions 9 | include $(TOP)/py/py.mk 10 | CROSS_COMPILE = riscv64-unknown-elf- 11 | 12 | INC += -I. 13 | INC += -I$(TOP) 14 | INC += -I$(BUILD) 15 | 16 | CFLAGS_RISCV = -mabi=ilp32 -march=rv32ic -ffreestanding -nostartfiles 17 | CFLAGS = $(INC) -Wall -Werror -std=c99 $(COPT) $(CFLAGS_RISCV) -Os 18 | LDFLAGS = -nostartfiles -Wl,-Bstatic,-T,sections.lds,--strip-debug,-Map=$@.map,--cref -ffreestanding -mabi=ilp32 -march=rv32ic 19 | 20 | LIBS = -lm -lc -lgcc 21 | 22 | SRC_C = \ 23 | main.c \ 24 | sections.c \ 25 | board.c \ 26 | modpyb.c \ 27 | modpybled.c \ 28 | modpyblcd.c \ 29 | modpybswitch.c \ 30 | lib/utils/stdout_helpers.c \ 31 | lib/utils/pyexec.c \ 32 | lib/mp-readline/readline.c 33 | 34 | OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) 35 | 36 | all: $(BUILD)/firmware.bin 37 | 38 | $(BUILD)/start.o: start.s 39 | $(Q)$(CC) -c $(CFLAGS) -o $@ $< 40 | 41 | $(BUILD)/firmware.elf: $(OBJ) $(BUILD)/start.o sections.lds 42 | $(ECHO) "LINK $@" 43 | $(Q)$(CC) $(LDFLAGS) -o $@ $(BUILD)/start.o $(OBJ) $(LIBS) 44 | $(Q)$(SIZE) $@ 45 | 46 | $(BUILD)/firmware.bin: $(BUILD)/firmware.elf 47 | $(CROSS_COMPILE)objcopy -O binary $(BUILD)/firmware.elf $(BUILD)/firmware.bin 48 | 49 | # ---- iCE40 UP5k Breakout Board ---- 50 | 51 | upload: $(BUILD)/firmware.bin 52 | iceprog -o 1M $(BUILD)/firmware.bin 53 | 54 | # ---- Clean ---- 55 | clean: myclean 56 | myclean: 57 | rm -f micropython.blif micropython.log micropython.asc micropython.rpt micropython.bin 58 | 59 | .PHONY: upload clean 60 | 61 | # List of sources for qstr extraction 62 | SRC_QSTR += $(SRC_C) mpconfigport.h 63 | # Append any auto-generated sources that are needed by sources listed in 64 | # SRC_QSTR 65 | SRC_QSTR_AUTO_DEPS += 66 | 67 | include $(TOP)/py/mkrules.mk 68 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/board.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_UPDUINO_BOARD_H 2 | #define MICROPY_INCLUDED_UPDUINO_BOARD_H 3 | 4 | int switch_get(int sw); 5 | 6 | void led_init(void); 7 | void led_state(int led, int state); 8 | void led_toggle(int led); 9 | 10 | int lcd_pos_x(); 11 | int lcd_pos_y(); 12 | void lcd_write(const char *data, int len); 13 | void lcd_clear(); 14 | void lcd_move(int xpos, int ypos); 15 | 16 | #endif // MICROPY_INCLUDED_UPDUINO_BOARD_H 17 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/modpyb.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_UPDUINO_MODPYB_H 27 | #define MICROPY_INCLUDED_UPDUINO_MODPYB_H 28 | 29 | extern const mp_obj_type_t pyb_led_type; 30 | extern const mp_obj_type_t pyb_lcd_type; 31 | extern const mp_obj_type_t pyb_switch_type; 32 | extern const mp_obj_module_t pyb_module; 33 | 34 | #endif // MICROPY_INCLUDED_UPDUINO_MODPYB_H 35 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/mphalport.h: -------------------------------------------------------------------------------- 1 | static inline mp_uint_t mp_hal_ticks_ms(void) { return 0; } 2 | static inline void mp_hal_set_interrupt_char(char c) {} 3 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/qstrdefsport.h: -------------------------------------------------------------------------------- 1 | // qstrs specific to this port 2 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/ports/picorv32/sections.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | extern uint32_t _sidata, _sdata, _edata; 5 | 6 | void executable_init_sections() 7 | { 8 | for (uint32_t *src = &_sidata, *dest = &_sdata; dest < &_edata;) { 9 | *dest++ = *src++; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/__pycache__/makeqstrdata.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/tutorials/12-RiscV/02-Micropython/py/__pycache__/makeqstrdata.cpython-35.pyc -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/binary.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_BINARY_H 27 | #define MICROPY_INCLUDED_PY_BINARY_H 28 | 29 | #include "py/obj.h" 30 | 31 | // Use special typecode to differentiate repr() of bytearray vs array.array('B') 32 | // (underlyingly they're same). Can't use 0 here because that's used to detect 33 | // type-specification errors due to end-of-string. 34 | #define BYTEARRAY_TYPECODE 1 35 | 36 | size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign); 37 | mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index); 38 | void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in); 39 | void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val); 40 | mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr); 41 | void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr); 42 | long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, const byte *src); 43 | void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val); 44 | 45 | #endif // MICROPY_INCLUDED_PY_BINARY_H 46 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/compile.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_COMPILE_H 27 | #define MICROPY_INCLUDED_PY_COMPILE_H 28 | 29 | #include "py/lexer.h" 30 | #include "py/parse.h" 31 | #include "py/emitglue.h" 32 | 33 | // These must fit in 8 bits; see scope.h 34 | enum { 35 | MP_EMIT_OPT_NONE, 36 | MP_EMIT_OPT_BYTECODE, 37 | MP_EMIT_OPT_NATIVE_PYTHON, 38 | MP_EMIT_OPT_VIPER, 39 | MP_EMIT_OPT_ASM, 40 | }; 41 | 42 | // the compiler will raise an exception if an error occurred 43 | // the compiler will clear the parse tree before it returns 44 | mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl); 45 | 46 | #if MICROPY_PERSISTENT_CODE_SAVE 47 | // this has the same semantics as mp_compile 48 | mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, uint emit_opt, bool is_repl); 49 | #endif 50 | 51 | // this is implemented in runtime.c 52 | mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_input_kind, mp_obj_dict_t *globals, mp_obj_dict_t *locals); 53 | 54 | #endif // MICROPY_INCLUDED_PY_COMPILE_H 55 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/formatfloat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_FORMATFLOAT_H 27 | #define MICROPY_INCLUDED_PY_FORMATFLOAT_H 28 | 29 | #include "py/mpconfig.h" 30 | 31 | #if MICROPY_PY_BUILTINS_FLOAT 32 | int mp_format_float(mp_float_t f, char *buf, size_t bufSize, char fmt, int prec, char sign); 33 | #endif 34 | 35 | #endif // MICROPY_INCLUDED_PY_FORMATFLOAT_H 36 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/frozenmod.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_FROZENMOD_H 27 | #define MICROPY_INCLUDED_PY_FROZENMOD_H 28 | 29 | #include "py/lexer.h" 30 | 31 | enum { 32 | MP_FROZEN_NONE, 33 | MP_FROZEN_STR, 34 | MP_FROZEN_MPY, 35 | }; 36 | 37 | int mp_find_frozen_module(const char *str, size_t len, void **data); 38 | const char *mp_find_frozen_str(const char *str, size_t *len); 39 | mp_import_stat_t mp_frozen_stat(const char *str); 40 | 41 | #endif // MICROPY_INCLUDED_PY_FROZENMOD_H 42 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/makeqstrdata.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/tutorials/12-RiscV/02-Micropython/py/makeqstrdata.pyc -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/mkenv.mk: -------------------------------------------------------------------------------- 1 | ifneq ($(lastword a b),b) 2 | $(error These Makefiles require make 3.81 or newer) 3 | endif 4 | 5 | # Set TOP to be the path to get from the current directory (where make was 6 | # invoked) to the top of the tree. $(lastword $(MAKEFILE_LIST)) returns 7 | # the name of this makefile relative to where make was invoked. 8 | # 9 | # We assume that this file is in the py directory so we use $(dir ) twice 10 | # to get to the top of the tree. 11 | 12 | THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) 13 | TOP := $(patsubst %/py/mkenv.mk,%,$(THIS_MAKEFILE)) 14 | 15 | # Turn on increased build verbosity by defining BUILD_VERBOSE in your main 16 | # Makefile or in your environment. You can also use V=1 on the make command 17 | # line. 18 | 19 | ifeq ("$(origin V)", "command line") 20 | BUILD_VERBOSE=$(V) 21 | endif 22 | ifndef BUILD_VERBOSE 23 | BUILD_VERBOSE = 0 24 | endif 25 | ifeq ($(BUILD_VERBOSE),0) 26 | Q = @ 27 | else 28 | Q = 29 | endif 30 | # Since this is a new feature, advertise it 31 | ifeq ($(BUILD_VERBOSE),0) 32 | $(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.) 33 | endif 34 | 35 | # default settings; can be overridden in main Makefile 36 | 37 | PY_SRC ?= $(TOP)/py 38 | BUILD ?= build 39 | 40 | RM = rm 41 | ECHO = @echo 42 | CP = cp 43 | MKDIR = mkdir 44 | SED = sed 45 | PYTHON = python 46 | 47 | AS = $(CROSS_COMPILE)as 48 | CC = $(CROSS_COMPILE)gcc 49 | CXX = $(CROSS_COMPILE)g++ 50 | LD = $(CROSS_COMPILE)ld 51 | OBJCOPY = $(CROSS_COMPILE)objcopy 52 | SIZE = $(CROSS_COMPILE)size 53 | STRIP = $(CROSS_COMPILE)strip 54 | AR = $(CROSS_COMPILE)ar 55 | ifeq ($(MICROPY_FORCE_32BIT),1) 56 | CC += -m32 57 | CXX += -m32 58 | LD += -m32 59 | endif 60 | 61 | MAKE_FROZEN = $(PYTHON) $(TOP)/tools/make-frozen.py 62 | MPY_CROSS = $(TOP)/mpy-cross/mpy-cross 63 | MPY_TOOL = $(PYTHON) $(TOP)/tools/mpy-tool.py 64 | 65 | all: 66 | .PHONY: all 67 | 68 | .DELETE_ON_ERROR: 69 | 70 | MKENV_INCLUDED = 1 71 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/modarray.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/builtin.h" 28 | 29 | #if MICROPY_PY_ARRAY 30 | 31 | STATIC const mp_rom_map_elem_t mp_module_array_globals_table[] = { 32 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_array) }, 33 | { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mp_type_array) }, 34 | }; 35 | 36 | STATIC MP_DEFINE_CONST_DICT(mp_module_array_globals, mp_module_array_globals_table); 37 | 38 | const mp_obj_module_t mp_module_array = { 39 | .base = { &mp_type_module }, 40 | .globals = (mp_obj_dict_t*)&mp_module_array_globals, 41 | }; 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/modcollections.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/builtin.h" 28 | 29 | #if MICROPY_PY_COLLECTIONS 30 | 31 | STATIC const mp_rom_map_elem_t mp_module_collections_globals_table[] = { 32 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ucollections) }, 33 | #if MICROPY_PY_COLLECTIONS_DEQUE 34 | { MP_ROM_QSTR(MP_QSTR_deque), MP_ROM_PTR(&mp_type_deque) }, 35 | #endif 36 | { MP_ROM_QSTR(MP_QSTR_namedtuple), MP_ROM_PTR(&mp_namedtuple_obj) }, 37 | #if MICROPY_PY_COLLECTIONS_ORDEREDDICT 38 | { MP_ROM_QSTR(MP_QSTR_OrderedDict), MP_ROM_PTR(&mp_type_ordereddict) }, 39 | #endif 40 | }; 41 | 42 | STATIC MP_DEFINE_CONST_DICT(mp_module_collections_globals, mp_module_collections_globals_table); 43 | 44 | const mp_obj_module_t mp_module_collections = { 45 | .base = { &mp_type_module }, 46 | .globals = (mp_obj_dict_t*)&mp_module_collections_globals, 47 | }; 48 | 49 | #endif // MICROPY_PY_COLLECTIONS 50 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/mpstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpstate.h" 28 | 29 | #if MICROPY_DYNAMIC_COMPILER 30 | mp_dynamic_compiler_t mp_dynamic_compiler = {0}; 31 | #endif 32 | 33 | mp_state_ctx_t mp_state_ctx; 34 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/nlr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2017 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpstate.h" 28 | 29 | #if !MICROPY_NLR_SETJMP 30 | // When not using setjmp, nlr_push_tail is called from inline asm so needs special care 31 | #if MICROPY_NLR_X86 && MICROPY_NLR_OS_WINDOWS 32 | // On these 32-bit platforms make sure nlr_push_tail doesn't have a leading underscore 33 | unsigned int nlr_push_tail(nlr_buf_t *nlr) asm("nlr_push_tail"); 34 | #else 35 | // LTO can't see inside inline asm functions so explicitly mark nlr_push_tail as used 36 | __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr); 37 | #endif 38 | #endif 39 | 40 | unsigned int nlr_push_tail(nlr_buf_t *nlr) { 41 | nlr_buf_t **top = &MP_STATE_THREAD(nlr_top); 42 | nlr->prev = *top; 43 | MP_NLR_SAVE_PYSTACK(nlr); 44 | *top = nlr; 45 | return 0; // normal return 46 | } 47 | 48 | void nlr_pop(void) { 49 | nlr_buf_t **top = &MP_STATE_THREAD(nlr_top); 50 | *top = (*top)->prev; 51 | } 52 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/nlrsetjmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2017 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpstate.h" 28 | 29 | #if MICROPY_NLR_SETJMP 30 | 31 | void nlr_jump(void *val) { 32 | nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top); 33 | nlr_buf_t *top = *top_ptr; 34 | if (top == NULL) { 35 | nlr_jump_fail(val); 36 | } 37 | top->ret_val = val; 38 | MP_NLR_RESTORE_PYSTACK(top); 39 | *top_ptr = top->prev; 40 | longjmp(top->jmpbuf, 1); 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objarray.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * Copyright (c) 2014 Paul Sokolovsky 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | #ifndef MICROPY_INCLUDED_PY_OBJARRAY_H 28 | #define MICROPY_INCLUDED_PY_OBJARRAY_H 29 | 30 | #include "py/obj.h" 31 | 32 | typedef struct _mp_obj_array_t { 33 | mp_obj_base_t base; 34 | size_t typecode : 8; 35 | // free is number of unused elements after len used elements 36 | // alloc size = len + free 37 | size_t free : (8 * sizeof(size_t) - 8); 38 | size_t len; // in elements 39 | void *items; 40 | } mp_obj_array_t; 41 | 42 | #endif // MICROPY_INCLUDED_PY_OBJARRAY_H 43 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objexcept.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJEXCEPT_H 27 | #define MICROPY_INCLUDED_PY_OBJEXCEPT_H 28 | 29 | #include "py/obj.h" 30 | #include "py/objtuple.h" 31 | 32 | typedef struct _mp_obj_exception_t { 33 | mp_obj_base_t base; 34 | size_t traceback_alloc : (8 * sizeof(size_t) / 2); 35 | size_t traceback_len : (8 * sizeof(size_t) / 2); 36 | size_t *traceback_data; 37 | mp_obj_tuple_t *args; 38 | } mp_obj_exception_t; 39 | 40 | #endif // MICROPY_INCLUDED_PY_OBJEXCEPT_H 41 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objfun.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJFUN_H 27 | #define MICROPY_INCLUDED_PY_OBJFUN_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_fun_bc_t { 32 | mp_obj_base_t base; 33 | mp_obj_dict_t *globals; // the context within which this function was defined 34 | const byte *bytecode; // bytecode for the function 35 | const mp_uint_t *const_table; // constant table 36 | // the following extra_args array is allocated space to take (in order): 37 | // - values of positional default args (if any) 38 | // - a single slot for default kw args dict (if it has them) 39 | // - a single slot for var args tuple (if it takes them) 40 | // - a single slot for kw args dict (if it takes them) 41 | mp_obj_t extra_args[]; 42 | } mp_obj_fun_bc_t; 43 | 44 | #endif // MICROPY_INCLUDED_PY_OBJFUN_H 45 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objgenerator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJGENERATOR_H 27 | #define MICROPY_INCLUDED_PY_OBJGENERATOR_H 28 | 29 | #include "py/obj.h" 30 | #include "py/runtime.h" 31 | 32 | mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_val, mp_obj_t throw_val, mp_obj_t *ret_val); 33 | 34 | #endif // MICROPY_INCLUDED_PY_OBJGENERATOR_H 35 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJLIST_H 27 | #define MICROPY_INCLUDED_PY_OBJLIST_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_list_t { 32 | mp_obj_base_t base; 33 | size_t alloc; 34 | size_t len; 35 | mp_obj_t *items; 36 | } mp_obj_list_t; 37 | 38 | #endif // MICROPY_INCLUDED_PY_OBJLIST_H 39 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objmodule.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJMODULE_H 27 | #define MICROPY_INCLUDED_PY_OBJMODULE_H 28 | 29 | #include "py/obj.h" 30 | 31 | extern const mp_map_t mp_builtin_module_map; 32 | extern const mp_map_t mp_builtin_module_weak_links_map; 33 | 34 | mp_obj_t mp_module_get(qstr module_name); 35 | void mp_module_register(qstr qstr, mp_obj_t module); 36 | 37 | #if MICROPY_MODULE_BUILTIN_INIT 38 | void mp_module_call_init(qstr module_name, mp_obj_t module_obj); 39 | #else 40 | static inline void mp_module_call_init(qstr module_name, mp_obj_t module_obj) { 41 | (void)module_name; 42 | (void)module_obj; 43 | } 44 | #endif 45 | 46 | #endif // MICROPY_INCLUDED_PY_OBJMODULE_H 47 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objnamedtuple.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014-2017 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H 27 | #define MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H 28 | 29 | #include "py/objtuple.h" 30 | 31 | typedef struct _mp_obj_namedtuple_type_t { 32 | mp_obj_type_t base; 33 | size_t n_fields; 34 | qstr fields[]; 35 | } mp_obj_namedtuple_type_t; 36 | 37 | typedef struct _mp_obj_namedtuple_t { 38 | mp_obj_tuple_t tuple; 39 | } mp_obj_namedtuple_t; 40 | 41 | size_t mp_obj_namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name); 42 | mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields); 43 | 44 | #endif // MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H 45 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objnone.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_none_t { 32 | mp_obj_base_t base; 33 | } mp_obj_none_t; 34 | 35 | STATIC void none_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { 36 | (void)self_in; 37 | if (MICROPY_PY_UJSON && kind == PRINT_JSON) { 38 | mp_print_str(print, "null"); 39 | } else { 40 | mp_print_str(print, "None"); 41 | } 42 | } 43 | 44 | const mp_obj_type_t mp_type_NoneType = { 45 | { &mp_type_type }, 46 | .name = MP_QSTR_NoneType, 47 | .print = none_print, 48 | .unary_op = mp_generic_unary_op, 49 | }; 50 | 51 | const mp_obj_none_t mp_const_none_obj = {{&mp_type_NoneType}}; 52 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objpolyiter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "py/runtime.h" 30 | 31 | // This is universal iterator type which calls "iternext" method stored in 32 | // particular object instance. (So, each instance of this time can have its 33 | // own iteration behavior.) Having this type saves to define type objects 34 | // for various internal iterator objects. 35 | 36 | // Any instance should have these 2 fields at the beginning 37 | typedef struct _mp_obj_polymorph_iter_t { 38 | mp_obj_base_t base; 39 | mp_fun_1_t iternext; 40 | } mp_obj_polymorph_iter_t; 41 | 42 | STATIC mp_obj_t polymorph_it_iternext(mp_obj_t self_in) { 43 | mp_obj_polymorph_iter_t *self = MP_OBJ_TO_PTR(self_in); 44 | // Redirect call to object instance's iternext method 45 | return self->iternext(self_in); 46 | } 47 | 48 | const mp_obj_type_t mp_type_polymorph_iter = { 49 | { &mp_type_type }, 50 | .name = MP_QSTR_iterator, 51 | .getiter = mp_identity_getiter, 52 | .iternext = polymorph_it_iternext, 53 | }; 54 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objsingleton.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "py/obj.h" 31 | 32 | /******************************************************************************/ 33 | /* singleton objects defined by Python */ 34 | 35 | typedef struct _mp_obj_singleton_t { 36 | mp_obj_base_t base; 37 | qstr name; 38 | } mp_obj_singleton_t; 39 | 40 | STATIC void singleton_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { 41 | (void)kind; 42 | mp_obj_singleton_t *self = MP_OBJ_TO_PTR(self_in); 43 | mp_printf(print, "%q", self->name); 44 | } 45 | 46 | const mp_obj_type_t mp_type_singleton = { 47 | { &mp_type_type }, 48 | .name = MP_QSTR_, 49 | .print = singleton_print, 50 | }; 51 | 52 | const mp_obj_singleton_t mp_const_ellipsis_obj = {{&mp_type_singleton}, MP_QSTR_Ellipsis}; 53 | #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED 54 | const mp_obj_singleton_t mp_const_notimplemented_obj = {{&mp_type_singleton}, MP_QSTR_NotImplemented}; 55 | #endif 56 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/objstringio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJSTRINGIO_H 27 | #define MICROPY_INCLUDED_PY_OBJSTRINGIO_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_stringio_t { 32 | mp_obj_base_t base; 33 | vstr_t *vstr; 34 | // StringIO has single pointer used for both reading and writing 35 | mp_uint_t pos; 36 | // Underlying object buffered by this StringIO 37 | mp_obj_t ref_obj; 38 | } mp_obj_stringio_t; 39 | 40 | #endif // MICROPY_INCLUDED_PY_OBJSTRINGIO_H 41 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/opmethods.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/obj.h" 28 | #include "py/builtin.h" 29 | 30 | STATIC mp_obj_t op_getitem(mp_obj_t self_in, mp_obj_t key_in) { 31 | mp_obj_type_t *type = mp_obj_get_type(self_in); 32 | return type->subscr(self_in, key_in, MP_OBJ_SENTINEL); 33 | } 34 | MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem); 35 | 36 | STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { 37 | mp_obj_type_t *type = mp_obj_get_type(self_in); 38 | return type->subscr(self_in, key_in, value_in); 39 | } 40 | MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem); 41 | 42 | STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) { 43 | mp_obj_type_t *type = mp_obj_get_type(self_in); 44 | return type->subscr(self_in, key_in, MP_OBJ_NULL); 45 | } 46 | MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem); 47 | 48 | STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) { 49 | mp_obj_type_t *type = mp_obj_get_type(lhs_in); 50 | return type->binary_op(MP_BINARY_OP_CONTAINS, lhs_in, rhs_in); 51 | } 52 | MP_DEFINE_CONST_FUN_OBJ_2(mp_op_contains_obj, op_contains); 53 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/parsenum.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_PARSENUM_H 27 | #define MICROPY_INCLUDED_PY_PARSENUM_H 28 | 29 | #include "py/mpconfig.h" 30 | #include "py/lexer.h" 31 | #include "py/obj.h" 32 | 33 | // these functions raise a SyntaxError if lex!=NULL, else a ValueError 34 | mp_obj_t mp_parse_num_integer(const char *restrict str, size_t len, int base, mp_lexer_t *lex); 35 | mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex); 36 | 37 | #endif // MICROPY_INCLUDED_PY_PARSENUM_H 38 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/parsenumbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_PARSENUMBASE_H 27 | #define MICROPY_INCLUDED_PY_PARSENUMBASE_H 28 | 29 | #include "py/mpconfig.h" 30 | 31 | size_t mp_parse_num_base(const char *str, size_t len, int *base); 32 | 33 | #endif // MICROPY_INCLUDED_PY_PARSENUMBASE_H 34 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/persistentcode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_PERSISTENTCODE_H 27 | #define MICROPY_INCLUDED_PY_PERSISTENTCODE_H 28 | 29 | #include "py/mpprint.h" 30 | #include "py/reader.h" 31 | #include "py/emitglue.h" 32 | 33 | mp_raw_code_t *mp_raw_code_load(mp_reader_t *reader); 34 | mp_raw_code_t *mp_raw_code_load_mem(const byte *buf, size_t len); 35 | mp_raw_code_t *mp_raw_code_load_file(const char *filename); 36 | 37 | void mp_raw_code_save(mp_raw_code_t *rc, mp_print_t *print); 38 | void mp_raw_code_save_file(mp_raw_code_t *rc, const char *filename); 39 | 40 | #endif // MICROPY_INCLUDED_PY_PERSISTENTCODE_H 41 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/pystack.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "py/runtime.h" 30 | 31 | #if MICROPY_ENABLE_PYSTACK 32 | 33 | void mp_pystack_init(void *start, void *end) { 34 | MP_STATE_THREAD(pystack_start) = start; 35 | MP_STATE_THREAD(pystack_end) = end; 36 | MP_STATE_THREAD(pystack_cur) = start; 37 | } 38 | 39 | void *mp_pystack_alloc(size_t n_bytes) { 40 | n_bytes = (n_bytes + (MICROPY_PYSTACK_ALIGN - 1)) & ~(MICROPY_PYSTACK_ALIGN - 1); 41 | #if MP_PYSTACK_DEBUG 42 | n_bytes += MICROPY_PYSTACK_ALIGN; 43 | #endif 44 | if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) { 45 | // out of memory in the pystack 46 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError, 47 | MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted))); 48 | } 49 | void *ptr = MP_STATE_THREAD(pystack_cur); 50 | MP_STATE_THREAD(pystack_cur) += n_bytes; 51 | #if MP_PYSTACK_DEBUG 52 | *(size_t*)(MP_STATE_THREAD(pystack_cur) - MICROPY_PYSTACK_ALIGN) = n_bytes; 53 | #endif 54 | return ptr; 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/qstrdefs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpconfig.h" 28 | 29 | // All the qstr definitions in this file are available as constants. 30 | // That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx. 31 | 32 | // qstr configuration passed to makeqstrdata.py of the form QCFG(key, value) 33 | QCFG(BYTES_IN_LEN, MICROPY_QSTR_BYTES_IN_LEN) 34 | QCFG(BYTES_IN_HASH, MICROPY_QSTR_BYTES_IN_HASH) 35 | 36 | Q() 37 | Q(*) 38 | Q(_) 39 | Q(/) 40 | Q(%#o) 41 | Q(%#x) 42 | Q({:#b}) 43 | Q( ) 44 | Q(\n) 45 | Q(maximum recursion depth exceeded) 46 | Q() 47 | Q() 48 | Q() 49 | Q() 50 | Q() 51 | Q() 52 | Q() 53 | Q() 54 | Q(utf-8) 55 | 56 | #if MICROPY_ENABLE_PYSTACK 57 | Q(pystack exhausted) 58 | #endif 59 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_READER_H 27 | #define MICROPY_INCLUDED_PY_READER_H 28 | 29 | #include "py/obj.h" 30 | 31 | // the readbyte function must return the next byte in the input stream 32 | // it must return MP_READER_EOF if end of stream 33 | // it can be called again after returning MP_READER_EOF, and in that case must return MP_READER_EOF 34 | #define MP_READER_EOF ((mp_uint_t)(-1)) 35 | 36 | typedef struct _mp_reader_t { 37 | void *data; 38 | mp_uint_t (*readbyte)(void *data); 39 | void (*close)(void *data); 40 | } mp_reader_t; 41 | 42 | void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len); 43 | void mp_reader_new_file(mp_reader_t *reader, const char *filename); 44 | void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd); 45 | 46 | #endif // MICROPY_INCLUDED_PY_READER_H 47 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/repl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_REPL_H 27 | #define MICROPY_INCLUDED_PY_REPL_H 28 | 29 | #include "py/mpconfig.h" 30 | #include "py/misc.h" 31 | #include "py/mpprint.h" 32 | 33 | #if MICROPY_HELPER_REPL 34 | bool mp_repl_continue_with_input(const char *input); 35 | size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str); 36 | #endif 37 | 38 | #endif // MICROPY_INCLUDED_PY_REPL_H 39 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/runtime_utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Josef Gajdusek 7 | * Copyright (c) 2015 Paul Sokolovsky 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in 17 | * all copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 25 | * THE SOFTWARE. 26 | */ 27 | 28 | #include "py/runtime.h" 29 | 30 | mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) { 31 | nlr_buf_t nlr; 32 | if (nlr_push(&nlr) == 0) { 33 | mp_obj_t ret = mp_call_function_1(fun, arg); 34 | nlr_pop(); 35 | return ret; 36 | } else { 37 | mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); 38 | return MP_OBJ_NULL; 39 | } 40 | } 41 | 42 | mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) { 43 | nlr_buf_t nlr; 44 | if (nlr_push(&nlr) == 0) { 45 | mp_obj_t ret = mp_call_function_2(fun, arg1, arg2); 46 | nlr_pop(); 47 | return ret; 48 | } else { 49 | mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); 50 | return MP_OBJ_NULL; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/stackctrl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/runtime.h" 28 | #include "py/stackctrl.h" 29 | 30 | void mp_stack_ctrl_init(void) { 31 | volatile int stack_dummy; 32 | MP_STATE_THREAD(stack_top) = (char*)&stack_dummy; 33 | } 34 | 35 | void mp_stack_set_top(void *top) { 36 | MP_STATE_THREAD(stack_top) = top; 37 | } 38 | 39 | mp_uint_t mp_stack_usage(void) { 40 | // Assumes descending stack 41 | volatile int stack_dummy; 42 | return MP_STATE_THREAD(stack_top) - (char*)&stack_dummy; 43 | } 44 | 45 | #if MICROPY_STACK_CHECK 46 | 47 | void mp_stack_set_limit(mp_uint_t limit) { 48 | MP_STATE_THREAD(stack_limit) = limit; 49 | } 50 | 51 | void mp_stack_check(void) { 52 | if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) { 53 | mp_raise_recursion_depth(); 54 | } 55 | } 56 | 57 | #endif // MICROPY_STACK_CHECK 58 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/stackctrl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_STACKCTRL_H 27 | #define MICROPY_INCLUDED_PY_STACKCTRL_H 28 | 29 | #include "py/mpconfig.h" 30 | 31 | void mp_stack_ctrl_init(void); 32 | void mp_stack_set_top(void *top); 33 | mp_uint_t mp_stack_usage(void); 34 | 35 | #if MICROPY_STACK_CHECK 36 | 37 | void mp_stack_set_limit(mp_uint_t limit); 38 | void mp_stack_check(void); 39 | #define MP_STACK_CHECK() mp_stack_check() 40 | 41 | #else 42 | 43 | #define mp_stack_set_limit(limit) 44 | #define MP_STACK_CHECK() 45 | 46 | #endif 47 | 48 | #endif // MICROPY_INCLUDED_PY_STACKCTRL_H 49 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/unicode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_UNICODE_H 27 | #define MICROPY_INCLUDED_PY_UNICODE_H 28 | 29 | #include "py/mpconfig.h" 30 | #include "py/misc.h" 31 | 32 | mp_uint_t utf8_ptr_to_index(const byte *s, const byte *ptr); 33 | bool utf8_check(const byte *p, size_t len); 34 | 35 | #endif // MICROPY_INCLUDED_PY_UNICODE_H 36 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/02-Micropython/py/warning.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "py/emit.h" 31 | #include "py/runtime.h" 32 | 33 | #if MICROPY_WARNINGS 34 | 35 | void mp_warning(const char *msg, ...) { 36 | va_list args; 37 | va_start(args, msg); 38 | mp_print_str(MICROPY_ERROR_PRINTER, "Warning: "); 39 | mp_vprintf(MICROPY_ERROR_PRINTER, msg, args); 40 | mp_print_str(MICROPY_ERROR_PRINTER, "\n"); 41 | va_end(args); 42 | } 43 | 44 | void mp_emitter_warning(pass_kind_t pass, const char *msg) { 45 | if (pass == MP_PASS_CODE_SIZE) { 46 | mp_warning(msg, NULL); 47 | } 48 | } 49 | 50 | #endif // MICROPY_WARNINGS 51 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/apio.ini: -------------------------------------------------------------------------------- 1 | [env] 2 | board = fpga101 3 | 4 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/font_rom.v: -------------------------------------------------------------------------------- 1 | module font_rom( 2 | input clk, 3 | input [11:0] addr, 4 | output [7:0] data_out 5 | ); 6 | 7 | reg [7:0] store[0:4095] /* verilator public_flat */; 8 | 9 | initial 10 | begin 11 | $readmemh("font_vga.mem", store); 12 | end 13 | 14 | always @(posedge clk) 15 | data_out <= store[addr]; 16 | endmodule 17 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/lcd_pll.v: -------------------------------------------------------------------------------- 1 | /** 2 | * PLL configuration 3 | * 4 | * This Verilog module was generated automatically 5 | * using the icepll tool from the IceStorm project. 6 | * Use at your own risk. 7 | * 8 | * Given input frequency: 12.000 MHz 9 | * Requested output frequency: 19.200 MHz 10 | * Achieved output frequency: 19.125 MHz 11 | */ 12 | 13 | module pll( 14 | input clock_in, 15 | output clock_out, 16 | output locked 17 | ); 18 | 19 | SB_PLL40_CORE #( 20 | .FEEDBACK_PATH("SIMPLE"), 21 | .DIVR(4'b0000), // DIVR = 0 22 | .DIVF(7'b0110010), // DIVF = 50 23 | .DIVQ(3'b101), // DIVQ = 5 24 | .FILTER_RANGE(3'b001) // FILTER_RANGE = 1 25 | ) uut ( 26 | .LOCK(locked), 27 | .RESETB(1'b1), 28 | .BYPASS(1'b0), 29 | .REFERENCECLK(clock_in), 30 | .PLLOUTGLOBAL(clock_out) 31 | ); 32 | 33 | endmodule 34 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/pinout.pcf: -------------------------------------------------------------------------------- 1 | # On board 2 | set_io -nowarn flash_csb 16 3 | set_io -nowarn flash_clk 15 4 | set_io -nowarn flash_io0 14 5 | set_io -nowarn flash_io1 17 6 | 7 | # 12 MHz clock 8 | set_io -nowarn CLK 35 # input 9 | 10 | # LCD connector 11 | set_io -nowarn LCD_CLK 18 # output 12 | set_io -nowarn LCD_DEN 19 # output 13 | set_io -nowarn LCD_VS 11 # output 14 | set_io -nowarn LCD_HS 9 # output 15 | set_io -nowarn LCD_RST 2 # output 16 | set_io -nowarn LCD_DAT[7] 6 # output 17 | set_io -nowarn LCD_DAT[6] 44 # output 18 | set_io -nowarn LCD_DAT[5] 4 # output 19 | set_io -nowarn LCD_DAT[4] 3 # output 20 | set_io -nowarn LCD_DAT[3] 48 # output 21 | set_io -nowarn LCD_DAT[2] 45 # output 22 | set_io -nowarn LCD_DAT[1] 47 # output 23 | set_io -nowarn LCD_DAT[0] 46 # output 24 | set_io -nowarn LCD_PWM 13 # output 25 | 26 | # Buttons 27 | set_io -nowarn B0 23 # input 28 | set_io -nowarn B1 25 # input 29 | set_io -nowarn B2 26 # input 30 | set_io -nowarn B3 27 # input 31 | set_io -nowarn B4 21 # input 32 | set_io -nowarn B5 12 # input 33 | 34 | # Audio jack 35 | set_io -nowarn AUDIO 37 # output 36 | 37 | # PMOD connector 38 | set_io -nowarn flash_io2 32 39 | set_io -nowarn flash_io3 31 40 | set_io -nowarn PMOD2 34 41 | set_io -nowarn PMOD3 43 42 | set_io -nowarn SER_RX 36 # PMOD4 43 | set_io -nowarn PMOD5 42 44 | set_io -nowarn SER_TX 38 # PMOD6 45 | set_io -nowarn PMOD7 28 46 | 47 | # RGB LED Driver 48 | set_io -nowarn LED_G 39 # output 49 | set_io -nowarn LED_B 40 # output 50 | set_io -nowarn LED_R 41 # output 51 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/prebuilt/Makefile: -------------------------------------------------------------------------------- 1 | ICEPROG = iceprog 2 | 3 | all: upload 4 | 5 | upload: 6 | $(ICEPROG) -o 1M firmware.bin 7 | 8 | .PHONY: upload clean 9 | 10 | -------------------------------------------------------------------------------- /tutorials/12-RiscV/prebuilt/firmware.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/tutorials/12-RiscV/prebuilt/firmware.bin -------------------------------------------------------------------------------- /tutorials/12-RiscV/prebuilt/picosoc.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmicko/fpga101-workshop/1f5d605bc158810148626df5261bf1dc87cf50a1/tutorials/12-RiscV/prebuilt/picosoc.bin -------------------------------------------------------------------------------- /tutorials/12-RiscV/text_rom.v: -------------------------------------------------------------------------------- 1 | module video_ram (input sys_clk, //system side clock 2 | input [9:0] sys_addr, //system side address (word address) 3 | input [7:0] sys_data, //system side write data 4 | input sys_wren, //bytewise wren 5 | 6 | input video_clk, //video side clock 7 | input [9:0] video_addr, 8 | output reg [7:0] video_data); 9 | 10 | parameter WORDS = 2 ** 10; 11 | 12 | reg [7:0] mem [0:WORDS-1]; 13 | 14 | always @(posedge sys_clk) begin 15 | if (sys_wren[0]) mem[sys_addr] <= sys_data; 16 | end 17 | 18 | 19 | always @(posedge video_clk) begin 20 | video_data <= mem[video_addr]; 21 | end 22 | 23 | endmodule -------------------------------------------------------------------------------- /tutorials/12-RiscV/up_spram.v: -------------------------------------------------------------------------------- 1 | module up_spram ( 2 | input clk, 3 | input [3:0] wen, 4 | input [21:0] addr, 5 | input [31:0] wdata, 6 | output [31:0] rdata 7 | ); 8 | 9 | wire cs_0, cs_1; 10 | wire [31:0] rdata_0, rdata_1; 11 | 12 | assign cs_0 = !addr[14]; 13 | assign cs_1 = addr[14]; 14 | assign rdata = addr[14] ? rdata_1 : rdata_0; 15 | 16 | 17 | SB_SPRAM256KA ram00 18 | ( 19 | .ADDRESS(addr[13:0]), 20 | .DATAIN(wdata[15:0]), 21 | .MASKWREN({wen[1], wen[1], wen[0], wen[0]}), 22 | .WREN(wen[1]|wen[0]), 23 | .CHIPSELECT(cs_0), 24 | .CLOCK(clk), 25 | .STANDBY(1'b0), 26 | .SLEEP(1'b0), 27 | .POWEROFF(1'b1), 28 | .DATAOUT(rdata_0[15:0]) 29 | ); 30 | 31 | SB_SPRAM256KA ram01 32 | ( 33 | .ADDRESS(addr[13:0]), 34 | .DATAIN(wdata[31:16]), 35 | .MASKWREN({wen[3], wen[3], wen[2], wen[2]}), 36 | .WREN(wen[3]|wen[2]), 37 | .CHIPSELECT(cs_0), 38 | .CLOCK(clk), 39 | .STANDBY(1'b0), 40 | .SLEEP(1'b0), 41 | .POWEROFF(1'b1), 42 | .DATAOUT(rdata_0[31:16]) 43 | ); 44 | 45 | 46 | SB_SPRAM256KA ram10 47 | ( 48 | .ADDRESS(addr[13:0]), 49 | .DATAIN(wdata[15:0]), 50 | .MASKWREN({wen[1], wen[1], wen[0], wen[0]}), 51 | .WREN(wen[1]|wen[0]), 52 | .CHIPSELECT(cs_1), 53 | .CLOCK(clk), 54 | .STANDBY(1'b0), 55 | .SLEEP(1'b0), 56 | .POWEROFF(1'b1), 57 | .DATAOUT(rdata_1[15:0]) 58 | ); 59 | 60 | SB_SPRAM256KA ram11 61 | ( 62 | .ADDRESS(addr[13:0]), 63 | .DATAIN(wdata[31:16]), 64 | .MASKWREN({wen[3], wen[3], wen[2], wen[2]}), 65 | .WREN(wen[3]|wen[2]), 66 | .CHIPSELECT(cs_1), 67 | .CLOCK(clk), 68 | .STANDBY(1'b0), 69 | .SLEEP(1'b0), 70 | .POWEROFF(1'b1), 71 | .DATAOUT(rdata_1[31:16]) 72 | ); 73 | 74 | endmodule -------------------------------------------------------------------------------- /tutorials/12-RiscV/video.v: -------------------------------------------------------------------------------- 1 | module video (input clk, //19.2MHz pixel clock in 2 | input resetn, 3 | output reg [7:0] lcd_dat, 4 | output reg lcd_hsync, 5 | output reg lcd_vsync, 6 | output reg lcd_den, 7 | output reg [9:0] h_pos, 8 | output reg [9:0] v_pos, 9 | input [23:0] rgb_data 10 | ); 11 | 12 | 13 | parameter h_visible = 10'd320; 14 | parameter h_front = 10'd20; 15 | parameter h_sync = 10'd30; 16 | parameter h_back = 10'd38; 17 | parameter h_total = h_visible + h_front + h_sync + h_back; 18 | 19 | parameter v_visible = 10'd240; 20 | parameter v_front = 10'd4; 21 | parameter v_sync = 10'd3; 22 | parameter v_back = 10'd15; 23 | parameter v_total = v_visible + v_front + v_sync + v_back; 24 | 25 | reg [1:0] channel = 0; 26 | 27 | wire h_active, v_active, visible; 28 | wire [23:0] rgb_data; 29 | 30 | always @(posedge clk) 31 | begin 32 | if (resetn == 0) begin 33 | h_pos <= 10'b0; 34 | v_pos <= 10'b0; 35 | 36 | end else begin 37 | //Pixel counters 38 | if (channel == 2) begin 39 | channel <= 0; 40 | if (h_pos == h_total - 1) begin 41 | h_pos <= 0; 42 | if (v_pos == v_total - 1) begin 43 | v_pos <= 0; 44 | end else begin 45 | v_pos <= v_pos + 1; 46 | end 47 | end else begin 48 | h_pos <= h_pos + 1; 49 | end 50 | end else begin 51 | channel <= channel + 1; 52 | end 53 | lcd_den <= !visible; 54 | lcd_hsync <= !((h_pos >= (h_visible + h_front)) && (h_pos < (h_visible + h_front + h_sync))); 55 | lcd_vsync <= !((v_pos >= (v_visible + v_front)) && (v_pos < (v_visible + v_front + v_sync))); 56 | lcd_dat <= channel == 0 ? rgb_data[23:16] : 57 | channel == 1 ? rgb_data[15:8] : 58 | rgb_data[7:0]; 59 | end 60 | end 61 | 62 | assign h_active = (h_pos < h_visible); 63 | assign v_active = (v_pos < v_visible); 64 | assign visible = h_active && v_active; 65 | 66 | endmodule 67 | --------------------------------------------------------------------------------