├── .inputrc ├── systemc ├── hello │ ├── .gitignore │ ├── hello.cc │ └── Makefile ├── lowpass │ ├── .gitignore │ ├── toplevel.cc │ ├── lowpass_tb.hpp │ ├── toplevel.hpp │ ├── lowpass.hpp │ ├── Makefile │ ├── main.cc │ ├── lowpass_tb.cc │ └── lowpass.cc └── fifo │ ├── .gitignore │ ├── Makefile │ ├── sender.hpp │ ├── receiver.hpp │ └── main.cc ├── tcp-ip ├── .gitignore └── Makefile ├── tempcontrol ├── .gitignore ├── Makefile ├── shutdown.sh └── README.md ├── greenpak4 ├── .gitignore ├── primitives │ ├── lfosc │ │ ├── lfosc.png │ │ ├── Makefile │ │ └── lfosc.v │ ├── rcosc │ │ ├── rcosc_2_mhz.png │ │ ├── schematic.pdf │ │ ├── Makefile │ │ ├── rcosc_25_khz.png │ │ └── rcosc.v │ ├── ringosc │ │ ├── ringosc.png │ │ ├── Makefile │ │ └── ringosc.v │ ├── delayline │ │ ├── delayline.png │ │ ├── schematic.pdf │ │ ├── Makefile │ │ └── delayline.v │ └── counter_inference │ │ ├── Makefile │ │ ├── counter.png │ │ ├── schematic.pdf │ │ └── counter.v ├── Makefile.common └── README.md ├── python ├── .gitignore ├── pi.py ├── feigenbaum.py ├── int.py ├── newton-fractal.py ├── einstein-solid.py └── scipy.py ├── sds011 ├── .gitignore ├── Makefile ├── upload ├── graph.gp ├── makegraphs ├── README.md └── sds011.c ├── socketcan ├── .gitignore ├── Makefile ├── sender.c └── receiver.c ├── .vimrc ├── openscad ├── .gitignore ├── cap_fence_post_v2.scad ├── cap_fence_post_v1.scad └── parallela.scad ├── ssh_config ├── vhdl ├── .gitignore ├── ex_1_10.vhd ├── ex_4_12.vhd ├── ex_2_10.vhd ├── ex_2_9.vhd ├── ex_3_15.vhd ├── ex_2_11.vhd ├── ex_3_11.vhd ├── ex_3_12.vhd ├── ex_4_10.vhd ├── ex_2_12.vhd ├── ex_1_10_tb.vhd ├── ex_3_14.vhd ├── ex_2_10_tb.vhd ├── ex_4_13.vhd ├── ex_4_9.vhd ├── ex_2_9_tb.vhd ├── ex_3_12_tb.vhd ├── ex_1_11.vhd ├── ex_2_13.vhd ├── ex_3_10.vhd ├── ex_2_13_tb.vhd ├── shiftreg.vhd ├── ex_4_10_tb.vhd ├── ex_4_12_tb.vhd ├── ex_3_13.vhd ├── ex_5_20.vhd ├── ex_3_10_tb.vhd ├── ex_4_11_tb.vhd ├── ex_2_11_tb.vhd ├── ex_4_11.vhd ├── ex_4_9_tb.vhd ├── ex_1_11_tb.vhd ├── ex_5_19_tb.vhd ├── ex_6_11.vhd ├── ex_6_10.vhd ├── ex_3_14_tb.vhd ├── events.vhd ├── ex_3_11_tb.vhd ├── ex_3_13_tb.vhd ├── ex_5_19.vhd ├── ex_5_20_tb.vhd ├── ex_6_18.vhd ├── shiftreg_tb.vhd ├── ex_6_15.vhd ├── ex_6_16.vhd ├── ex_4_13_tb.vhd ├── events_tb.vhd ├── ex_6_13.vhd ├── io_buffer.vhd └── ex_6_12.vhd ├── .Xresources ├── fortran ├── .gitignore ├── hello.f95 ├── README.md ├── Makefile ├── logistic-map.f95 └── mandelbrot.f95 ├── icestick ├── blink │ ├── .gitignore │ ├── blink.pcf │ ├── blink.v │ └── Makefile ├── ws2812 │ ├── ws2812.pcf │ ├── .gitignore │ ├── Makefile │ ├── ledstring_tb.v │ ├── ws2812_tx_tb.v │ ├── top.v │ ├── ledstring.v │ └── ws2812_tx.v ├── bincnt │ ├── bincnt.pcf │ ├── bincnt.vhd │ └── Makefile ├── 80-icestick.rules └── README.md ├── libnfc ├── blacklist-pn533.conf ├── .gitignore ├── 80-acr122u.rules ├── Makefile ├── README.md ├── LICENSE ├── list-devices.c ├── list-tags.c └── dump-girogo.c ├── matlab ├── ising │ ├── ising.png │ ├── energy_timestep.png │ ├── energy_histogram.png │ ├── energy.m │ └── ising.m ├── logistic_map_bifurcation.png ├── lorenz.m ├── logistic_map.m ├── logistic_map_bifurcation.m ├── heat.m ├── julia4d.m ├── mandelbrot.m ├── tree.m ├── poisson.m ├── euler_forward.m ├── pendulum.m ├── sierpinski.m ├── julia.m ├── wave.m ├── conway.m └── Quaternion.m ├── nbody ├── nbody │ ├── nbody.pdf │ ├── unroll.pdf │ ├── nbody_1024.txt │ ├── nbody_10240.txt │ ├── nbody_20480.txt │ ├── nbody_4096.txt │ ├── unroll.txt │ ├── bench.sh │ ├── unroll.gp │ ├── nbody.gp │ ├── Makefile │ └── kernel.cu ├── leapfrog │ ├── leapfrog.pdf │ ├── leapfrog_1024.txt │ ├── leapfrog_10240.txt │ ├── leapfrog_51200.txt │ ├── leapfrog_102400.txt │ ├── bench.sh │ ├── leapfrog.gp │ └── Makefile ├── nbody_cpu │ ├── nbody_cpu.pdf │ ├── bench.sh │ ├── nbody_cpu5.txt │ ├── nbody_cpu.gp │ ├── Makefile │ └── nbody_cpu.c └── opengl │ └── Makefile ├── rdm6300 ├── DS1Z_QuickPrint1.png ├── DS1Z_QuickPrint2.png ├── DS1Z_QuickPrint3.png ├── DS1Z_QuickPrint4.png └── README.md ├── .bash_profile ├── cowrie ├── README.md ├── cowrie-upload └── cowrie-stats.sh ├── hello.v ├── .gitignore ├── ngspice ├── voltage_divider.cir ├── voltage_divider_tf.cir ├── voltage_divider_dc.cir ├── rl_tran.cir ├── rc_ac.cir ├── diode_dc.cir ├── ideal_opamp.cir └── npn_dc.cir ├── count.v ├── .xinitrc ├── div.c ├── count_tb.v ├── mt7681 ├── build-andes-toolchain.sh └── README.md ├── PODCASTS.md ├── matsq.c └── coq ├── Induction.v └── Basics.v /.inputrc: -------------------------------------------------------------------------------- 1 | set editing-mode vi 2 | -------------------------------------------------------------------------------- /systemc/hello/.gitignore: -------------------------------------------------------------------------------- 1 | hello 2 | -------------------------------------------------------------------------------- /tcp-ip/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | main 3 | -------------------------------------------------------------------------------- /tempcontrol/.gitignore: -------------------------------------------------------------------------------- 1 | tempcontrol 2 | -------------------------------------------------------------------------------- /greenpak4/.gitignore: -------------------------------------------------------------------------------- 1 | *.txt 2 | *.json 3 | -------------------------------------------------------------------------------- /python/.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__ 2 | *.pyc 3 | -------------------------------------------------------------------------------- /sds011/.gitignore: -------------------------------------------------------------------------------- 1 | sds011 2 | logs/ 3 | html/ 4 | -------------------------------------------------------------------------------- /socketcan/.gitignore: -------------------------------------------------------------------------------- 1 | sender 2 | receiver 3 | -------------------------------------------------------------------------------- /systemc/lowpass/.gitignore: -------------------------------------------------------------------------------- 1 | main 2 | *.vcd 3 | -------------------------------------------------------------------------------- /systemc/fifo/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.vcd 3 | main 4 | -------------------------------------------------------------------------------- /.vimrc: -------------------------------------------------------------------------------- 1 | syntax enable 2 | filetype plugin indent on 3 | -------------------------------------------------------------------------------- /openscad/.gitignore: -------------------------------------------------------------------------------- 1 | # Generated STL files 2 | *.stl 3 | -------------------------------------------------------------------------------- /ssh_config: -------------------------------------------------------------------------------- 1 | KeepAlive yes 2 | ServerAliveInterval 60 3 | -------------------------------------------------------------------------------- /vhdl/.gitignore: -------------------------------------------------------------------------------- 1 | work-obj08.cf 2 | work-obj93.cf 3 | *.ghw 4 | -------------------------------------------------------------------------------- /.Xresources: -------------------------------------------------------------------------------- 1 | XTerm*faceName: Inconsolata 2 | XTerm*faceSize: 10 3 | -------------------------------------------------------------------------------- /fortran/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | hello 3 | logistic-map 4 | mandelbrot 5 | -------------------------------------------------------------------------------- /fortran/hello.f95: -------------------------------------------------------------------------------- 1 | program hello 2 | print *, 'Hello world!' 3 | end -------------------------------------------------------------------------------- /icestick/blink/.gitignore: -------------------------------------------------------------------------------- 1 | *.blif 2 | *.asc 3 | *.bin 4 | *.vcd 5 | -------------------------------------------------------------------------------- /icestick/ws2812/ws2812.pcf: -------------------------------------------------------------------------------- 1 | set_io clk 21 2 | set_io dout 112 3 | -------------------------------------------------------------------------------- /libnfc/blacklist-pn533.conf: -------------------------------------------------------------------------------- 1 | blacklist pn533 2 | blacklist nfc 3 | -------------------------------------------------------------------------------- /icestick/ws2812/.gitignore: -------------------------------------------------------------------------------- 1 | *.blif 2 | *.asc 3 | *.bin 4 | *.vcd 5 | *.vvp 6 | -------------------------------------------------------------------------------- /libnfc/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | tags 3 | list-devices 4 | list-tags 5 | dump-girogo 6 | -------------------------------------------------------------------------------- /matlab/ising/ising.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/matlab/ising/ising.png -------------------------------------------------------------------------------- /nbody/nbody/nbody.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/nbody/nbody/nbody.pdf -------------------------------------------------------------------------------- /nbody/nbody/unroll.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/nbody/nbody/unroll.pdf -------------------------------------------------------------------------------- /nbody/leapfrog/leapfrog.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/nbody/leapfrog/leapfrog.pdf -------------------------------------------------------------------------------- /libnfc/80-acr122u.rules: -------------------------------------------------------------------------------- 1 | ATTRS{idVendor}=="072f", ATTRS{idProduct}=="2200", MODE="660", GROUP="plugdev" 2 | -------------------------------------------------------------------------------- /nbody/nbody_cpu/nbody_cpu.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/nbody/nbody_cpu/nbody_cpu.pdf -------------------------------------------------------------------------------- /rdm6300/DS1Z_QuickPrint1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/rdm6300/DS1Z_QuickPrint1.png -------------------------------------------------------------------------------- /rdm6300/DS1Z_QuickPrint2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/rdm6300/DS1Z_QuickPrint2.png -------------------------------------------------------------------------------- /rdm6300/DS1Z_QuickPrint3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/rdm6300/DS1Z_QuickPrint3.png -------------------------------------------------------------------------------- /rdm6300/DS1Z_QuickPrint4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/rdm6300/DS1Z_QuickPrint4.png -------------------------------------------------------------------------------- /matlab/ising/energy_timestep.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/matlab/ising/energy_timestep.png -------------------------------------------------------------------------------- /matlab/ising/energy_histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/matlab/ising/energy_histogram.png -------------------------------------------------------------------------------- /.bash_profile: -------------------------------------------------------------------------------- 1 | export EDITOR=vim 2 | export HISTFILESIZE= 3 | export HISTSIZE= 4 | shopt -s histappend 5 | fortune -o 6 | -------------------------------------------------------------------------------- /greenpak4/primitives/lfosc/lfosc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/lfosc/lfosc.png -------------------------------------------------------------------------------- /matlab/logistic_map_bifurcation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/matlab/logistic_map_bifurcation.png -------------------------------------------------------------------------------- /nbody/nbody/nbody_1024.txt: -------------------------------------------------------------------------------- 1 | 32 4428719104.000000 2 | 64 4490444288.000000 3 | 128 4595692032.000000 4 | 256 3264965632.000000 5 | -------------------------------------------------------------------------------- /greenpak4/primitives/rcosc/rcosc_2_mhz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/rcosc/rcosc_2_mhz.png -------------------------------------------------------------------------------- /greenpak4/primitives/rcosc/schematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/rcosc/schematic.pdf -------------------------------------------------------------------------------- /greenpak4/primitives/ringosc/ringosc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/ringosc/ringosc.png -------------------------------------------------------------------------------- /nbody/nbody/nbody_10240.txt: -------------------------------------------------------------------------------- 1 | 32 9114998784.000000 2 | 64 10528072704.000000 3 | 128 11447904256.000000 4 | 256 11314562048.000000 5 | -------------------------------------------------------------------------------- /nbody/nbody/nbody_20480.txt: -------------------------------------------------------------------------------- 1 | 32 9145307136.000000 2 | 64 11509904384.000000 3 | 128 11495402496.000000 4 | 256 11353984000.000000 5 | -------------------------------------------------------------------------------- /nbody/nbody/nbody_4096.txt: -------------------------------------------------------------------------------- 1 | 32 7826458112.000000 2 | 64 10666080256.000000 3 | 128 7922688512.000000 4 | 256 6753381888.000000 5 | -------------------------------------------------------------------------------- /greenpak4/primitives/delayline/delayline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/delayline/delayline.png -------------------------------------------------------------------------------- /greenpak4/primitives/delayline/schematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/delayline/schematic.pdf -------------------------------------------------------------------------------- /greenpak4/primitives/lfosc/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = lfosc 2 | SOURCE = lfosc.v 3 | PART = SLG46620V 4 | 5 | include ../../Makefile.common 6 | -------------------------------------------------------------------------------- /greenpak4/primitives/rcosc/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = rcosc 2 | SOURCE = rcosc.v 3 | PART = SLG46620V 4 | 5 | include ../../Makefile.common 6 | -------------------------------------------------------------------------------- /greenpak4/primitives/rcosc/rcosc_25_khz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/rcosc/rcosc_25_khz.png -------------------------------------------------------------------------------- /icestick/blink/blink.pcf: -------------------------------------------------------------------------------- 1 | set_io led1 99 2 | set_io led2 98 3 | set_io led3 97 4 | set_io led4 96 5 | set_io led5 95 6 | set_io clk 21 7 | -------------------------------------------------------------------------------- /greenpak4/primitives/ringosc/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = ringosc 2 | SOURCE = ringosc.v 3 | PART = SLG46620V 4 | 5 | include ../../Makefile.common 6 | -------------------------------------------------------------------------------- /icestick/bincnt/bincnt.pcf: -------------------------------------------------------------------------------- 1 | set_io led1 99 2 | set_io led2 98 3 | set_io led3 97 4 | set_io led4 96 5 | set_io led5 95 6 | set_io clk 21 7 | -------------------------------------------------------------------------------- /greenpak4/primitives/delayline/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = delayline 2 | SOURCE = delayline.v 3 | PART = SLG46620V 4 | 5 | include ../../Makefile.common 6 | -------------------------------------------------------------------------------- /icestick/80-icestick.rules: -------------------------------------------------------------------------------- 1 | ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6010", MODE="660", GROUP="plugdev", RUN+="/sbin/rmmod ftdi_sio usbserial" 2 | -------------------------------------------------------------------------------- /greenpak4/primitives/counter_inference/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = counter 2 | SOURCE = counter.v 3 | PART = SLG46620V 4 | 5 | include ../../Makefile.common 6 | -------------------------------------------------------------------------------- /greenpak4/primitives/counter_inference/counter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/counter_inference/counter.png -------------------------------------------------------------------------------- /greenpak4/primitives/counter_inference/schematic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/m-thu/sandbox/HEAD/greenpak4/primitives/counter_inference/schematic.pdf -------------------------------------------------------------------------------- /fortran/README.md: -------------------------------------------------------------------------------- 1 | PGPLOT library: 2 | 3 | Arch Linux: 4 | ```sh 5 | export PGPLOT_DIR=/usr/lib 6 | ``` 7 | -------------------------------------------------------------------------------- /nbody/leapfrog/leapfrog_1024.txt: -------------------------------------------------------------------------------- 1 | 8 90.765823 2 | 16 76.579742 3 | 32 72.070526 4 | 64 75.232124 5 | 128 78.518143 6 | 256 77.031776 7 | 512 111.051842 8 | -------------------------------------------------------------------------------- /sds011/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -std=c99 -Wall -Wextra -pedantic -pipe -Os 2 | 3 | sds011 : sds011.c 4 | 5 | clean : 6 | rm -f sds011 7 | 8 | .PHONY : clean 9 | -------------------------------------------------------------------------------- /nbody/leapfrog/leapfrog_10240.txt: -------------------------------------------------------------------------------- 1 | 8 576.444763 2 | 16 381.084106 3 | 32 381.491974 4 | 64 403.857056 5 | 128 410.779388 6 | 256 413.164154 7 | 512 433.330597 8 | -------------------------------------------------------------------------------- /nbody/leapfrog/leapfrog_51200.txt: -------------------------------------------------------------------------------- 1 | 8 2569.953125 2 | 16 1646.840698 3 | 32 1725.392456 4 | 64 1825.691406 5 | 128 1866.385742 6 | 256 1935.240967 7 | 512 2081.847656 8 | -------------------------------------------------------------------------------- /nbody/leapfrog/leapfrog_102400.txt: -------------------------------------------------------------------------------- 1 | 8 5220.274414 2 | 16 3259.348633 3 | 32 3404.046143 4 | 64 3577.566406 5 | 128 3674.522949 6 | 256 3831.328857 7 | 512 4181.731445 8 | -------------------------------------------------------------------------------- /nbody/nbody/unroll.txt: -------------------------------------------------------------------------------- 1 | 1 9976368128.000000 2 | 2 10606317568.000000 3 | 4 11079996416.000000 4 | 8 11333538816.000000 5 | 16 11464454144.000000 6 | 32 11503733760.000000 7 | -------------------------------------------------------------------------------- /nbody/nbody_cpu/bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | rm -f nbody_cpu5.txt 4 | for ((i = 10000; i <= 45000; i+=5000)) 5 | do 6 | ./nbody_cpu_opt $i >>nbody_cpu5.txt 7 | done 8 | -------------------------------------------------------------------------------- /tempcontrol/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -std=c99 -Wall -Wextra -pedantic -pipe -Os -Wno-unused-result 2 | 3 | tempcontrol : tempcontrol.c 4 | 5 | clean : 6 | rm -f tempcontrol 7 | 8 | .PHONY : clean 9 | -------------------------------------------------------------------------------- /matlab/ising/energy.m: -------------------------------------------------------------------------------- 1 | function [E] = energy(S, B) 2 | % Spin-spin interaction 3 | E = - sum(S(1:end-1) .* S(2:end)); 4 | 5 | % External magnetic field 6 | E = E - B * sum(S); 7 | end -------------------------------------------------------------------------------- /greenpak4/primitives/ringosc/ringosc.v: -------------------------------------------------------------------------------- 1 | module top ( 2 | (* LOC="P3" *) output y 3 | ); 4 | 5 | GP_RINGOSC #( 6 | .AUTO_PWRDN(0) 7 | ) ringosc ( 8 | .CLKOUT_FABRIC(y) 9 | ); 10 | 11 | endmodule 12 | -------------------------------------------------------------------------------- /tempcontrol/shutdown.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # GPIO 26: Shutdown switch (input, active low) 4 | GPIO_SHUTDOWN=/sys/class/gpio/gpio26/value 5 | 6 | while [ true ]; do 7 | [ `cat ${GPIO_SHUTDOWN}` -eq 1 ] || /sbin/poweroff 8 | sleep 1 9 | done -------------------------------------------------------------------------------- /nbody/nbody_cpu/nbody_cpu5.txt: -------------------------------------------------------------------------------- 1 | 10000 106928999.144568 2 | 15000 106888361.045131 3 | 20000 106817635.591636 4 | 25000 106819347.120150 5 | 30000 106813515.470157 6 | 35000 106859042.019592 7 | 40000 106834753.345263 8 | 45000 106851136.579497 9 | -------------------------------------------------------------------------------- /nbody/nbody/bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for N in 1024 4096 10240 20480 4 | do 5 | rm -f nbody_$N.txt 6 | for P in 32 64 128 256 7 | do 8 | echo -ne "$P\t" >>nbody_$N.txt 9 | ./nbody -N$N -T16 -S1000 -P$P >>nbody_$N.txt 10 | done 11 | done 12 | -------------------------------------------------------------------------------- /cowrie/README.md: -------------------------------------------------------------------------------- 1 | Cowrie SSH Honeypot (Raspberry Pi) 2 | ================================== 3 | 4 | `gawk, dig`: 5 | ```sh 6 | sudo apt-get install gawk dnsutils 7 | ``` 8 | `crontab`: 9 | ```sh 10 | 0 0 * * * /home/pi/cowrie-stats.sh && /home/pi/cowrie-upload 11 | ``` 12 | -------------------------------------------------------------------------------- /hello.v: -------------------------------------------------------------------------------- 1 | // hello.v 2 | // 3 | // Compile with: iverilog -o hello.vvp hello.v 4 | // Run with: vvp hello.vvp 5 | // Lint: verilator --lint-only hello.v 6 | 7 | module hello(); 8 | initial begin 9 | $display("Hello world!"); 10 | $finish; 11 | end 12 | endmodule 13 | -------------------------------------------------------------------------------- /vhdl/ex_1_10.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_1_10.vhd 2 | 3 | entity mux is 4 | port ( a, b, sel : in bit; 5 | z : out bit ); 6 | end mux; 7 | 8 | architecture behav of mux is 9 | begin 10 | with sel select z <= 11 | a when '0', 12 | b when '1'; 13 | end behav; 14 | -------------------------------------------------------------------------------- /nbody/leapfrog/bench.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | for N in 1024 10240 51200 102400 4 | do 5 | rm -f leapfrog_$N.txt 6 | for TPB in 8 16 32 64 128 256 512 7 | do 8 | echo -ne "$TPB\t" >>leapfrog_$N.txt 9 | ./leapfrog -N$N -T$TPB -S10000 >>leapfrog_$N.txt 10 | done 11 | done 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # LaTeX 2 | *.aux 3 | *.log 4 | *.toc 5 | *.out 6 | notes.pdf 7 | matlab.pdf 8 | signals-systems.pdf 9 | dsp.pdf 10 | electronics-cooling.pdf 11 | 12 | # Verilog 13 | *.vvp 14 | *.vcd 15 | 16 | # vim 17 | *.swp 18 | 19 | # C 20 | *.o 21 | *.ko 22 | *.lib 23 | *.a 24 | *.so 25 | *.so.* 26 | -------------------------------------------------------------------------------- /greenpak4/primitives/lfosc/lfosc.v: -------------------------------------------------------------------------------- 1 | module top ( 2 | (* LOC = "P3" *) output y 3 | ); 4 | 5 | // LF oscillator (~ 1.73 kHz) 6 | GP_LFOSC #( 7 | .PWRDN_EN(0), 8 | .AUTO_PWRDN(0), 9 | .OUT_DIV(1) // 1,2,4,16 10 | ) lfosc ( 11 | .PWRDN(1'b0), 12 | .CLKOUT(y) 13 | ); 14 | 15 | endmodule 16 | -------------------------------------------------------------------------------- /icestick/blink/blink.v: -------------------------------------------------------------------------------- 1 | module top ( 2 | input wire clk, 3 | output wire led1,led2,led3,led4,led5 4 | ); 5 | 6 | reg [23:0] counter = 0; 7 | 8 | assign led1 = counter[23]; 9 | assign {led2,led3,led4,led5} = 4'b0000; 10 | 11 | always @(posedge clk) 12 | counter <= counter + 24'b1; 13 | 14 | endmodule 15 | -------------------------------------------------------------------------------- /ngspice/voltage_divider.cir: -------------------------------------------------------------------------------- 1 | Voltage divider DC operating point 2 | * Ngspice batch mode: 3 | * ngspice -b voltage_divider.cir 4 | 5 | * DC operating point 6 | .op 7 | 8 | * DC voltage source, V0 = 10 V, Node 0: ground 9 | V0 1 0 DC 10 10 | * Resistive voltage divider 11 | R1 1 2 10k 12 | R2 2 0 10k 13 | 14 | .end 15 | -------------------------------------------------------------------------------- /vhdl/ex_4_12.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_12.vhd 2 | 3 | library ieee; 4 | use ieee.std_logic_1164.all; 5 | 6 | entity gate is 7 | port ( a, b : in std_ulogic_vector; 8 | y : out std_ulogic ); 9 | end entity gate; 10 | 11 | architecture behav of gate is 12 | begin 13 | y <= not (or (a and b)); 14 | end architecture behav; 15 | -------------------------------------------------------------------------------- /cowrie/cowrie-upload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # HTML file 4 | HTML=/home/pi/cowrie-stats.html 5 | # Remote directory 6 | REMOTEDIR=html/ 7 | # Additional commands to execute on target 8 | CMD="chmod o+r html/cowrie-stats.html" 9 | 10 | # account 11 | USER= 12 | HOST= 13 | 14 | scp $HTML ${USER}@${HOST}:$REMOTEDIR 15 | ssh ${USER}@${HOST} "$CMD" 16 | -------------------------------------------------------------------------------- /count.v: -------------------------------------------------------------------------------- 1 | // count.v 2 | // 3 | // Lint: verilator --lint-only count.v 4 | 5 | module count #( 6 | parameter COUNTER_WIDTH = 4 7 | ) ( 8 | input rst, 9 | input clk, 10 | output reg[COUNTER_WIDTH-1:0] cnt 11 | ); 12 | 13 | always @(posedge clk) begin 14 | if (rst) 15 | cnt <= 0; 16 | else 17 | cnt <= cnt + 1; 18 | end 19 | endmodule 20 | -------------------------------------------------------------------------------- /greenpak4/primitives/rcosc/rcosc.v: -------------------------------------------------------------------------------- 1 | module top ( 2 | (* LOC = "P3" *) output x 3 | ); 4 | 5 | GP_RCOSC #( 6 | .AUTO_PWRDN(0), 7 | .OSC_FREQ("2M"), // 2M, 25k 8 | .HARDIP_DIV(1), // 1,2,4,8 9 | .FABRIC_DIV(1), // 1,2,3,4,8,12,24,64 10 | .PWRDN_EN(0) 11 | ) rcosc ( 12 | .PWRDN(1'b0), 13 | .CLKOUT_FABRIC(x) 14 | ); 15 | 16 | endmodule 17 | -------------------------------------------------------------------------------- /sds011/upload: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # directory with files generated by 'makegraphs' 4 | OUTDIR=/home/pi/sds011/html 5 | # directory name on target 6 | REMOTEDIR=html/sds011/ 7 | # additional commands to execute on target 8 | CMD="chmod g+r html/sds011/*" 9 | 10 | # account 11 | USER= 12 | HOST= 13 | 14 | scp $OUTDIR/* ${USER}@${HOST}:$REMOTEDIR 15 | ssh ${USER}@${HOST} "$CMD" 16 | -------------------------------------------------------------------------------- /fortran/Makefile: -------------------------------------------------------------------------------- 1 | all : hello logistic-map mandelbrot 2 | 3 | hello : hello.o 4 | 5 | logistic-map : logistic-map.o -lpgplot 6 | 7 | mandelbrot : mandelbrot.o -lpgplot 8 | 9 | FC = gfortran 10 | FFLAGS = -Os 11 | CC = gfortran 12 | 13 | %.o : %.f95 14 | $(FC) $(FFLAGS) -c $< -o $@ 15 | 16 | clean: 17 | rm -f *.o 18 | rm -f hello 19 | 20 | .PHONY : clean all -------------------------------------------------------------------------------- /systemc/hello/hello.cc: -------------------------------------------------------------------------------- 1 | #include "systemc.h" 2 | 3 | SC_MODULE (hello_world) 4 | { 5 | SC_CTOR (hello_world) 6 | { 7 | } 8 | 9 | void hello() 10 | { 11 | cout << "Hello world!" << endl; 12 | } 13 | }; 14 | 15 | int sc_main(int argc, char *argv[]) 16 | { 17 | (void)argc; (void)argv; 18 | 19 | hello_world hello("HELLO"); 20 | hello.hello(); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /vhdl/ex_2_10.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_10.vhd 2 | 3 | entity alu is 4 | port ( a, b : in integer; 5 | sel : in bit; 6 | y : out integer ); 7 | end alu; 8 | 9 | architecture behav of alu is 10 | begin 11 | process (a, b, sel) is 12 | begin 13 | if sel = '0' then 14 | y <= a + b; 15 | else 16 | y <= a - b; 17 | end if; 18 | end process; 19 | end behav; 20 | -------------------------------------------------------------------------------- /socketcan/Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -Wall -Wextra -pedantic -std=c99 -Os -pipe 2 | 3 | all : sender receiver 4 | 5 | vcan : 6 | @(lsmod | grep vcan >/dev/null) || sudo modprobe vcan 7 | @(ip addr | grep vcan >/dev/null) \ 8 | || (sudo ip link add dev vcan0 type vcan; \ 9 | sudo ip link set up vcan0) 10 | 11 | clean : 12 | rm -f *.o 13 | rm -f sender receiver 14 | 15 | .PHONY : all clean vcan 16 | -------------------------------------------------------------------------------- /vhdl/ex_2_9.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_9.vhd 2 | 3 | entity counter is 4 | port ( clk : in bit; 5 | q : out integer ); 6 | end counter; 7 | 8 | architecture behav of counter is 9 | begin 10 | process (clk) is 11 | variable count : integer := 0; 12 | begin 13 | if rising_edge(clk) then 14 | count := count + 1; 15 | q <= count; 16 | end if; 17 | end process; 18 | end behav; 19 | -------------------------------------------------------------------------------- /nbody/nbody/unroll.gp: -------------------------------------------------------------------------------- 1 | set term post eps enhanced color solid linewidth 2.0 font "Helvetica,18" 2 | set output "unroll.eps" 3 | 4 | set samples 1000 5 | set encoding iso_8859_1 6 | 7 | set xlabel "unroll count" 8 | set ylabel "#interactions / s" 9 | 10 | set key right center 11 | 12 | set pointsize 3 13 | plot "unroll.txt" u 1:2 w points title "N = 20480, p = 64" 14 | 15 | set output 16 | -------------------------------------------------------------------------------- /systemc/hello/Makefile: -------------------------------------------------------------------------------- 1 | SYSTEMC_HOME ?= /usr/local/systemc-2.3.1 2 | 3 | CC := g++ 4 | CXXFLAGS := -std=c++98 -pedantic -Wall -Wextra -Wno-long-long \ 5 | -I. -I$(SYSTEMC_HOME)/include 6 | LDFLAGS := -L. -L$(SYSTEMC_HOME)/lib-linux64 \ 7 | -Wl,-rpath=$(SYSTEMC_HOME)/lib-linux64 8 | LDLIBS := -lsystemc -lm 9 | 10 | hello : hello.o 11 | 12 | clean : 13 | rm -f hello 14 | rm -f *.o 15 | 16 | .PHONY : clean 17 | -------------------------------------------------------------------------------- /vhdl/ex_3_15.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_15.vhd 2 | -- ghdl -r --std=08 ex_3_15 3 | 4 | entity ex_3_15 is 5 | end entity ex_3_15; 6 | 7 | architecture behav of ex_3_15 is 8 | begin 9 | assert false report "note" severity note; 10 | assert false report "warning" severity warning; 11 | assert false report "error" severity error; 12 | assert false report "failure" severity failure; 13 | end architecture behav; 14 | -------------------------------------------------------------------------------- /vhdl/ex_2_11.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_11.vhd 2 | 3 | entity integrator is 4 | port ( clk : in bit; 5 | data : in real; 6 | sum : out real ); 7 | end integrator; 8 | 9 | architecture behav of integrator is 10 | signal s : real := 0.0; 11 | begin 12 | process (clk) is 13 | begin 14 | if rising_edge(clk) then 15 | s <= s + data; 16 | end if; 17 | end process; 18 | 19 | sum <= s; 20 | end behav; 21 | -------------------------------------------------------------------------------- /ngspice/voltage_divider_tf.cir: -------------------------------------------------------------------------------- 1 | Voltage divider transfer function 2 | * Ngspice batch mode: 3 | * ngspice -b voltage_divider_tf.cir 4 | 5 | * DC transfer characteristic 6 | * H = V(2,0) / V0 = 0.5 7 | * Output impedance: R1//R2 = 5 kOhm 8 | * Input impedance: R1+R2 = 20 kOhm 9 | .tf V(2,0) V0 10 | 11 | * DC voltage source, V0 = 10 V 12 | V0 1 0 DC 10 13 | * Resistive voltage divider 14 | R1 1 2 10k 15 | R2 2 0 10k 16 | 17 | .end 18 | -------------------------------------------------------------------------------- /vhdl/ex_3_11.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_11.vhd 2 | 3 | entity float_alu is 4 | port ( x, y : in real; 5 | f0, f1 : in bit; 6 | z : out real ); 7 | end entity float_alu; 8 | 9 | architecture behav of float_alu is 10 | begin 11 | z <= x + y when f1 = '0' and f0 = '0' 12 | else x - y when f1 = '0' and f0 = '1' 13 | else x * y when f1 = '1' and f0 = '0' 14 | else x / y; 15 | end architecture behav; 16 | -------------------------------------------------------------------------------- /systemc/lowpass/toplevel.cc: -------------------------------------------------------------------------------- 1 | #include "toplevel.hpp" 2 | 3 | Toplevel::Toplevel(sc_module_name name) : 4 | tb("tb"), 5 | lp("lp"), 6 | clock("clock", 10, SC_NS) 7 | { 8 | (void)name; 9 | 10 | tb.clk (clock ); 11 | tb.x (signal_x ); 12 | tb.y (signal_y ); 13 | tb.reset(signal_reset); 14 | 15 | lp.clk (clock ); 16 | lp.x (signal_x ); 17 | lp.y (signal_y ); 18 | lp.reset(signal_reset); 19 | } 20 | -------------------------------------------------------------------------------- /sds011/graph.gp: -------------------------------------------------------------------------------- 1 | set term png size 1280,480 font "Arial,15" 2 | set output 3 | 4 | set samples 1000 5 | set encoding iso_8859_1 6 | 7 | set timefmt "%H:%M:%S" 8 | set xdata time 9 | set xrange [ "00:00:00":"23:59:59" ] 10 | set format x "%H:%M" 11 | #unset mxtics 12 | 13 | set xlabel "Hour" 14 | set ylabel "ug / m^3" 15 | 16 | plot filename u 1:2 title "PM 2.5" w points lt rgb "green",\ 17 | filename u 1:3 title "PM 10" w points lt rgb "red" 18 | -------------------------------------------------------------------------------- /ngspice/voltage_divider_dc.cir: -------------------------------------------------------------------------------- 1 | Voltage divider DC analysis 2 | * Ngspice batch mode: 3 | * ngspice -b voltage_divider_dc.cir 4 | 5 | * Sweep input voltage from 0 V to 10 V in 1 V steps 6 | .dc V0 0 10 1 7 | * Print output voltage 8 | .print DC V(Vout,0) 9 | * Plot output voltage 10 | .plot DC V(Vout,0) 11 | 12 | * DC voltage source, V0 = 10 V, Node 0: ground 13 | V0 V0 0 DC 10 14 | * Resistive voltage divider 15 | R1 V0 Vout 10k 16 | R2 Vout 0 10k 17 | 18 | .end 19 | -------------------------------------------------------------------------------- /systemc/lowpass/lowpass_tb.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOWPASS_TB_HPP 2 | #define LOWPASS_TB_HPP 3 | 4 | #include 5 | using namespace sc_core; 6 | 7 | struct Testbench : public sc_module 8 | { 9 | // Ports 10 | sc_in clk; 11 | sc_in y; 12 | sc_out reset; 13 | sc_out x; 14 | 15 | // Processes 16 | void genSignals(); 17 | 18 | // Constructor 19 | SC_HAS_PROCESS(Testbench); 20 | Testbench(sc_module_name); 21 | }; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /vhdl/ex_3_12.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_12.vhd 2 | 3 | entity counter is 4 | port ( clk : in bit; 5 | count : out natural ); 6 | end entity counter; 7 | 8 | architecture behav of counter is 9 | begin 10 | process is 11 | variable c : natural := 15; 12 | begin 13 | count <= c; 14 | loop 15 | wait until clk = '1'; 16 | c := c - 1 when c /= 0 17 | else 15; 18 | count <= c; 19 | end loop; 20 | end process; 21 | end architecture behav; 22 | -------------------------------------------------------------------------------- /systemc/fifo/Makefile: -------------------------------------------------------------------------------- 1 | SYSTEMC_HOME ?= /usr/local/systemc-2.3.1 2 | 3 | CC := g++ 4 | CXXFLAGS := -std=c++98 -pedantic -Wall -Wextra -Wno-long-long \ 5 | -I. -I$(SYSTEMC_HOME)/include -Os -pipe 6 | LDFLAGS := -L. -L$(SYSTEMC_HOME)/lib-linux64 \ 7 | -Wl,-rpath=$(SYSTEMC_HOME)/lib-linux64 8 | LDLIBS := -lsystemc -lm 9 | 10 | main : main.o 11 | 12 | main.o : sender.hpp receiver.hpp 13 | 14 | clean : 15 | rm -f main 16 | rm -f *.o 17 | rm -f *.vcd 18 | 19 | .PHONY : clean 20 | -------------------------------------------------------------------------------- /.xinitrc: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # ~/.xinitrc 4 | # 5 | # Executed by startx (run your window manager from here) 6 | 7 | if [ -d /etc/X11/xinit/xinitrc.d ]; then 8 | for f in /etc/X11/xinit/xinitrc.d/*; do 9 | [ -x "$f" ] && . "$f" 10 | done 11 | unset f 12 | fi 13 | 14 | # exec gnome-session 15 | # exec startkde 16 | # exec startxfce4 17 | # ...or the Window Manager of your choice 18 | xrdb -merge ~/.Xresources 19 | setxkbmap -option caps:escape 20 | #exec awesome 21 | exec wmaker 22 | -------------------------------------------------------------------------------- /nbody/nbody_cpu/nbody_cpu.gp: -------------------------------------------------------------------------------- 1 | set term post eps enhanced color solid linewidth 2.0 font "Helvetica,18" 2 | set output "nbody_cpu.eps" 3 | 4 | set samples 1000 5 | set encoding iso_8859_1 6 | 7 | set xlabel "N" 8 | set ylabel "#interactions / s" 9 | 10 | #set logscale y 11 | 12 | f(x) = a 13 | 14 | fit f(x) "nbody_cpu5.txt" u 1:2 via a 15 | 16 | set pointsize 3 17 | plot [5000:50000] [1.067e8:1.07e8] "nbody_cpu5.txt" u 1:2 notitle w points, f(x) notitle 18 | 19 | set output 20 | -------------------------------------------------------------------------------- /vhdl/ex_4_10.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_10.vhd 2 | 3 | entity priority is 4 | port ( din : in bit_vector(15 downto 0); 5 | index : out natural; 6 | one : out bit ); 7 | end entity priority; 8 | 9 | architecture behav of priority is 10 | begin 11 | process (din) is 12 | begin 13 | for i in din'range loop 14 | if din(i) = '1' then 15 | index <= i; 16 | exit; 17 | end if; 18 | end loop; 19 | end process; 20 | 21 | one <= or din; 22 | end architecture behav; 23 | -------------------------------------------------------------------------------- /systemc/lowpass/toplevel.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TOPLEVEL_HPP 2 | #define TOPLEVEL_HPP 3 | 4 | #include "lowpass_tb.hpp" 5 | #include "lowpass.hpp" 6 | #include 7 | 8 | using namespace sc_core; 9 | 10 | struct Toplevel : public sc_module 11 | { 12 | // Modules 13 | Testbench tb; 14 | Lowpass lp; 15 | 16 | // Constructor 17 | Toplevel(sc_module_name); 18 | 19 | private: 20 | sc_clock clock; 21 | sc_signal signal_x, signal_y; 22 | sc_signal signal_reset; 23 | }; 24 | 25 | #endif 26 | -------------------------------------------------------------------------------- /vhdl/ex_2_12.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_12.vhd 2 | -- ghdl -r --std=08 ex_2_12 --wave=ex_2_12.ghw 3 | -- gtkwave ex_2_12.ghw 4 | 5 | entity ex_2_12 is 6 | end ex_2_12; 7 | 8 | architecture behav of ex_2_12 is 9 | signal clk : bit; 10 | begin 11 | clock_gen : process is 12 | begin 13 | clk <= '1'; 14 | --wait for 10 ns; 15 | --wait for 1 ns; 16 | wait for 1 ps; 17 | clk <= '0'; 18 | --wait for 10 ns; 19 | --wait for 1 ns; 20 | wait for 1 ps; 21 | end process clock_gen; 22 | end behav; 23 | -------------------------------------------------------------------------------- /systemc/lowpass/lowpass.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOWPASS_HPP 2 | #define LOWPASS_HPP 3 | 4 | #include 5 | using namespace sc_core; 6 | 7 | struct Lowpass : public sc_module 8 | { 9 | // Ports 10 | sc_in clk, reset; 11 | sc_in x; 12 | sc_out y; 13 | 14 | // Processes 15 | void state(); 16 | void transition(); 17 | 18 | // Constructor 19 | SC_HAS_PROCESS(Lowpass); 20 | Lowpass(sc_module_name); 21 | 22 | private: 23 | sc_signal x_1, x_2, x_3, average; 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /greenpak4/primitives/delayline/delayline.v: -------------------------------------------------------------------------------- 1 | module top ( 2 | (* LOC = "P3" *) output wire x, 3 | (* LOC = "P4" *) output wire y 4 | ); 5 | 6 | // LF Oscillator (~ 1.73 kHz) 7 | GP_LFOSC #( 8 | .PWRDN_EN(0), 9 | .AUTO_PWRDN(0), 10 | .OUT_DIV(16) // 1,2,4,16 11 | ) lfosc ( 12 | .PWRDN(1'b0), 13 | .CLKOUT(x) 14 | ); 15 | 16 | // Delay line (~ 4*165 ns = 660 ns) 17 | GP_DELAY #( 18 | .DELAY_STEPS(4), 19 | .GLITCH_FILTER(0) 20 | ) delayline ( 21 | .IN(x), 22 | .OUT(y) 23 | ); 24 | 25 | endmodule 26 | -------------------------------------------------------------------------------- /nbody/leapfrog/leapfrog.gp: -------------------------------------------------------------------------------- 1 | set term post eps enhanced color solid linewidth 2.0 font "Helvetica,18" 2 | set output "leapfrog.eps" 3 | 4 | set samples 1000 5 | set encoding iso_8859_1 6 | 7 | set xlabel "#threads / block" 8 | set ylabel "time / ms" 9 | 10 | 11 | set pointsize 3 12 | plot "leapfrog_1024.txt" u 1:2 w points title "N = 1024", "leapfrog_10240.txt" u 1:2 w points title "N = 10240", "leapfrog_51200.txt" u 1:2 w points title "N = 51200", "leapfrog_102400.txt" u 1:2 w points title "N = 102400" 13 | 14 | set output 15 | -------------------------------------------------------------------------------- /ngspice/rl_tran.cir: -------------------------------------------------------------------------------- 1 | Transient analysis of an LR circuit 2 | * Ngspice batch mode: 3 | * ngspice -b rl_tran.cir 4 | 5 | * Transient analysis 6 | * Max. step size: 100 ns, stop time: 10 ms 7 | .tran 10ns 10us 8 | .plot TRAN V(V0) V(Vr) 9 | 10 | * Sinusoidal input voltage 11 | * DC offset: 0 V, amplitude: 1 V, frequency: 1 MHz 12 | V0 V0 0 DC 0 SIN 0 1 1MEG 13 | 14 | * LR 15 | L1 V0 Vr 500u 16 | R1 Vr 0 1k 17 | 18 | * Save plot to file 19 | .control 20 | run 21 | set hcopydevtype=postscript 22 | hardcopy rl_tran.ps V0 Vr 23 | .endc 24 | 25 | .end 26 | -------------------------------------------------------------------------------- /vhdl/ex_1_10_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_1_10_tb.vhd 2 | -- ghdl -r --std=08 ex_1_10_tb --wave=ex_1_10.ghw 3 | -- gtkwave ex_1_10.ghw 4 | 5 | entity ex_1_10_tb is 6 | end ex_1_10_tb; 7 | 8 | architecture testbench of ex_1_10_tb is 9 | signal a, b, sel, z : bit; 10 | begin 11 | dut : entity work.mux(behav) 12 | port map ( a, b, sel, z ); 13 | 14 | process 15 | begin 16 | a <= '1'; 17 | b <= '0'; 18 | sel <= '0'; 19 | wait for 20 ns; 20 | sel <= '1'; 21 | wait for 20 ns; 22 | wait; 23 | end process; 24 | end testbench; 25 | -------------------------------------------------------------------------------- /libnfc/Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc -Wall -Wextra -pedantic -std=c99 -pipe -Os 2 | LIB = -lnfc 3 | 4 | all: list-devices list-tags dump-girogo tags 5 | 6 | tags: *.c 7 | ctags *.c 8 | 9 | list-devices: list-devices.c 10 | $(CC) -o list-devices list-devices.c $(LIB) 11 | 12 | list-tags: list-tags.c 13 | $(CC) -o list-tags list-tags.c $(LIB) 14 | 15 | dump-girogo: dump-girogo.c 16 | $(CC) -o dump-girogo dump-girogo.c $(LIB) 17 | 18 | clean: 19 | rm -f tags 20 | rm -f list-devices 21 | rm -f list-tags 22 | rm -f dump-girogo 23 | 24 | .PHONY: all clean 25 | -------------------------------------------------------------------------------- /nbody/nbody/nbody.gp: -------------------------------------------------------------------------------- 1 | set term post eps enhanced color solid linewidth 2.0 font "Helvetica,18" 2 | set output "nbody.eps" 3 | 4 | set samples 1000 5 | set encoding iso_8859_1 6 | 7 | set xlabel "#threads / block" 8 | set ylabel "#interactions / s" 9 | 10 | set key at 300, 1e10 11 | 12 | set pointsize 3 13 | plot "nbody_1024.txt" u 1:2 w points title "N = 1024", "nbody_4096.txt" u 1:2 w points title "N = 4096", "nbody_10240.txt" u 1:2 w points title "N = 10240", "nbody_20480.txt" u 1:2 w points title "N = 20480" 14 | 15 | set output 16 | -------------------------------------------------------------------------------- /vhdl/ex_3_14.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_14.vhd 2 | 3 | entity average is 4 | port ( clk : in bit; 5 | din : in real; 6 | dout : out real ); 7 | end entity average; 8 | 9 | architecture behav of average is 10 | constant N : integer := 4; 11 | begin 12 | process is 13 | variable sum : real; 14 | begin 15 | loop 16 | sum := 0.0; 17 | for i in 0 to N-1 loop 18 | wait until clk = '1'; 19 | sum := sum + din; 20 | end loop; 21 | 22 | dout <= sum / real(N); 23 | end loop; 24 | end process; 25 | end architecture behav; 26 | -------------------------------------------------------------------------------- /openscad/cap_fence_post_v2.scad: -------------------------------------------------------------------------------- 1 | // Cap for fence post 2 | // V2: reduced inner diameter by 1 mm 3 | 4 | $fa = 1; 5 | $fs = 0.4; 6 | 7 | // Inner diameter: 38.5 mm 8 | diameter = 38.5; 9 | // Thickness cap: 2 mm 10 | thickness = 2; 11 | // Height of cylinder part: 22 mm 12 | height = 22; 13 | // Height of top part: 3.4 mm 14 | top = 3.4; 15 | 16 | eps = 0.01; 17 | 18 | // Rotate for printing 19 | rotate([180,0,0]) 20 | difference() { 21 | cylinder(h=height+top, d=diameter+thickness); 22 | translate([0,0,-eps]) cylinder(h=height+2*eps, d=diameter); 23 | } 24 | -------------------------------------------------------------------------------- /vhdl/ex_2_10_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_10_tb.vhd 2 | -- ghdl -r --std=08 ex_2_10_tb --wave=ex_2_10.ghw 3 | -- gtkwave ex_2_10.ghw 4 | 5 | entity ex_2_10_tb is 6 | end ex_2_10_tb; 7 | 8 | architecture testbench of ex_2_10_tb is 9 | signal a, b, y : integer := 0; 10 | signal sel : bit; 11 | begin 12 | dut : entity work.alu(behav) 13 | port map ( a, b, sel, y ); 14 | 15 | process 16 | begin 17 | a <= 2; 18 | b <= 1; 19 | sel <= '0'; 20 | wait for 20 ns; 21 | sel <= '1'; 22 | wait for 20 ns; 23 | wait; 24 | end process; 25 | end testbench; 26 | -------------------------------------------------------------------------------- /greenpak4/Makefile.common: -------------------------------------------------------------------------------- 1 | YOSYS = yosys 2 | GP4PAR = gp4par 3 | GP4PROG = gp4prog 4 | VERILATOR = verilator 5 | 6 | all : $(PROJECT).txt 7 | 8 | $(PROJECT).json : $(SOURCE) 9 | $(YOSYS) -p 'read_verilog $(SOURCE)' \ 10 | -p 'synth_greenpak4 -part $(PART) -json $(PROJECT).json' 11 | 12 | %.txt : %.json 13 | $(GP4PAR) $< -o $@ -p $(PART) 14 | 15 | emulate : $(PROJECT).txt 16 | $(GP4PROG) -v 3.3 --emulate $(PROJECT).txt $(NETS) 17 | 18 | lint : 19 | $(VERILATOR) --lint-only $(SOURCE) 20 | 21 | clean : 22 | rm -f *.txt *.json 23 | 24 | .PHONY : clean emulate lint all 25 | -------------------------------------------------------------------------------- /vhdl/ex_4_13.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_13.vhd 2 | 3 | entity decoder is 4 | port ( a : in bit_vector(2 downto 0); 5 | y : out bit_vector(7 downto 0) ); 6 | end entity decoder; 7 | 8 | architecture behav of decoder is 9 | begin 10 | with a select 11 | y <= b"00000001" when b"000", 12 | b"00000010" when b"001", 13 | b"00000100" when b"010", 14 | b"00001000" when b"011", 15 | b"00010000" when b"100", 16 | b"00100000" when b"101", 17 | b"01000000" when b"110", 18 | b"10000000" when b"111"; 19 | end architecture behav; 20 | -------------------------------------------------------------------------------- /matlab/lorenz.m: -------------------------------------------------------------------------------- 1 | % Lorenz attractor (Matlab/Octave) 2 | 3 | % \dot x = \sigma (y - x) 4 | % \dot y = x (\rho - z) - y 5 | % \dot z = xy - \beta z 6 | 7 | clear 8 | close all 9 | format 10 | 11 | % Parameters 12 | sigma = 10; 13 | beta = 8/3; 14 | rho = 28; 15 | 16 | % Initial conditions 17 | x0 = [1, 1, 1]; 18 | 19 | f = @(t, x) [sigma * (x(2) - x(1)) ; ... 20 | x(1) * (rho - x(3)) - x(2); ... 21 | x(1) * x(2) - beta * x(3)]; 22 | 23 | t_span = [0:0.01:200]; 24 | 25 | [t, x] = ode45(f, t_span, x0); 26 | 27 | plot3(x(:, 1), x(:, 2), x(:, 3)); 28 | axis off 29 | -------------------------------------------------------------------------------- /systemc/lowpass/Makefile: -------------------------------------------------------------------------------- 1 | SYSTEMC_HOME ?= /usr/local/systemc-2.3.1 2 | 3 | CC := g++ 4 | CXXFLAGS := -std=c++98 -pedantic -Wall -Wextra -Wno-long-long \ 5 | -I. -I$(SYSTEMC_HOME)/include -Os -pipe 6 | LDFLAGS := -L. -L$(SYSTEMC_HOME)/lib-linux64 \ 7 | -Wl,-rpath=$(SYSTEMC_HOME)/lib-linux64 8 | LDLIBS := -lsystemc -lm 9 | 10 | main : main.o toplevel.o lowpass_tb.o lowpass.o 11 | 12 | main.o : toplevel.hpp 13 | toplevel.o : toplevel.hpp 14 | lowpass_tb.o : lowpass_tb.hpp 15 | lowpass.o : lowpass.hpp 16 | 17 | clean : 18 | rm -f main 19 | rm -f *.o 20 | rm -f *.vcd 21 | 22 | .PHONY : view clean 23 | -------------------------------------------------------------------------------- /systemc/lowpass/main.cc: -------------------------------------------------------------------------------- 1 | #include "toplevel.hpp" 2 | #include "lowpass_tb.hpp" 3 | 4 | int sc_main(int argc, char *argv[]) 5 | { 6 | (void)argc; 7 | (void)argv; 8 | 9 | Toplevel top("top"); 10 | 11 | // Create VCD file 12 | sc_trace_file *fp; 13 | fp = sc_create_vcd_trace_file("lowpass"); 14 | sc_trace(fp, top.tb.clk, "clk"); 15 | sc_trace(fp, top.tb.reset, "rst"); 16 | sc_trace(fp, top.tb.x, "x"); 17 | sc_trace(fp, top.tb.y, "y"); 18 | 19 | // Run simulation (250 ns) 20 | sc_start(250, SC_NS); 21 | 22 | // Close VCD file 23 | sc_close_vcd_trace_file(fp); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /vhdl/ex_4_9.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_9.vhd 2 | 3 | entity reg is 4 | port ( din : in bit_vector(31 downto 0); 5 | read, write : in integer range 0 to 15; 6 | wen : in bit; 7 | dout : out bit_vector(31 downto 0) ); 8 | end entity reg; 9 | 10 | architecture behav of reg is 11 | begin 12 | process (din, read, write, wen) is 13 | type mem is array(0 to 15) of bit_vector (31 downto 0); 14 | variable memory : mem; 15 | begin 16 | if (wen = '1') then 17 | memory(write) := din; 18 | end if; 19 | dout <= memory(read); 20 | end process; 21 | end architecture behav; 22 | -------------------------------------------------------------------------------- /vhdl/ex_2_9_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_9_tb.vhd 2 | -- ghdl -r --std=08 ex_2_9_tb --wave=ex_2_9.ghw 3 | -- gtkwave ex_2_9.ghw 4 | 5 | entity ex_2_9_tb is 6 | end ex_2_9_tb; 7 | 8 | architecture testbench of ex_2_9_tb is 9 | signal clk : bit; 10 | signal q : integer; 11 | begin 12 | dut : entity work.counter(behav) 13 | port map ( clk, q ); 14 | 15 | process 16 | begin 17 | clk <= '0'; 18 | wait for 20 ns; 19 | clk <= '1'; 20 | wait for 20 ns; 21 | clk <= '0'; 22 | wait for 20 ns; 23 | clk <= '1'; 24 | wait for 20 ns; 25 | wait; 26 | end process; 27 | end testbench; 28 | -------------------------------------------------------------------------------- /vhdl/ex_3_12_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_12_tb.vhd 2 | -- ghdl -r --std=08 ex_3_12_tb --wave=ex_3_12.ghw 3 | -- gtkwave ex_3_12.ghw 4 | 5 | entity ex_3_12_tb is 6 | end entity ex_3_12_tb; 7 | 8 | architecture testbench of ex_3_12_tb is 9 | signal clk : bit; 10 | signal count : natural; 11 | begin 12 | dut : entity work.counter(behav) 13 | port map ( clk, count ); 14 | 15 | process 16 | begin 17 | for i in 0 to 16 loop 18 | clk <= '0'; 19 | wait for 20 ns; 20 | clk <= '1'; 21 | wait for 20 ns; 22 | end loop; 23 | 24 | wait; 25 | end process; 26 | end architecture testbench; 27 | -------------------------------------------------------------------------------- /ngspice/rc_ac.cir: -------------------------------------------------------------------------------- 1 | AC analysis of an RC circuit 2 | * Ngspice batch mode: 3 | * ngspice -b rc_ac.cir 4 | 5 | * AC analysis 6 | * Plot frequency in decades, 100 points per decade 7 | * 1 Hz -> 100 kHz 8 | .ac DEC 100 1 100k 9 | *.plot AC V(V0) V(Vout) 10 | 11 | * AC voltage source 12 | * Amplitude for AC analysis: 1 V, phase: 0 13 | V0 V0 0 DC 0 SIN 0 1 100 AC 1 0 14 | 15 | * RC 16 | R1 V0 Vout 1k 17 | C1 Vout 0 1u 18 | 19 | * Save plot to file 20 | .control 21 | run 22 | set hcopydevice=postscript 23 | hardcopy rc_ac_mag.ps V(Vout)/V(V0) 24 | hardcopy rc_ac_phase.ps phase(V(Vout))*180/pi 25 | .endc 26 | 27 | .end 28 | -------------------------------------------------------------------------------- /nbody/nbody_cpu/Makefile: -------------------------------------------------------------------------------- 1 | GCC_WARNINGS:=-Wall -Wextra -pedantic 2 | GCC_OPTFLAGS:=-O3 -march=native -mtune=native -fomit-frame-pointer 3 | CFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 4 | CFLAGS+=-lm 5 | 6 | CC:=gcc 7 | 8 | nbody_cpu: nbody_cpu.c 9 | $(CC) $(CFLAGS) -o $@ $< 10 | 11 | profile: nbody_cpu.c 12 | $(CC) $(CFLAGS) -fprofile-generate -o nbody_cpu_profile nbody_cpu.c 13 | ./nbody_cpu_profile 10000 14 | 15 | optimized: nbody_cpu.c 16 | $(CC) $(CFLAGS) -fprofile-use -o nbody_cpu_opt nbody_cpu.c 17 | 18 | clean: 19 | rm -f nbody_cpu nbody_cpu_profile nbody_cpu_opt nbody_cpu.gcda 20 | 21 | .PHONY: clean 22 | -------------------------------------------------------------------------------- /vhdl/ex_1_11.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_1_10.vhd 2 | -- ghdl -a --std=08 ex_1_11.vhd 3 | 4 | entity mux4 is 5 | port ( a0, a1, a2, a3, 6 | b0, b1, b2 ,b3, 7 | sel : in bit; 8 | z0, z1, z2, z3 : out bit ); 9 | end mux4; 10 | 11 | architecture struct of mux4 is 12 | begin 13 | mux0 : entity work.mux(behav) 14 | port map ( a0, b0, sel, z0 ); 15 | mux1 : entity work.mux(behav) 16 | port map ( a1, b1, sel, z1 ); 17 | mux2 : entity work.mux(behav) 18 | port map ( a2, b2, sel, z2 ); 19 | mux3 : entity work.mux(behav) 20 | port map ( a3, b3, sel, z3 ); 21 | end struct; 22 | -------------------------------------------------------------------------------- /div.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // n + 10 | n^3 + 100 6 | 7 | int main() 8 | { 9 | uint64_t n; 10 | 11 | for (n = 0; n < 10000; ++n) 12 | if (((n*n*n + 100) % (n + 10)) == 0) 13 | printf("n = %" PRIu64 "\n", n); 14 | 15 | return 0; 16 | } 17 | 18 | /* 19 | gcc -Wall -std=c99 -pedantic -Wextra -O3 div.c 20 | ./a.out 21 | 22 | n = 0 23 | n = 2 24 | n = 5 25 | n = 8 26 | n = 10 27 | n = 15 28 | n = 20 29 | n = 26 30 | n = 35 31 | n = 40 32 | n = 50 33 | n = 65 34 | n = 80 35 | n = 90 36 | n = 140 37 | n = 170 38 | n = 215 39 | n = 290 40 | n = 440 41 | n = 890 42 | */ 43 | -------------------------------------------------------------------------------- /vhdl/ex_2_13.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_13.vhd 2 | 3 | library ieee; 4 | use ieee.std_logic_1164.all; 5 | 6 | entity tristate is 7 | port ( din, en : in std_ulogic; 8 | dout : out std_ulogic ); 9 | end tristate; 10 | 11 | architecture behav of tristate is 12 | begin 13 | process (din, en) is 14 | begin 15 | if en = '0' or en = 'L' then 16 | dout <= 'Z'; 17 | elsif (en = '1' or en = 'H') and (din = '0' or din = 'L') then 18 | dout <= '0'; 19 | elsif (en = '1' or en = 'H') and (din = '1' or din = 'H') then 20 | dout <= '1'; 21 | else 22 | dout <= 'X'; 23 | end if; 24 | end process; 25 | end behav; 26 | -------------------------------------------------------------------------------- /vhdl/ex_3_10.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_10.vhd 2 | 3 | entity limiter is 4 | port ( data_in, lower, upper : in integer; 5 | data_out : out integer; 6 | out_of_limits : out bit ); 7 | end entity limiter; 8 | 9 | architecture behav of limiter is 10 | begin 11 | process (data_in, lower, upper) is 12 | begin 13 | if data_in < lower then 14 | data_out <= lower; 15 | out_of_limits <= '1'; 16 | elsif data_in > upper then 17 | data_out <= upper; 18 | out_of_limits <= '1'; 19 | else 20 | data_out <= data_in; 21 | out_of_limits <= '0'; 22 | end if; 23 | end process; 24 | end architecture behav; 25 | -------------------------------------------------------------------------------- /ngspice/diode_dc.cir: -------------------------------------------------------------------------------- 1 | Diode current-voltage curve 2 | * Ngspice batch mode: 3 | * ngspice -b diode_dc.cir 4 | 5 | * DC Analysis (0-1 V in 1 mV steps) 6 | .dc V0 0 1 1m 7 | .plot DC I(Vmeas) V(Vd) 8 | 9 | * DC voltage source 10 | V0 V0 0 DC 1 11 | 12 | * Diode with current limiting resistor 13 | R1 V0 Vd 1k 14 | D1 Vd Vmeas DMOD 15 | * Dummy voltage source for measuring current through diode 16 | Vmeas Vmeas 0 DC 0 17 | 18 | * Diode model with default parameters 19 | .model DMOD D 20 | 21 | * Save plot to file 22 | .control 23 | run 24 | set hcopydevtype=postscript 25 | hardcopy diode_dc.ps I(Vmeas) vs V(Vd) 26 | .endc 27 | 28 | .end 29 | -------------------------------------------------------------------------------- /greenpak4/primitives/counter_inference/counter.v: -------------------------------------------------------------------------------- 1 | module top ( 2 | (* LOC = "P3" *) output wire clk, 3 | (* LOC = "P4" *) output wire y 4 | ); 5 | 6 | // LF Oscillator (~ 1.73 kHz) 7 | GP_LFOSC #( 8 | .PWRDN_EN(0), 9 | .AUTO_PWRDN(0), 10 | .OUT_DIV(1) // 1,2,4,16 11 | ) lfosc ( 12 | .PWRDN(1'b0), 13 | .CLKOUT(clk) 14 | ); 15 | 16 | localparam CNT_WIDTH = 2; 17 | reg[CNT_WIDTH-1:0] cnt = 2**CNT_WIDTH - 1; 18 | 19 | assign y = (cnt == 0); 20 | 21 | // Down counter, no reset 22 | always @(posedge clk) begin 23 | if (cnt == 0) 24 | cnt <= 2**CNT_WIDTH - 1; 25 | else 26 | cnt <= cnt - 1; 27 | end 28 | 29 | endmodule 30 | -------------------------------------------------------------------------------- /icestick/ws2812/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = ws2812 2 | SOURCE = top.v ledstring.v ws2812_tx.v 3 | 4 | YOSYS = yosys 5 | ARACHNE_PNR = arachne-pnr 6 | ICEPACK = icepack 7 | ICEPROG = iceprog 8 | IVERILOG = iverilog 9 | LINT = verilator --lint-only 10 | 11 | all : $(PROJECT).bin 12 | 13 | $(PROJECT).blif : $(SOURCE) 14 | $(YOSYS) -p "synth_ice40 -blif $(PROJECT).blif" $(SOURCE) 15 | 16 | %.asc : %.blif $(PROJECT).pcf 17 | $(ARACHNE_PNR) -d 1k -p $(PROJECT).pcf $< -o $@ 18 | 19 | %.bin : %.asc 20 | $(ICEPACK) $< $@ 21 | 22 | flash : $(PROJECT).bin 23 | $(ICEPROG) $< 24 | 25 | clean : 26 | rm -f *.blif *.asc *.bin *.vcd *.vvp 27 | 28 | .PHONY : clean all lint flash 29 | -------------------------------------------------------------------------------- /vhdl/ex_2_13_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_13_tb.vhd 2 | -- ghdl -r --std=08 ex_2_13_tb --wave=ex_2_13.ghw 3 | -- gtkwave ex_2_13.ghw 4 | 5 | library ieee; 6 | use ieee.std_logic_1164.all; 7 | 8 | entity ex_2_13_tb is 9 | end ex_2_13_tb; 10 | 11 | architecture testbench of ex_2_13_tb is 12 | signal din, en, dout : std_ulogic; 13 | begin 14 | dut : entity work.tristate(behav) 15 | port map ( din, en, dout ); 16 | 17 | process 18 | begin 19 | wait for 20 ns; 20 | en <= '0'; 21 | wait for 20 ns; 22 | en <= '1'; 23 | din <= '0'; 24 | wait for 20 ns; 25 | din <= '1'; 26 | wait for 20 ns; 27 | wait; 28 | end process; 29 | end testbench; 30 | -------------------------------------------------------------------------------- /vhdl/shiftreg.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=93 shiftreg.vhd 2 | 3 | library ieee; 4 | use ieee.std_logic_1164.all; 5 | 6 | entity shiftreg is 7 | port ( 8 | clk, ser, set : in std_logic; 9 | out1, out2, out3, out4 : out std_logic 10 | ); 11 | end entity; 12 | 13 | architecture behav of shiftreg is 14 | signal reg : std_logic_vector (1 to 4); 15 | begin 16 | process (clk, set) 17 | begin 18 | if set = '1' then 19 | reg <= (others => '1'); 20 | elsif rising_edge(clk) then 21 | reg <= ser & reg (1 to 3); 22 | end if; 23 | end process; 24 | 25 | out1 <= reg(1); 26 | out2 <= reg(2); 27 | out3 <= reg(3); 28 | out4 <= reg(4); 29 | end architecture; 30 | -------------------------------------------------------------------------------- /tcp-ip/Makefile: -------------------------------------------------------------------------------- 1 | #CFLAGS = -Wall -Wextra -pedantic -Os -pipe -std=gnu99 2 | CFLAGS = -Wall -Wextra -pedantic -Os -pipe -std=c99 3 | 4 | main : main.o 5 | 6 | up1 : 7 | @if [ ! -e /dev/net/tap ]; then sudo mknod /dev/net/tap c 10 200; fi 8 | @sudo ip link set dev tap0 up 9 | @sudo ip route add dev tap0 10.0.0.0/8 10 | @sudo ip address add dev tap0 local 10.0.0.1 11 | 12 | up2 : 13 | @if [ ! -e /dev/net/tap ]; then sudo mknod /dev/net/tap c 10 200; fi 14 | @sudo ip link set dev tap0 up 15 | @sudo ip route add dev tap0 172.16.0.0/12 16 | @sudo ip address add dev tap0 local 172.16.0.1 17 | 18 | clean: 19 | rm -f *.o 20 | rm -f main 21 | 22 | .PHONY : clean up1 up2 23 | -------------------------------------------------------------------------------- /openscad/cap_fence_post_v1.scad: -------------------------------------------------------------------------------- 1 | // Cap for fence post 2 | 3 | $fa = 1; 4 | $fs = 0.4; 5 | 6 | // Inner diameter: 39.5 mm 7 | diameter = 39.5; 8 | // Thickness cap: 2 mm 9 | thickness = 2; 10 | // Height of cylinder part: 22 mm 11 | height = 22; 12 | // Height of top part: 3.4 mm 13 | top = 3.4; 14 | 15 | eps = 0.01; 16 | 17 | // Rotate for printing 18 | rotate([180,0,0]) 19 | difference() { 20 | union() { 21 | translate([0,0,height/2]) resize([diameter+thickness,diameter+thickness,2*top]) sphere(d=diameter+thickness); 22 | cylinder(h=height, d=diameter+thickness, center=true); 23 | } 24 | cylinder(h=height+2*eps, d=diameter, center=true); 25 | } 26 | -------------------------------------------------------------------------------- /vhdl/ex_4_10_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_10_tb.vhd 2 | -- ghdl -r --std=08 ex_4_10_tb --wave=ex_4_10.ghw 3 | -- gtkwave ex_4_10.ghw 4 | 5 | entity ex_4_10_tb is 6 | end entity ex_4_10_tb; 7 | 8 | architecture testbench of ex_4_10_tb is 9 | signal din : bit_vector(15 downto 0); 10 | signal index : natural; 11 | signal one : bit; 12 | begin 13 | dut : entity work.priority(behav) 14 | port map ( din, index, one ); 15 | 16 | process 17 | begin 18 | din <= x"0000"; 19 | wait for 20 ns; 20 | din <= ('1', others => '0'); 21 | wait for 20 ns; 22 | din <= x"0001"; 23 | wait for 20 ns; 24 | 25 | wait; 26 | end process; 27 | end architecture testbench; 28 | -------------------------------------------------------------------------------- /vhdl/ex_4_12_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_12_tb.vhd 2 | -- ghdl -r --std=08 ex_4_12_tb --wave=ex_4_12.ghw 3 | -- gtkwave ex_4_12.ghw 4 | 5 | library ieee; 6 | use ieee.std_logic_1164.all; 7 | 8 | entity ex_4_12_tb is 9 | end entity ex_4_12_tb; 10 | 11 | architecture testbench of ex_4_12_tb is 12 | signal a, b : std_ulogic_vector(1 downto 0); 13 | signal y : std_ulogic; 14 | begin 15 | dut : entity work.gate(behav) 16 | port map ( a, b, y ); 17 | 18 | process 19 | begin 20 | a <= b"00"; 21 | b <= b"00"; 22 | wait for 20 ns; 23 | a <= b"11"; 24 | b <= b"11"; 25 | wait for 20 ns; 26 | 27 | wait; 28 | end process; 29 | end architecture testbench; 30 | -------------------------------------------------------------------------------- /vhdl/ex_3_13.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_13.vhd 2 | 3 | entity counter is 4 | port ( clk, load : in bit; 5 | data : in natural; 6 | count : out natural ); 7 | end entity counter; 8 | 9 | architecture behav of counter is 10 | begin 11 | process is 12 | variable c : natural := 15; 13 | begin 14 | count <= c; 15 | loop 16 | loop 17 | wait until clk = '1' or load = '1'; 18 | exit when load = '1'; 19 | 20 | -- load /= '0' 21 | c := c - 1 when c /= 0 22 | else 15; 23 | count <= c; 24 | end loop; 25 | 26 | -- load = '1' 27 | c := data; 28 | count <= c; 29 | end loop; 30 | end process; 31 | end architecture behav; 32 | -------------------------------------------------------------------------------- /icestick/blink/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = blink 2 | SOURCE = blink.v 3 | 4 | YOSYS = yosys 5 | ARACHNE_PNR = arachne-pnr 6 | ICEPACK = icepack 7 | ICEPROG = iceprog 8 | IVERILOG = iverilog 9 | LINT = verilator --lint-only 10 | 11 | all : $(PROJECT).bin 12 | 13 | $(PROJECT).blif : $(SOURCE) 14 | $(YOSYS) -p "synth_ice40 -blif $(PROJECT).blif" $(SOURCE) 15 | 16 | %.asc : %.blif $(PROJECT).pcf 17 | $(ARACHNE_PNR) -d 1k -p $(PROJECT).pcf $< -o $@ 18 | 19 | %.bin : %.asc 20 | $(ICEPACK) $< $@ 21 | 22 | lint : $(SOURCE) 23 | $(LINT) $(SOURCE) 24 | 25 | flash : $(PROJECT).bin 26 | $(ICEPROG) $< 27 | 28 | clean : 29 | rm -f *.blif *.asc *.bin *.vcd 30 | 31 | .PHONY : clean all lint flash 32 | -------------------------------------------------------------------------------- /systemc/fifo/sender.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SENDER_HPP 2 | #define SENDER_HPP 3 | 4 | #include 5 | using namespace sc_core; 6 | 7 | struct Sender : public sc_module 8 | { 9 | sc_in clock; 10 | sc_port > out; 11 | 12 | // Constructor 13 | SC_HAS_PROCESS(Sender); 14 | Sender(sc_module_name name) 15 | { 16 | (void)name; 17 | 18 | SC_THREAD(send); 19 | sensitive << clock.pos(); 20 | } 21 | 22 | // Sender thread 23 | void send() 24 | { 25 | const char *DATA = "Hello world!"; 26 | 27 | // Send data on every second rising edge 28 | do { 29 | wait(2); 30 | out->write(*DATA); 31 | } while (*++DATA); 32 | } 33 | }; 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /vhdl/ex_5_20.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_5_20.vhd 2 | 3 | library ieee; 4 | use ieee.std_logic_1164.all; 5 | use ieee.numeric_std.all; 6 | 7 | entity counter is 8 | port ( clk_n, load_en : in std_ulogic; 9 | d : in std_ulogic_vector(3 downto 0); 10 | q : out std_ulogic_vector(3 downto 0) ); 11 | end entity counter; 12 | 13 | architecture behav of counter is 14 | signal cnt : unsigned(3 downto 0) := (others => '0'); 15 | begin 16 | process (clk_n, load_en) is 17 | begin 18 | if load_en = '1' then 19 | cnt <= unsigned(d); 20 | elsif falling_edge(clk_n) then 21 | cnt <= cnt + 1; 22 | end if; 23 | end process; 24 | 25 | q <= std_ulogic_vector(cnt); 26 | end architecture behav; 27 | -------------------------------------------------------------------------------- /systemc/lowpass/lowpass_tb.cc: -------------------------------------------------------------------------------- 1 | #include "lowpass_tb.hpp" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Testbench::Testbench(sc_module_name name) 7 | { 8 | (void)name; 9 | 10 | SC_THREAD(genSignals); 11 | sensitive << clk.neg(); 12 | } 13 | 14 | void Testbench::genSignals() 15 | { 16 | // Pulse reset 17 | reset.write(true); 18 | x.write(0); 19 | wait(); 20 | reset.write(false); 21 | 22 | // Ramp up input 23 | for (int i = 0; i <= 5; ++i) { 24 | x.write(i*10); 25 | wait(); 26 | } 27 | 28 | // Keep input constant 29 | for (int i = 0; i < 5; ++i) 30 | wait(); 31 | 32 | // Ramp down input 33 | for (int i = 5; i >= 0; --i) { 34 | x.write(i*10); 35 | wait(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /vhdl/ex_3_10_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_10_tb.vhd 2 | -- ghdl -r --std=08 ex_3_10_tb --wave=ex_3_10.ghw 3 | -- gtkwave ex_3_10.ghw 4 | 5 | entity ex_3_10_tb is 6 | end entity ex_3_10_tb; 7 | 8 | architecture testbench of ex_3_10_tb is 9 | signal data_in, data_out, upper, lower : integer; 10 | signal out_of_limits : bit; 11 | begin 12 | dut : entity work.limiter(behav) 13 | port map ( data_in, lower, upper, data_out, out_of_limits ); 14 | 15 | process 16 | begin 17 | lower <= 5; 18 | upper <= 10; 19 | data_in <= 6; 20 | wait for 20 ns; 21 | data_in <= 1; 22 | wait for 20 ns; 23 | data_in <= 15; 24 | wait for 20 ns; 25 | wait; 26 | end process; 27 | end architecture testbench; 28 | -------------------------------------------------------------------------------- /vhdl/ex_4_11_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_11_tb.vhd 2 | -- ghdl -r --std=08 ex_4_11_tb --wave=ex_4_11.ghw 3 | -- gtkwave ex_4_11.ghw 4 | 5 | use work.array_type.all; 6 | 7 | entity ex_4_11_tb is 8 | end entity ex_4_11_tb; 9 | 10 | architecture testbench of ex_4_11_tb is 11 | --signal din : int_array(0 to -1); -- test assertion 12 | signal din : int_array(0 to 2); 13 | signal dout : integer; 14 | begin 15 | dut : entity work.max(behav) 16 | port map ( din, dout ); 17 | 18 | process 19 | begin 20 | din <= (1, 2, 3); 21 | wait for 20 ns; 22 | din <= (5, 4, 3); 23 | wait for 20 ns; 24 | din <= (1, 2, 1); 25 | wait for 20 ns; 26 | 27 | wait; 28 | end process; 29 | end architecture testbench; 30 | -------------------------------------------------------------------------------- /vhdl/ex_2_11_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_2_11_tb.vhd 2 | -- ghdl -r --std=08 ex_2_11_tb --wave=ex_2_11.ghw 3 | -- gtkwave ex_2_11.ghw 4 | 5 | entity ex_2_11_tb is 6 | end ex_2_11_tb; 7 | 8 | architecture testbench of ex_2_11_tb is 9 | signal data, sum : real; 10 | signal clk : bit; 11 | begin 12 | dut : entity work.integrator(behav) 13 | port map ( clk, data, sum ); 14 | 15 | process 16 | begin 17 | data <= 0.5; 18 | clk <= '0'; 19 | wait for 20 ns; 20 | clk <= '1'; 21 | wait for 20 ns; 22 | clk <= '0'; 23 | wait for 20 ns; 24 | clk <= '1'; 25 | wait for 20 ns; 26 | clk <= '0'; 27 | wait for 20 ns; 28 | clk <= '1'; 29 | wait for 20 ns; 30 | wait; 31 | end process; 32 | end testbench; 33 | -------------------------------------------------------------------------------- /vhdl/ex_4_11.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_11.vhd 2 | 3 | package array_type is 4 | type int_array is array (natural range <>) of integer; 5 | end package array_type; 6 | 7 | use work.array_type.all; 8 | 9 | entity max is 10 | port ( din : in int_array; 11 | dout : out integer ); 12 | end entity max; 13 | 14 | architecture behav of max is 15 | begin 16 | process (din) is 17 | variable max : integer; 18 | begin 19 | assert din'length /= 0 20 | report "Array has length zero!" 21 | severity failure; 22 | 23 | max := din'left; 24 | for i in din'range loop 25 | if din(i) > max then 26 | max := din(i); 27 | end if; 28 | dout <= max; 29 | end loop; 30 | end process; 31 | end architecture behav; 32 | -------------------------------------------------------------------------------- /icestick/bincnt/bincnt.vhd: -------------------------------------------------------------------------------- 1 | library ieee; 2 | use ieee.std_logic_1164.all; 3 | use ieee.numeric_std.all; 4 | 5 | entity bincnt is 6 | port (clk : in std_logic; 7 | led1, led2, led3, led4, led5 : out std_logic); 8 | end bincnt; 9 | 10 | architecture behavioral of bincnt is 11 | signal cnt : unsigned (4 downto 0) := (others => '0'); 12 | begin 13 | process (clk) 14 | variable delay : unsigned (23 downto 0) := (others => '0'); 15 | begin 16 | if rising_edge(clk) then 17 | if delay = 2_999_999 then 18 | cnt <= cnt + 1; 19 | delay := x"000000"; 20 | else 21 | delay := delay + 1; 22 | end if; 23 | end if; 24 | end process; 25 | 26 | (led5,led4,led3,led2,led1) <= std_logic_vector(cnt); 27 | end behavioral; 28 | -------------------------------------------------------------------------------- /vhdl/ex_4_9_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_9_tb.vhd 2 | -- ghdl -r --std=08 ex_4_9_tb --wave=ex_4_9.ghw 3 | -- gtkwave ex_4_9.ghw 4 | 5 | entity ex_4_9_tb is 6 | end entity ex_4_9_tb; 7 | 8 | architecture testbench of ex_4_9_tb is 9 | signal din, dout : bit_vector(31 downto 0); 10 | signal read, write : integer range 0 to 15; 11 | signal wen : bit := '0'; 12 | begin 13 | dut : entity work.reg(behav) 14 | port map ( din, read, write, wen, dout ); 15 | 16 | process 17 | begin 18 | read <= 1; 19 | wait for 20 ns; 20 | wen <= '1'; 21 | write <= 1; 22 | din <= x"ffffffff"; 23 | wait for 20 ns; 24 | wen <= '0'; 25 | write <= 0; 26 | wait for 20 ns; 27 | 28 | wait; 29 | end process; 30 | end architecture testbench; 31 | -------------------------------------------------------------------------------- /systemc/lowpass/lowpass.cc: -------------------------------------------------------------------------------- 1 | #include "lowpass.hpp" 2 | 3 | Lowpass::Lowpass(sc_module_name name) 4 | { 5 | (void)name; 6 | 7 | SC_METHOD(state); 8 | sensitive << clk << reset; 9 | 10 | SC_METHOD(transition); 11 | sensitive << x << x_1 << x_2 << x_3; 12 | } 13 | 14 | void Lowpass::state() 15 | { 16 | if (reset.read()) { 17 | x_1.write(0); 18 | x_2.write(0); 19 | x_3.write(0); 20 | y.write (0); 21 | } else if (clk.posedge()) { 22 | x_1.write(x.read() ); 23 | x_2.write(x_1.read() ); 24 | x_3.write(x_2.read() ); 25 | y.write (average.read()); 26 | } 27 | } 28 | 29 | void Lowpass::transition() 30 | { 31 | average.write( 32 | (x.read() 33 | + x_1.read() 34 | + x_2.read() 35 | + x_3.read() 36 | ) / 4); 37 | } 38 | -------------------------------------------------------------------------------- /icestick/bincnt/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = bincnt 2 | SOURCE = bincnt.vhd 3 | 4 | GHDL = ghdl 5 | GHDL_SYNTH = ~/ghdlsynth-beta 6 | YOSYS = yosys 7 | ARACHNE_PNR = arachne-pnr 8 | ICEPACK = icepack 9 | ICEPROG = iceprog 10 | 11 | all : $(PROJECT).bin 12 | 13 | work-obj93.cf : $(SOURCE) 14 | $(GHDL) -a $(SOURCE) 15 | 16 | $(PROJECT).blif : work-obj93.cf 17 | $(YOSYS) -m $(GHDL_SYNTH)/ghdl.so -p "ghdl $(PROJECT); \ 18 | synth_ice40 -blif $(PROJECT).blif" 19 | 20 | %.asc : %.blif $(PROJECT).pcf 21 | $(ARACHNE_PNR) -d 1k -p $(PROJECT).pcf $< -o $@ 22 | 23 | %.bin : %.asc 24 | $(ICEPACK) $< $@ 25 | 26 | flash : $(PROJECT).bin 27 | $(ICEPROG) $< 28 | 29 | clean : 30 | rm -f *.blif *.asc *.bin *.vcd *.vvp work-obj93.cf 31 | 32 | .PHONY : clean all lint flash 33 | -------------------------------------------------------------------------------- /vhdl/ex_1_11_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_1_11_tb.vhd 2 | -- ghdl -r --std=08 ex_1_11_tb --wave=ex_1_11.ghw 3 | -- gtkwave ex_1_11.ghw 4 | 5 | entity ex_1_11_tb is 6 | end ex_1_11_tb; 7 | 8 | architecture testbench of ex_1_11_tb is 9 | signal a0, a1, a2, a3, 10 | b0, b1, b2, b3, 11 | sel, 12 | z0, z1, z2, z3 : bit; 13 | begin 14 | dut : entity work.mux4(struct) 15 | port map ( a0, a1, a2, a3, 16 | b0, b1, b2, b3, 17 | sel, 18 | z0, z1, z2, z3 ); 19 | 20 | process 21 | begin 22 | (a0, a1, a2, a3) <= bit_vector'("0000"); 23 | (b0, b1, b2, b3) <= bit_vector'("1111"); 24 | sel <= '0'; 25 | wait for 20 ns; 26 | sel <= '1'; 27 | wait for 20 ns; 28 | wait; 29 | end process; 30 | end testbench; 31 | -------------------------------------------------------------------------------- /ngspice/ideal_opamp.cir: -------------------------------------------------------------------------------- 1 | Ideal opamp used as inverting amplifier 2 | * Ngspice batch mode: 3 | * ngspice -b ideal_opamp.cir 4 | 5 | * Transfer function 6 | * H = Vout / Vin = - R2/R1 = -10 7 | .tf V(Vout,0) Vin 8 | 9 | * DC voltage source, Vin = 1 V 10 | Vin Vin 0 DC 1V 11 | * Use opamp as inverting amplifier 12 | R1 Vin In_m 1k 13 | R2 In_m Vout 10k 14 | 15 | * Opamp 16 | X1 Vout In_m 0 opamp 17 | 18 | ********************************** 19 | * Subcircuit: ideal opamp * 20 | * Vout : output * 21 | * In_m : inverting input (-) * 22 | * In_p : non-inverting input (+) * 23 | ********************************** 24 | .subckt opamp Vout In_m In_p 25 | * Linear voltage-controlled voltage source 26 | E1 Vout 0 In_p In_m 1Meg 27 | .ends 28 | 29 | .end 30 | -------------------------------------------------------------------------------- /vhdl/ex_5_19_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_5_19_tb.vhd 2 | -- ghdl -r --std=08 ex_5_19_tb --wave=ex_5_19.ghw 3 | -- gtkwave ex_5_19.ghw 4 | 5 | library ieee; 6 | use ieee.std_logic_1164.all; 7 | 8 | entity ex_5_19_tb is 9 | end entity ex_5_19_tb; 10 | 11 | architecture testbench of ex_5_19_tb is 12 | signal a0, a1, a2, a3 : bit := '0'; 13 | signal sel0, sel1 : bit := '0'; 14 | signal y : bit; 15 | begin 16 | dut : entity work.mux4(behav) 17 | port map ( a0 => a0, a1 => a1, a2 => a2, a3 => a3, 18 | sel0 => sel0, sel1 => sel1, 19 | y => y ); 20 | 21 | process 22 | begin 23 | a0 <= '1'; 24 | a1 <= '0'; 25 | wait for 20 ns; 26 | sel0 <= '1'; 27 | wait for 20 ns; 28 | wait; 29 | end process; 30 | end architecture testbench; 31 | -------------------------------------------------------------------------------- /vhdl/ex_6_11.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_11.vhd 2 | -- ghdl -r --std=08 ex_6_11 --wave=ex_6_11.ghw 3 | -- gtkwave ex_6_11.ghw 4 | 5 | entity ex_6_11 is 6 | end entity ex_6_11; 7 | 8 | architecture behav of ex_6_11 is 9 | procedure gen_clk(signal reset : in bit; 10 | signal clk : out bit) is 11 | begin 12 | loop 13 | clk <= '1', '0' after 1 us; 14 | wait until reset = '1' for 20 us; 15 | if reset'event then 16 | clk <= '0'; 17 | return; 18 | end if; 19 | end loop; 20 | end procedure gen_clk; 21 | 22 | signal syn_clk, rst : bit := '0'; 23 | begin 24 | gen_clk(reset => rst, clk => syn_clk); 25 | process is 26 | begin 27 | wait for 60 us; 28 | rst <= '1'; 29 | wait for 30 us; 30 | wait; 31 | end process; 32 | end architecture behav; 33 | -------------------------------------------------------------------------------- /vhdl/ex_6_10.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_10.vhd 2 | -- ghdl -r --std=08 ex_6_10 3 | 4 | entity ex_6_10 is 5 | end entity ex_6_10; 6 | 7 | architecture behav of ex_6_10 is 8 | begin 9 | process is 10 | type real_array is array(natural range <>) of real; 11 | constant deviations : real_array := (1.0, 2.0, 3.0); 12 | variable sum_of_squares : real; 13 | 14 | procedure calc_sos (dev : in real_array; sos : out real) is 15 | variable tmp : real := 0.0; 16 | begin 17 | for i in dev'range loop 18 | tmp := tmp + dev(i)**2; 19 | end loop; 20 | 21 | sos := tmp; 22 | end procedure calc_sos; 23 | begin 24 | calc_sos(deviations, sum_of_squares); 25 | report "Sum of squares: " & to_string(sum_of_squares); 26 | wait; 27 | end process; 28 | end architecture behav; 29 | -------------------------------------------------------------------------------- /vhdl/ex_3_14_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_14_tb.vhd 2 | -- ghdl -r --std=08 ex_3_14_tb --wave=ex_3_14.ghw 3 | -- gtkwave ex_3_14.ghw 4 | 5 | entity ex_3_14_tb is 6 | end entity ex_3_14_tb; 7 | 8 | architecture testbench of ex_3_14_tb is 9 | signal clk : bit; 10 | signal din, dout : real; 11 | begin 12 | dut : entity work.average(behav) 13 | port map ( clk, din, dout ); 14 | 15 | process 16 | begin 17 | for i in 0 to 3 loop 18 | din <= real(i); 19 | clk <= '0'; 20 | wait for 20 ns; 21 | clk <= '1'; 22 | wait for 20 ns; 23 | end loop; 24 | 25 | for i in 4 to 7 loop 26 | din <= real(i); 27 | clk <= '0'; 28 | wait for 20 ns; 29 | clk <= '1'; 30 | wait for 20 ns; 31 | end loop; 32 | 33 | wait; 34 | end process; 35 | end architecture testbench; 36 | -------------------------------------------------------------------------------- /python/pi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | 4 | from numpy import random 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | 8 | N = 1000 9 | 10 | circlex = [] 11 | circley = [] 12 | 13 | squarex = [] 14 | squarey = [] 15 | 16 | i = 1 17 | 18 | while i <= N: 19 | x = random.uniform(-1, 1) 20 | y = random.uniform(-1, 1) 21 | 22 | if (x**2 + y**2) <= 1.: 23 | circlex.append(x) 24 | circley.append(y) 25 | else: 26 | squarex.append(x) 27 | squarey.append(y) 28 | 29 | i += 1 30 | 31 | pi = 4. * len(circlex) / N 32 | 33 | print("Pi is approximately", pi) 34 | print("Error:", (pi-np.pi)/np.pi*100, "%") 35 | 36 | plt.plot(circlex, circley, "b.") 37 | plt.plot(squarex, squarey, "g.") 38 | plt.grid() 39 | 40 | plt.show() -------------------------------------------------------------------------------- /matlab/logistic_map.m: -------------------------------------------------------------------------------- 1 | % Logistic map (time plot) 2 | 3 | clc 4 | clear 5 | close all 6 | format 7 | 8 | % Set default interpreter for xlabel, ylabel, title, ... 9 | set(0, 'defaultTextInterpreter', 'latex'); 10 | set(groot, 'defaultAxesTickLabelInterpreter', 'latex'); 11 | set(groot, 'defaultLegendInterpreter', 'latex'); 12 | 13 | % mu = 0.5; % x_i -> 0 14 | mu = 2.8; % x_i -> fixed point 15 | % mu = 3; % x_i oscillates 16 | 17 | N = 40; 18 | x = zeros(1, N); 19 | x(1) = 0.5; 20 | 21 | for i=2:N 22 | x(i) = mu * x(i-1) * (1 - x(i-1)); 23 | end 24 | 25 | figure 26 | plot(x) 27 | xlabel('Time step', 'FontSize', 15) 28 | ylabel('$$x_i$$', 'FontSize', 15) 29 | set(gca, 'FontSize', 15) 30 | 31 | % Fixed point 32 | x_f = (mu - 1) / mu; 33 | hold on; plot(ones(1, N) * x_f); hold off -------------------------------------------------------------------------------- /ngspice/npn_dc.cir: -------------------------------------------------------------------------------- 1 | NPN BJT current-voltage curves 2 | * Ngspice batch mode: 3 | * ngspice -b npn_dc.cir 4 | 5 | * Collector-emitter voltage: 0 V -> 5V in 1 mV steps (inner loop) 6 | * Base current : 5 uA -> 25 uA in 5 uA steps (outer loop) 7 | .dc Vce 0 5 1m Ib 5u 25u 5u 8 | .plot DC I(Vc) V(Vce) 9 | 10 | * Current source (base current) 11 | Ib 0 Vb DC 0 12 | * Voltage source (collector-emitter voltage) 13 | Vce Vce 0 DC 0 14 | * Dummy voltage source for measuring collector current 15 | Vc Vce Vc DC 0 16 | * NPN BJT transistor 17 | Q1 Vc Vb 0 NPNMOD 18 | 19 | * Transistor model with default parameters 20 | .model NPNMOD NPN 21 | 22 | * Save plot to file 23 | .control 24 | run 25 | set hcopydevtype=postscript 26 | plot I(Vc) vs V(Vce) 27 | hardcopy npn_dc.ps I(Vc) vs V(Vce) 28 | .endc 29 | 30 | .end 31 | -------------------------------------------------------------------------------- /systemc/fifo/receiver.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RECEIVER_HPP 2 | #define RECEIVER_HPP 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace sc_core; 9 | 10 | struct Receiver : public sc_module 11 | { 12 | sc_in clock; 13 | sc_port > in; 14 | 15 | char c; 16 | 17 | // Constructor 18 | SC_HAS_PROCESS(Receiver); 19 | Receiver(sc_module_name name) 20 | { 21 | (void)name; 22 | 23 | c = 'X'; 24 | 25 | SC_THREAD(receive); 26 | sensitive << clock.pos(); 27 | } 28 | 29 | // Receiver thread 30 | void receive() 31 | { 32 | for (;;) { 33 | wait(); 34 | c = '*'; 35 | // Non blocking read, doesn't overwrite c 36 | // if no data is available 37 | in->nb_read(c); 38 | 39 | cout << c; 40 | } 41 | } 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /vhdl/events.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 events.vhd 2 | 3 | library ieee; 4 | use ieee.std_logic_1164.all; 5 | 6 | entity events is 7 | port ( 8 | clk, res, sig, ce : in std_logic; 9 | evt : out std_logic 10 | ); 11 | end entity; 12 | 13 | architecture behav of events is 14 | signal ff1q, ff2q : std_logic; 15 | begin 16 | -- D-FF1 17 | process (clk) 18 | begin 19 | if rising_edge(clk) then 20 | if res = '1' then 21 | ff1q <= '0'; 22 | elsif ce = '1' then 23 | ff1q <= sig; 24 | end if; 25 | end if; 26 | end process; 27 | 28 | -- D-FF2 29 | process (clk) 30 | begin 31 | if rising_edge(clk) then 32 | if res = '1' then 33 | ff2q <= '0'; 34 | else 35 | ff2q <= ff1q; 36 | end if; 37 | end if; 38 | end process; 39 | 40 | -- EXOR 41 | evt <= ff1q xor ff2q; 42 | end architecture; 43 | -------------------------------------------------------------------------------- /icestick/ws2812/ledstring_tb.v: -------------------------------------------------------------------------------- 1 | // Compile with: iverilog -o ledstring_tb.vvp ledstring_tb.v ledstring.v ws2812_tx.v 2 | // Run with: vvp ledstring_tb.vvp 3 | // GTKWave: gtkwave ledstring.vcd 4 | 5 | `default_nettype none 6 | `timescale 1ns / 1ps 7 | 8 | module ledstring_tb; 9 | reg reset, clock; 10 | wire dout; 11 | 12 | initial begin 13 | reset <= 1'b1; 14 | clock <= 1'b0; 15 | end 16 | 17 | initial begin 18 | $dumpfile("ledstring.vcd"); 19 | $dumpvars(0, uut); 20 | 21 | @(negedge clock); 22 | reset = 0; 23 | @(negedge clock); 24 | reset = 1; 25 | repeat (20000) @(posedge clock); 26 | 27 | $finish; 28 | end 29 | 30 | // Clock period: 1/48 MHz = 20.833 ns 31 | always #(20.833/2) clock = ~clock; 32 | 33 | ledstring uut ( 34 | .clk(clock), 35 | .rst(reset), 36 | .dout(dout) 37 | ); 38 | endmodule 39 | -------------------------------------------------------------------------------- /vhdl/ex_3_11_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_11_tb.vhd 2 | -- ghdl -r --std=08 ex_3_11_tb --wave=ex_3_11.ghw 3 | -- gtkwave ex_3_11.ghw 4 | 5 | entity ex_3_11_tb is 6 | end entity ex_3_11_tb; 7 | 8 | architecture testbench of ex_3_11_tb is 9 | signal x, y, z : real; 10 | signal f0, f1 : bit; 11 | begin 12 | dut : entity work.float_alu(behav) 13 | port map ( x, y, f0, f1, z ); 14 | 15 | process 16 | begin 17 | x <= 1.0; 18 | y <= 2.0; 19 | 20 | -- addition 21 | f1 <= '0'; 22 | f0 <= '0'; 23 | wait for 20 ns; 24 | 25 | -- subtraction 26 | f1 <= '0'; 27 | f0 <= '1'; 28 | wait for 20 ns; 29 | 30 | -- multiplication 31 | f1 <= '1'; 32 | f0 <= '0'; 33 | wait for 20 ns; 34 | 35 | -- division 36 | f1 <= '1'; 37 | f0 <= '1'; 38 | wait for 20 ns; 39 | 40 | wait; 41 | end process; 42 | end architecture testbench; 43 | -------------------------------------------------------------------------------- /matlab/logistic_map_bifurcation.m: -------------------------------------------------------------------------------- 1 | % Logistic map (bifurcations) 2 | 3 | clc 4 | clear 5 | close all 6 | format 7 | 8 | % Set default interpreter for xlabel, ylabel, title, ... 9 | set(0, 'defaultTextInterpreter', 'latex'); 10 | set(groot, 'defaultAxesTickLabelInterpreter', 'latex'); 11 | set(groot, 'defaultLegendInterpreter', 'latex'); 12 | 13 | N = 1000; 14 | M = 100000; 15 | x = zeros(1, N); 16 | x_result = zeros(1, M); 17 | 18 | k = 1; 19 | for mu=linspace(0, 4, M) 20 | x(1) = rand; 21 | 22 | for i=2:N 23 | x(i) = mu * x(i-1) * (1 - x(i-1)); 24 | end 25 | 26 | x_result(k) = x(N); 27 | k = k + 1; 28 | end 29 | 30 | figure 31 | scatter(linspace(0, 4, M), x_result, 0.5) 32 | xlabel('$$\mu$$', 'FontSize', 15) 33 | ylabel(strcat(['$$x_{', int2str(N), '}$$']), 'FontSize', 15) 34 | set(gca, 'FontSize', 15) -------------------------------------------------------------------------------- /vhdl/ex_3_13_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_3_13_tb.vhd 2 | -- ghdl -r --std=08 ex_3_13_tb --wave=ex_3_13.ghw 3 | -- gtkwave ex_3_13.ghw 4 | 5 | entity ex_3_13_tb is 6 | end entity ex_3_13_tb; 7 | 8 | architecture testbench of ex_3_13_tb is 9 | signal clk, load : bit := '0'; 10 | signal data, count : natural; 11 | begin 12 | dut : entity work.counter(behav) 13 | port map ( clk, load, data, count ); 14 | 15 | process 16 | begin 17 | for i in 0 to 16 loop 18 | clk <= '0'; 19 | wait for 20 ns; 20 | clk <= '1'; 21 | wait for 20 ns; 22 | end loop; 23 | 24 | data <= 10; 25 | load <= '1'; 26 | wait for 5 ns; 27 | load <= '0'; 28 | 29 | for i in 0 to 16 loop 30 | clk <= '0'; 31 | wait for 20 ns; 32 | clk <= '1'; 33 | wait for 20 ns; 34 | end loop; 35 | 36 | wait; 37 | end process; 38 | end architecture testbench; 39 | -------------------------------------------------------------------------------- /vhdl/ex_5_19.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_5_19.vhd 2 | 3 | entity mux4 is 4 | port ( a0, a1, a2, a3 : in bit; 5 | sel0, sel1 : in bit; 6 | y : out bit ); 7 | constant T_PD : time := 4.5 ns; 8 | end entity mux4; 9 | 10 | /* 11 | architecture behav of mux4 is 12 | begin 13 | with bit_vector'(sel0, sel1) select 14 | y <= a0 when "00", 15 | a1 when "01", 16 | a2 when "10", 17 | a3 when "11"; 18 | end architecture behav; 19 | */ 20 | 21 | architecture behav of mux4 is 22 | signal sel : bit_vector(0 to 1); 23 | begin 24 | sel <= (sel0, sel1); 25 | 26 | process (all) is 27 | begin 28 | case sel is 29 | when "00" => y <= a0 after T_PD; 30 | when "01" => y <= a1 after T_PD; 31 | when "10" => y <= a2 after T_PD; 32 | when "11" => y <= a3 after T_PD; 33 | end case; 34 | end process; 35 | end architecture behav; 36 | -------------------------------------------------------------------------------- /vhdl/ex_5_20_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_5_20_tb.vhd 2 | -- ghdl -r --std=08 ex_5_20_tb --wave=ex_5_20.ghw 3 | -- gtkwave ex_5_20.ghw 4 | 5 | library ieee; 6 | use ieee.std_logic_1164.all; 7 | 8 | entity ex_5_20_tb is 9 | end entity ex_5_20_tb; 10 | 11 | architecture testbench of ex_5_20_tb is 12 | signal clk_n : std_ulogic := '1'; 13 | signal load_en : std_ulogic := '0'; 14 | signal d, q : std_ulogic_vector(3 downto 0); 15 | begin 16 | dut : entity work.counter(behav) 17 | port map ( clk_n => clk_n, load_en => load_en, 18 | d => d, q => q ); 19 | 20 | process 21 | begin 22 | d <= "1111"; 23 | load_en <= '1', '0' after 10 ns; 24 | 25 | for i in 0 to 15 loop 26 | clk_n <= '0'; 27 | wait for 10 ns; 28 | clk_n <= '1'; 29 | wait for 10 ns; 30 | end loop; 31 | 32 | wait; 33 | end process; 34 | end architecture testbench; 35 | -------------------------------------------------------------------------------- /systemc/fifo/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "sender.hpp" 4 | #include "receiver.hpp" 5 | 6 | using namespace std; 7 | using namespace sc_core; 8 | 9 | int sc_main(int argc, char *argv[]) 10 | { 11 | (void)argc; 12 | (void)argv; 13 | 14 | // Modules 15 | Sender s("s"); 16 | Receiver r("r"); 17 | 18 | // Channels 19 | sc_clock clock("clock", 10, SC_NS); 20 | sc_fifo fifo ("fifo", 10); 21 | 22 | // Connect modules 23 | s.clock(clock); 24 | s.out (fifo ); 25 | r.clock(clock); 26 | r.in (fifo ); 27 | 28 | // Create VCD file 29 | sc_trace_file *fp; 30 | fp = sc_create_vcd_trace_file("main"); 31 | sc_trace(fp, s.clock, "clock"); 32 | sc_trace(fp, r.c, "c"); 33 | 34 | // Run simulation 35 | sc_start(400, SC_NS); 36 | cout << endl; 37 | 38 | // Close VCD file 39 | sc_close_vcd_trace_file(fp); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /count_tb.v: -------------------------------------------------------------------------------- 1 | // count_tb.v 2 | // 3 | // Compile with: iverilog -o count_tb.vvp count_tb.v count.v 4 | // Run with: vvp count_tb.vvp 5 | // GTKWave: gtkwave count.vcd 6 | 7 | `timescale 1 ns / 1 ps 8 | 9 | module count_tb(); 10 | localparam COUNTER_BITS = 3; 11 | 12 | reg reset; 13 | reg clock = 0; 14 | wire[COUNTER_BITS-1:0] counter; 15 | 16 | initial begin 17 | $display("initial time value: %d", $time); 18 | $dumpfile("count.vcd"); 19 | $dumpvars(0, c); 20 | $monitor("reset: %b\tclock: %b\tcounter: %b", 21 | reset, clock, counter); 22 | 23 | // pulse reset 24 | reset = 0; 25 | #1; 26 | reset = 1; 27 | #1; 28 | reset = 0; 29 | 30 | #24; 31 | $finish; 32 | end 33 | 34 | always #1 clock = ~clock; 35 | 36 | count #( 37 | .COUNTER_WIDTH(COUNTER_BITS)) 38 | c ( 39 | .rst(reset), 40 | .clk(clock), 41 | .cnt(counter) 42 | ); 43 | endmodule 44 | -------------------------------------------------------------------------------- /python/feigenbaum.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | @author: m-thu 5 | """ 6 | 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | # Feigenbaum diagram 11 | # 12 | # z_0 = 0 13 | # z_n = z_{n-1}^2 + c 14 | # 15 | # -2 < c < 0.25 16 | # z_99 ... z_105 17 | 18 | if __name__ == "__main__": 19 | plt.close("all") 20 | 21 | N = 1000 22 | min = 99 23 | max = 105 24 | #min = 200 25 | #max = 210 26 | 27 | c = np.linspace(-2, 0.25, N) 28 | x = [] 29 | y = [] 30 | for idx in range(0, N): 31 | z = 0. 32 | for i in range(0, min): 33 | z = z**2 + c[idx] 34 | for i in range(min, max): 35 | z = z**2 + c[idx] 36 | x.append(c[idx]) 37 | y.append(z) 38 | 39 | plt.close("all") 40 | fig = plt.figure() 41 | ax = fig.add_subplot(111) 42 | ax.set(xlabel = "c", ylabel = r"$z_{"+str(min)+r"}\,\dots\,z_{"+str(max)+r"}$") 43 | ax.scatter(x, y, marker = ".", s = 0.5) 44 | plt.show() -------------------------------------------------------------------------------- /libnfc/README.md: -------------------------------------------------------------------------------- 1 | Libnfc tools 2 | ------------ 3 | 4 | Install libnfc: 5 | ```sh 6 | git clone https://github.com/nfc-tools/libnfc.git 7 | cd libnfc 8 | autoreconf -is --force 9 | ./configure && make && sudo make install 10 | sudo sh -c 'echo /usr/local/lib >>/etc/ld.so.conf' && sudo ldconfig 11 | ``` 12 | 13 | Install udev rule for ACR122U and blacklist `nfc` and `pn533` modules: 14 | ```sh 15 | sudo cp 80-acr122u.rules /etc/udev/rules.d/ 16 | sudo cp blacklist-pn533.conf /etc/modprobe.d/ 17 | ``` 18 | 19 | Tools: 20 | * [list-devices](list-devices.c): Lists all connected devices supported by libnfc 21 | * [list-tags](list-tags.c): Lists all tags in vicinity of reader 22 | * [dump-girogo](dump-girogo.c): Dumps girogo cards 23 | 24 | ### Resources 25 | * [Libnfc - NFC Tools](http://nfc-tools.org/index.php?title=Libnfc) 26 | * [Libnfc API Reference](http://www.libnfc.org/api/) 27 | * [Libnfc on GitHub](https://github.com/nfc-tools/libnfc) 28 | -------------------------------------------------------------------------------- /python/int.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | Created on Sat Jun 9 13:14:35 2018 5 | 6 | @author: matthias 7 | """ 8 | 9 | # = 1/(b-a) * \int_a^b f(x) dx 10 | # (b-a) * = \int_a^b f(x) dx 11 | # --> \int_a^b f(x) dx ~ (b-a)/N * \sum_{i=1}^N f(x_i) 12 | 13 | from scipy import random 14 | import numpy as np 15 | from numpy import pi 16 | import matplotlib.pyplot as plt 17 | 18 | def integrate(f, a, b, N): 19 | xrand = random.uniform(a, b, N) 20 | 21 | sum = 0. 22 | 23 | for x in xrand: 24 | sum += f(x) 25 | 26 | return sum * (b-a) / N 27 | 28 | if __name__ == "__main__": 29 | f = lambda x: np.sin(x) 30 | a = 0 31 | b = pi 32 | N = 1000 33 | 34 | areas = [] 35 | for i in range(N): 36 | areas.append(integrate(f, a, b, N)) 37 | 38 | plt.hist(areas, bins = 30, ec = "black") 39 | plt.xlabel("Value of the integral") 40 | plt.show() -------------------------------------------------------------------------------- /fortran/logistic-map.f95: -------------------------------------------------------------------------------- 1 | ! Logistic map 2 | ! Matthias Thumann, 2021 3 | 4 | program logistic_map 5 | real*8, parameter :: mu_min = 0., mu_max = 4. 6 | real*8, parameter :: mu_steps = 100000. 7 | integer, parameter :: N_max = 100 ! Max. iterations before plotting 8 | real*8 :: mu, x 9 | 10 | ! Initialize PGPLOT library 11 | call pgbegin(0, '/xwin', 1, 1) 12 | call pgenv(sngl(mu_min), sngl(mu_max), 0., 1., 0, 1) 13 | call pglab('mu', 'x_1000', 'Logistic map') 14 | 15 | mu = mu_min 16 | delta = (mu_max - mu_min) / mu_steps 17 | do 18 | if (mu > mu_max) exit 19 | 20 | x = rand(0) 21 | do i = 1, N_max 22 | x = mu * x * (1 - x) 23 | enddo 24 | 25 | call pgsci(2) ! Color 26 | call pgpt(1, sngl(mu), sngl(x), -1) 27 | 28 | mu = mu + delta 29 | enddo 30 | 31 | call pgend 32 | end -------------------------------------------------------------------------------- /matlab/heat.m: -------------------------------------------------------------------------------- 1 | % Numerical solution of 1-dimensional heat equation 2 | % Matthias Thumann, 2021 3 | 4 | clc 5 | clear 6 | close all 7 | format 8 | 9 | % Parameters 10 | k = 1; 11 | c = 1; 12 | rho = 1; 13 | 14 | L = 20; % Length 15 | T0 = 100; % Initial temperature 16 | t_max = 200; 17 | delta_x = 5; 18 | delta_t = 10; 19 | 20 | eta = k * delta_t / (c * rho * delta_x^2); 21 | 22 | % Boundary condition: T(x=0) = 0, T(x=L) = 0 23 | T = zeros(L, t_max+1); 24 | 25 | % Initial temperature: T0 for 2 <= x <= L - 1 26 | T(2:L-1,1) = ones(L-2,1) * T0; 27 | 28 | for j=1:t_max 29 | for i=2:L-1 30 | T(i,j+1) = T(i,j) + ... 31 | eta * (T(i+1,j) + T(i-1, j) - 2*T(i, j)); 32 | end 33 | end 34 | 35 | figure(1) 36 | surf([1:t_max+1], [1:L], T) 37 | xlabel('Time') 38 | ylabel('Position') 39 | zlabel('Temperature') 40 | 41 | figure(2) 42 | imagesc(T) 43 | xlabel('Time') 44 | ylabel('Position') -------------------------------------------------------------------------------- /matlab/julia4d.m: -------------------------------------------------------------------------------- 1 | % 4D Julia set (Matlab/Octave) 2 | 3 | clear all 4 | close all 5 | format 6 | 7 | % Plot area 8 | x_min = -1.5; 9 | x_max = 1.5; 10 | y_min = -1.5; 11 | y_max = 1.5; 12 | z_min = -1.5; 13 | z_max = 1.5; 14 | 15 | % Resolution 16 | res = 0.05; 17 | 18 | % Maximum absolute value for q 19 | s = 2; 20 | 21 | % Maximum number of iterations 22 | max_iter = 23; 23 | 24 | c = Quaternion([-1, 0.2, 0, 0]); 25 | 26 | X = []; 27 | Y = []; 28 | Z = []; 29 | 30 | tic(); 31 | 32 | for x = x_min:res:x_max 33 | for y = y_min:res:y_max 34 | for z = z_min:res:z_max 35 | q = Quaternion([x, y, z, 0]); 36 | for i = 1:max_iter 37 | q = q*q + c; 38 | if (q.Norm > s) 39 | break 40 | end 41 | end 42 | if i == max_iter 43 | X = [X, x]; 44 | Y = [Y, y]; 45 | Z = [Z, z]; 46 | end 47 | end 48 | end 49 | end 50 | 51 | disp(['Time elapsed: ', num2str(toc()), ' s']) 52 | 53 | figure(1) 54 | scatter3(X, Y, Z, 6) 55 | -------------------------------------------------------------------------------- /matlab/mandelbrot.m: -------------------------------------------------------------------------------- 1 | % Mandelbrot set (Matlab/Octave) 2 | 3 | % z_{n+1} = z_n^2 + c, z_0 = 0, z,c \in \mathbb{C} 4 | % c \in \mathcal{M} \iff \limsup\limits_{n\to\infty} |z_{n+1}| \leq 2 5 | 6 | clear 7 | close all 8 | format 9 | 10 | % Maximum absolute value of z_{n+1} 11 | s = 2; 12 | 13 | % Maximum number of iterations 14 | max_iter = 23; 15 | 16 | % Plot area 17 | x_min = -2.1; 18 | x_max = 0.9; 19 | y_min = -1.2; 20 | y_max = 1.2; 21 | 22 | % Resolution 23 | res = 0.001; 24 | 25 | x = x_min:res:x_max; 26 | y = y_min:res:y_max; 27 | [c_x, c_y] = meshgrid(x, y); 28 | c = complex(c_x, c_y); 29 | z = zeros(size(c)); 30 | M = zeros(size(c)); 31 | 32 | tic() 33 | 34 | for i = 1:max_iter 35 | z = z.^2 + c; 36 | M = M + (abs(z) <= s); 37 | end 38 | 39 | disp(['Time elapsed: ', num2str(toc()), ' s']) 40 | 41 | figure(1) 42 | imagesc(M) 43 | colormap([jet(); flipud(jet()); 0 0 0]); 44 | %colormap jet 45 | axis off 46 | -------------------------------------------------------------------------------- /sds011/makegraphs: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # directory containing log files 4 | LOGDIR=/home/pi/sds011/logs 5 | # gnuplot script file 6 | GPSCRIPT=/home/pi/sds011/graph.gp 7 | 8 | # directory for generated files 9 | OUTDIR=/home/pi/sds011/html 10 | # filename for generated HTML file 11 | HTML=index.html 12 | 13 | HOME=`pwd` 14 | cd $LOGDIR 15 | 16 | cat >$OUTDIR/$HTML < 18 | 19 | 20 | 21 | SDS011 dust sensor data 22 | 23 | 24 | HTML_HEADER 25 | 26 | for LOG in $LOGDIR/*; do 27 | gnuplot -e "filename='${LOG}'" $GPSCRIPT >$OUTDIR/`basename $LOG`.png 28 | cat >>$OUTDIR/$HTML < 30 | `basename $LOG` 31 |
Date: `basename $LOG`
32 | 33 | HTML_IMAGE 34 | done 35 | 36 | cat >>$OUTDIR/$HTML < 38 | 39 | HTML_FOOTER 40 | 41 | cd $HOME 42 | -------------------------------------------------------------------------------- /vhdl/ex_6_18.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_18.vhd 2 | -- ghdl -r --std=08 ex_6_18 3 | 4 | entity ex_6_18 is 5 | end entity ex_6_18; 6 | 7 | architecture behav of ex_6_18 is 8 | function "and" (left, right : integer) return boolean is 9 | variable l, r : boolean; 10 | begin 11 | l := false when left = 0 else true when left /= 0; 12 | r := false when right = 0 else true when right /= 0; 13 | return l and r; 14 | end function "and"; 15 | 16 | function "or" (left, right : integer) return boolean is 17 | variable l, r : boolean; 18 | begin 19 | l := false when left = 0 else true when left /= 0; 20 | r := false when right = 0 else true when right /= 0; 21 | return l or r; 22 | end function "or"; 23 | begin 24 | process is 25 | begin 26 | report "0 and 1 = " & to_string(0 and 1); 27 | report "1 and 1 = " & to_string(1 and 1); 28 | report "0 or 0 = " & to_string(0 or 0); 29 | report "0 or 1 = " & to_string(0 or 1); 30 | wait; 31 | end process; 32 | end architecture behav; 33 | -------------------------------------------------------------------------------- /icestick/ws2812/ws2812_tx_tb.v: -------------------------------------------------------------------------------- 1 | // Compile with: iverilog -o ws2812_tx_tb.vvp ws2812_tx_tb.v ws2812_tx.v 2 | // Run with: vvp ws2812_tx_tb.vvp 3 | // GTKWave: gtkwave ws2812_tx.vcd 4 | 5 | `default_nettype none 6 | `timescale 1ns / 1ps 7 | 8 | module ws2812_tx_tb; 9 | `define T_CLK 20.833 10 | 11 | reg reset, clock, start; 12 | wire busy; 13 | reg [23:0] data = 24'haaaaaa; 14 | 15 | initial begin 16 | reset <= 1'b1; 17 | clock <= 1'b0; 18 | start <= 1'b0; 19 | end 20 | 21 | initial begin 22 | $dumpfile("ws2812_tx.vcd"); 23 | $dumpvars(0, uut); 24 | 25 | @(negedge clock); 26 | reset = 0; 27 | @(negedge clock); 28 | reset = 1; 29 | start = 1; 30 | @(negedge clock); 31 | start = 0; 32 | wait (busy == 1'b0); 33 | 34 | $finish; 35 | end 36 | 37 | // Clock period: 1/48 MHz = 20.833 ns 38 | always #(20.833/2) clock = ~clock; 39 | 40 | ws2812_tx uut ( 41 | .clk(clock), 42 | .rst(reset), 43 | .start(start), 44 | .data(data), 45 | .bsy(busy) 46 | ); 47 | endmodule 48 | -------------------------------------------------------------------------------- /vhdl/shiftreg_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=93 shiftreg_tb.vhd 2 | -- ghdl -r --std=93 shiftreg_tb --wave=shiftreg_tb.ghw 3 | -- gtkwave shiftreg_tb.ghw 4 | 5 | library ieee; 6 | use ieee.std_logic_1164.all; 7 | 8 | entity shiftreg_tb is 9 | end entity; 10 | 11 | architecture behav of shiftreg_tb is 12 | signal clk, ser, set, out1, out2, out3, out4 : std_logic := '0'; 13 | begin 14 | dut : entity work.shiftreg 15 | port map ( 16 | clk => clk, ser => ser, set => set, 17 | out1 => out1, out2 => out2, out3 => out3, out4 => out4 18 | ); 19 | 20 | process 21 | begin 22 | set <= '1'; 23 | wait for 20 ns; 24 | set <= '0'; 25 | wait for 20 ns; 26 | 27 | for i in 1 to 5 loop 28 | clk <= '1'; 29 | wait for 20 ns; 30 | clk <= '0'; 31 | wait for 20 ns; 32 | end loop; 33 | 34 | ser <= '1'; 35 | wait for 20 ns; 36 | 37 | for i in 1 to 5 loop 38 | clk <= '1'; 39 | wait for 20 ns; 40 | clk <= '0'; 41 | wait for 20 ns; 42 | end loop; 43 | 44 | wait; 45 | end process; 46 | end architecture; 47 | -------------------------------------------------------------------------------- /vhdl/ex_6_15.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_15.vhd 2 | -- ghdl -r --std=08 ex_6_15 3 | 4 | library ieee; 5 | use ieee.std_logic_1164.all; 6 | 7 | entity ex_6_15 is 8 | end entity ex_6_15; 9 | 10 | architecture behav of ex_6_15 is 11 | function weaken(x : in std_ulogic) return std_ulogic is 12 | begin 13 | case x is 14 | when '0'|'L' => 15 | return 'L'; 16 | when '1'|'H' => 17 | return 'H'; 18 | when 'X'|'W' => 19 | return 'W'; 20 | when others => 21 | return x; 22 | end case; 23 | end function weaken; 24 | begin 25 | process is 26 | begin 27 | report "0: " & to_string(weaken('0')); 28 | report "L: " & to_string(weaken('L')); 29 | report "1: " & to_string(weaken('1')); 30 | report "H: " & to_string(weaken('H')); 31 | report "X: " & to_string(weaken('X')); 32 | report "W: " & to_string(weaken('W')); 33 | report "U: " & to_string(weaken('U')); 34 | report "Z: " & to_string(weaken('Z')); 35 | report "-: " & to_string(weaken('-')); 36 | wait; 37 | end process; 38 | end architecture behav; 39 | -------------------------------------------------------------------------------- /matlab/tree.m: -------------------------------------------------------------------------------- 1 | % Self-similar tree generated by random process 2 | 3 | clc 4 | clear 5 | close all 6 | format 7 | 8 | % Set default interpreter for xlabel, ylabel, title, ... 9 | set(0, 'defaultTextInterpreter', 'latex'); 10 | set(groot, 'defaultAxesTickLabelInterpreter', 'latex'); 11 | set(groot, 'defaultLegendInterpreter', 'latex'); 12 | 13 | % Starting point 14 | P = [0.5; 0]; 15 | % Number of iterations 16 | N = 10e4; 17 | 18 | points = zeros(N, 2); 19 | points(1,:) = P.'; 20 | 21 | for i=2:N 22 | r = rand; 23 | if r <= 0.1 24 | P = P .* [0.05; 0.6]; 25 | elseif r <= 0.2 26 | P = P .* [0.05; -0.5] + [0; 1]; 27 | elseif r <= 0.4 28 | P = [0.46 -0.15; 0.39 0.38] * P + [0; 0.6]; 29 | elseif r <= 0.6 30 | P = [0.47 -0.15; 0.17 0.42] * P + [0; 1.1]; 31 | elseif r <= 0.8 32 | P = [0.43 0.28; -0.25 0.45] * P + [0; 1.0]; 33 | else 34 | P = [0.42 0.26; -0.35 0.31] * P + [0; 0.7]; 35 | end 36 | 37 | points(i,:) = P.'; 38 | end 39 | 40 | figure 41 | scatter(points(:,1), points(:,2), 1) -------------------------------------------------------------------------------- /vhdl/ex_6_16.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_16.vhd 2 | -- ghdl -r --std=08 ex_6_16 3 | 4 | library ieee; 5 | use ieee.std_logic_1164.all; 6 | 7 | entity ex_6_16 is 8 | end entity ex_6_16; 9 | 10 | architecture behav of ex_6_16 is 11 | function valid_edge(signal x : in std_ulogic) return boolean is 12 | begin 13 | if (x'last_value = '0' or x'last_value = 'L') 14 | and (x = '1' or x = 'H') then 15 | return true; 16 | elsif (x'last_value = '1' or x'last_value = 'H') 17 | and (x = '0' or x = 'L') then 18 | return true; 19 | else 20 | return false; 21 | end if; 22 | end function valid_edge; 23 | 24 | signal x : std_ulogic := 'U'; 25 | begin 26 | process is 27 | begin 28 | x <= '1'; 29 | wait until x = '1'; 30 | report "U -> 1: " & to_string(valid_edge(x)); 31 | x <= '0'; 32 | wait until x = '0'; 33 | report "1 -> 0: " & to_string(valid_edge(x)); 34 | x <= 'L'; 35 | wait until x = 'L'; 36 | report "0 -> L: " & to_string(valid_edge(x)); 37 | x <= 'H'; 38 | wait until x = 'H'; 39 | report "L -> H: " & to_string(valid_edge(x)); 40 | wait; 41 | end process; 42 | end architecture behav; 43 | -------------------------------------------------------------------------------- /libnfc/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Matthias Thumann (m-thu@users.sourceforge.net) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | 21 | -------------------------------------------------------------------------------- /sds011/README.md: -------------------------------------------------------------------------------- 1 | SDS011 dust sensor (Raspberry Pi) 2 | ================================= 3 | 4 | `/etc/rc.local`: 5 | ```sh 6 | su pi -c '/home/pi/sds011/sds011 /dev/ttyUSB0 &' 7 | ``` 8 | 9 | `crontab`: 10 | ```sh 11 | 0 * * * * /home/pi/sds011/makegraphs && /home/pi/sds011/upload 12 | ``` 13 | 14 | [SDS011 dust sensor data](http://mthu.freeshell.org/sds011/index.html) 15 | 16 | Local webserver config (`/etc/nginx/nginx.conf`): 17 | ```nginx 18 | user www-data; 19 | worker_processes auto; 20 | pid /run/nginx.pid; 21 | 22 | events { 23 | worker_connections 768; 24 | # multi_accept on; 25 | } 26 | 27 | http { 28 | access_log /var/log/nginx/access.log; 29 | error_log /var/log/nginx/error.log; 30 | 31 | server { 32 | location / { 33 | root /home/pi/sds011/html; 34 | autoindex on; 35 | } 36 | } 37 | } 38 | ``` 39 | 40 | ### References 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /vhdl/ex_4_13_tb.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_4_13_tb.vhd 2 | -- ghdl -r --std=08 ex_4_13_tb 3 | 4 | entity ex_4_13_tb is 5 | end entity ex_4_13_tb; 6 | 7 | architecture testbench of ex_4_13_tb is 8 | signal a : bit_vector(2 downto 0); 9 | signal y : bit_vector(7 downto 0); 10 | 11 | type test_stim is record 12 | stim : bit_vector(2 downto 0); 13 | delay : time; 14 | resp : bit_vector(7 downto 0); 15 | end record test_stim; 16 | 17 | type test_stim_array is array (1 to 3) of test_stim; 18 | 19 | constant test : test_stim_array := 20 | ((stim => b"000", delay => 20 ns, resp => b"00000001"), 21 | (stim => b"001", delay => 20 ns, resp => b"00000010"), 22 | (stim => b"111", delay => 20 ns, resp => b"10000000")); 23 | begin 24 | dut : entity work.decoder(behav) 25 | port map ( a, y ); 26 | 27 | process 28 | begin 29 | for i in test'range loop 30 | report "Test vector " & to_string(i) & ":"; 31 | a <= test(i).stim; 32 | wait for test(i).delay; 33 | assert test(i).resp = y severity failure; 34 | report "a: " & to_string(a) & ", y: " & to_string(y); 35 | report "" & LF; 36 | end loop; 37 | 38 | wait; 39 | end process; 40 | end architecture testbench; 41 | -------------------------------------------------------------------------------- /vhdl/events_tb.vhd: -------------------------------------------------------------------------------- 1 | -- Testbench for events.vhd 2 | 3 | -- ghdl -a --std=08 events_tb.vhd 4 | -- ghdl -r --std=08 events_tb --wave=events_tb.ghw 5 | -- gtkwave events_tb.ghw 6 | 7 | library ieee; 8 | use ieee.std_logic_1164.all; 9 | 10 | entity events_tb is 11 | end entity; 12 | 13 | architecture behav of events_tb is 14 | constant T : time := 20 ns; -- 50 MHz 15 | signal clk, res, sig, ce, evt : std_logic := '0'; 16 | begin 17 | -- Unit under test 18 | uut : entity work.events 19 | port map (clk => clk, res => res, sig => sig, 20 | ce => ce, evt => evt); 21 | 22 | -- Clock generator 23 | process 24 | begin 25 | clk <= '0'; 26 | wait for T/2; 27 | clk <= '1'; 28 | wait for T/2; 29 | end process; 30 | 31 | -- Stimuli 32 | process 33 | begin 34 | res <= '1'; 35 | wait for 2*T; 36 | res <= '0'; 37 | 38 | wait for 2*T; 39 | sig <= '1'; 40 | wait for T; 41 | sig <= '0'; 42 | wait for T; 43 | 44 | ce <= '1'; 45 | sig <= '1'; 46 | wait for 3*T; 47 | sig <= '0'; 48 | wait for 3*T; 49 | sig <= '1'; 50 | wait for T; 51 | 52 | res <= '1'; 53 | wait for 2*T; 54 | 55 | assert false report "Testbench finished!" 56 | severity failure; 57 | end process; 58 | end architecture; 59 | -------------------------------------------------------------------------------- /matlab/poisson.m: -------------------------------------------------------------------------------- 1 | % Numerical solution of Poisson equation 2 | % Matthias Thumann, 2021 3 | 4 | clc 5 | clear 6 | close all 7 | format 8 | 9 | % ^ 10 | % | y 11 | % | 12 | % 13 | % 100 (V) 14 | % -------------------- ^ 15 | % | | | 16 | % | | | 17 | % | | L 18 | % | | 0 (V) | 19 | % | | | 20 | % | | | 21 | % -------------------- v 22 | % 0 (V) 23 | % <------- L --------> ---> x 24 | 25 | L = 30; 26 | 27 | % Initial values for potential 28 | U = zeros(L,L); 29 | 30 | % Boundary conditions 31 | U(:,L) = ones(1,L) * 100; 32 | 33 | rho = zeros(L, L); 34 | rho(10, 15) = -10; 35 | 36 | figure 37 | surf([1:L], [1:L], U) 38 | title('Initial potential with boundary conditions') 39 | 40 | for k=1:100 41 | % Iterate over all points except for the boundaries 42 | for i=2:L-1 43 | for j=2:L-1 44 | U(i,j) = 1/4 * (U(i+1, j) + U(i-1, j) + U(i, j+1) + U(i, j-1))... 45 | + pi * rho(i,j); 46 | end 47 | end 48 | end 49 | 50 | figure 51 | surf([1:L], [1:L], U) 52 | title('Potential after 100 iterations') -------------------------------------------------------------------------------- /vhdl/ex_6_13.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_13.vhd 2 | -- ghdl -r --std=08 ex_6_13 --wave=ex_6_13.ghw 3 | -- gtkwave ex_6_13.ghw 4 | 5 | library ieee; 6 | use ieee.std_logic_1164.all; 7 | 8 | entity ex_6_13 is 9 | end entity ex_6_13; 10 | 11 | architecture behav of ex_6_13 is 12 | signal clk_last_edge : time := 0 ns; 13 | 14 | procedure check_hold(signal clock, data : in std_ulogic; 15 | t_hold : in delay_length; 16 | signal last_edge : inout time) is 17 | begin 18 | if rising_edge(clock) then 19 | last_edge <= now; 20 | end if; 21 | 22 | if data'event then 23 | assert now - last_edge >= t_hold 24 | report "Hold time violation!" 25 | severity warning; 26 | end if; 27 | end procedure check_hold; 28 | 29 | signal clk, d : std_ulogic := '0'; 30 | constant HOLD_TIME : delay_length := 10 ns; 31 | begin 32 | check_hold(clock => clk, data => d, t_hold => HOLD_TIME, 33 | last_edge => clk_last_edge); 34 | 35 | process is 36 | begin 37 | wait for 5 ns; 38 | clk <= '1'; 39 | wait for 5 ns; 40 | clk <= '0'; 41 | d <= '1'; 42 | wait for 5 ns; 43 | clk <= '1'; 44 | wait for 5 ns; 45 | clk <= '0'; 46 | wait for 5 ns; 47 | wait; 48 | end process; 49 | end architecture behav; 50 | -------------------------------------------------------------------------------- /icestick/ws2812/top.v: -------------------------------------------------------------------------------- 1 | `default_nettype none 2 | `timescale 1ns / 1ps 3 | 4 | module top ( 5 | input wire clk, 6 | output wire dout 7 | ); 8 | //$ icepll -o 48 9 | // 10 | //F_PLLIN: 12.000 MHz (given) 11 | //F_PLLOUT: 48.000 MHz (requested) 12 | //F_PLLOUT: 48.000 MHz (achieved) 13 | // 14 | //FEEDBACK: SIMPLE 15 | //F_PFD: 12.000 MHz 16 | //F_VCO: 768.000 MHz 17 | // 18 | //DIVR: 0 (4'b0000) 19 | //DIVF: 63 (7'b0111111) 20 | //DIVQ: 4 (3'b100) 21 | // 22 | //FILTER_RANGE: 1 (3'b001) 23 | 24 | wire lock, clkout; 25 | 26 | SB_PLL40_CORE #( 27 | .FEEDBACK_PATH("SIMPLE"), 28 | .PLLOUT_SELECT("GENCLK"), 29 | .DIVR(4'b0000), 30 | .DIVF(7'b0111111), 31 | .DIVQ(3'b100), 32 | .FILTER_RANGE(3'b001) 33 | ) pll ( 34 | .LOCK(lock), 35 | .RESETB(1'b1), 36 | .BYPASS(1'b0), 37 | .REFERENCECLK(clk), 38 | .PLLOUTCORE(clkout) 39 | ); 40 | 41 | reg [3:0] cnt = 4'hf; 42 | reg reset = 1'b1; 43 | 44 | always @(posedge clk) begin 45 | if (lock) 46 | if (|cnt) begin 47 | cnt <= cnt - 1'b1; 48 | reset <= 1'b0; 49 | end 50 | else begin 51 | cnt <= 1'b0; 52 | reset <= 1'b1; 53 | end 54 | end 55 | 56 | ledstring leds ( 57 | .clk(clkout), 58 | .rst(reset), 59 | .dout(dout) 60 | ); 61 | endmodule 62 | -------------------------------------------------------------------------------- /icestick/README.md: -------------------------------------------------------------------------------- 1 | Lattice iCEstick 2 | ================ 3 | 4 | Install Lattice Diamond Programmer 3.0 on Ubuntu 14.04: 5 | ```sh 6 | sudo apt-get install alien 7 | sudo alien --scripts -i programmer_3_8_x64-115-3-x86_64-linux.rpm 8 | sudo cp 80-icestick.rules /etc/udev/rules.d/ 9 | ``` 10 | 11 | Start Lattice Diamond Programmer 3.0: 12 | ```sh 13 | /usr/local/programmer/3.8_x64/bin/lin64/programmer 14 | ``` 15 | 16 | Diamond settings: 17 | ``` 18 | Cable: HW-USBN-2B (FTDI) 19 | Port : FTUSB-0 20 | 21 | Family Name: iCE40 22 | Device Name: iCE40HX1K 23 | ``` 24 | 25 | Lattice resources: 26 | * [iCEstick Evaluation Kit](http://www.latticesemi.com/icestick) 27 | * [iCEstick User Manual](http://www.latticesemi.com/icestick) 28 | * [Lattice iCEcube2(tm)](http://www.latticesemi.com/Products/DesignSoftwareAndIP/FPGAandLDS/iCEcube2.aspx) 29 | * [Lattice Diamond Programmer version 3.0](http://www.latticesemi.com/Products/DesignSoftwareAndIP/ProgrammingAndConfigurationSw/Programmer.aspx) 30 | * [Lattice iCE40 LP/HX Family Datasheet](http://www.latticesemi.com/~/media/LatticeSemi/Documents/DataSheets/iCE/iCE40LPHXFamilyDataSheet.pdf) 31 | * [Lattice iCE Technology Library](http://www.latticesemi.com/~/media/LatticeSemi/Documents/TechnicalBriefs/SBTICETechnologyLibrary201504.pdf) 32 | 33 | Other resources: 34 | * [Yosys Open SYnthesis Suite](http://www.clifford.at/yosys/) 35 | * [Project IceStorm](http://www.clifford.at/icestorm/) 36 | * [Arachne-pnr](https://github.com/cseed/arachne-pnr) 37 | -------------------------------------------------------------------------------- /fortran/mandelbrot.f95: -------------------------------------------------------------------------------- 1 | ! Mandelbrot set 2 | ! Matthias Thumann, 2021 3 | 4 | program mandelbrot 5 | implicit none 6 | 7 | ! Maximum number of iterations 8 | integer, parameter :: max_iter = 23 9 | ! Maximum absolute value of z 10 | real*8, parameter :: max_z = 2 11 | ! Plot area 12 | real*8, parameter :: x_min = -2.1, x_max = 0.9, & 13 | y_min = -1.2, y_max = 1.2 14 | ! Resolution 15 | real*8, parameter :: resolution = 0.004 16 | 17 | real*8 :: x, y 18 | complex*8 :: z, c 19 | integer :: i 20 | 21 | ! Initialize PGPLOT library 22 | call pgbegin(0, '/xwin', 1, 1) 23 | call pgenv(sngl(x_min), sngl(x_max), & 24 | sngl(y_min), sngl(y_max), 0, 1) 25 | call pglab('Re(c)', 'Im(c)', 'Mandelbrot set') 26 | 27 | x = x_min 28 | do 29 | if (x > x_max) exit 30 | 31 | y = y_min 32 | do 33 | if (y > y_max) exit 34 | 35 | z = 0 36 | c = complex(x, y) 37 | do i = 1, max_iter 38 | if (abs(z) > max_z) exit 39 | z = z**2 + c 40 | enddo 41 | 42 | if (i < max_iter) then 43 | call pgsci(mod(i, 15)) ! Color 44 | call pgpt(1, sngl(x), sngl(y), -1) 45 | endif 46 | 47 | y = y + resolution 48 | enddo 49 | 50 | x = x + resolution 51 | enddo 52 | 53 | call pgend 54 | end -------------------------------------------------------------------------------- /vhdl/io_buffer.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=93 io_buffer.vhd 2 | 3 | library ieee; 4 | use ieee.std_logic_1164.all; 5 | 6 | entity io_buffer is 7 | port ( 8 | osig, clk, res : in std_logic; 9 | s : in std_logic_vector (2 downto 0); 10 | isig : out std_logic; 11 | iopad : inout std_logic 12 | ); 13 | end entity; 14 | 15 | architecture behav of io_buffer is 16 | signal odir, oreg, omux : std_logic; 17 | begin 18 | -- Concurrent signal assignments 19 | isig <= iopad; 20 | odir <= s(2) xor osig; 21 | 22 | -- D-FF 23 | process (clk, res) 24 | begin 25 | if res = '1' then 26 | oreg <= '0'; 27 | elsif rising_edge(clk) then 28 | oreg <= odir; 29 | end if; 30 | end process; 31 | 32 | -- MUX (alternative 1) 33 | omux <= oreg when s(1) = '1' 34 | else odir; 35 | 36 | -- MUX (alternative 2) 37 | --with s(1) select omux <= 38 | --oreg when '1', 39 | --odir when others; 40 | 41 | -- MUX (alternative 3) 42 | --process (s(1), oreg, odir) 43 | --begin 44 | --case s(1) is 45 | --when '1' => omux <= oreg; 46 | --when others => omux <= odir; 47 | --end case; 48 | --end process; 49 | 50 | -- Driver (alternative 1) 51 | iopad <= omux when s(0) = '1' 52 | else 'Z'; 53 | 54 | -- Driver (alternative 2) 55 | --with s(0) select iopad <= 56 | --omux when '1', 57 | --'Z' when others; 58 | 59 | -- Driver (alternative 3) 60 | --process (s(0), omux) 61 | --begin 62 | --if s(0) = '1' then 63 | --iopad <= omux; 64 | --else 65 | --iopad <= 'Z'; 66 | --end if; 67 | --end process; 68 | end architecture; 69 | -------------------------------------------------------------------------------- /matlab/euler_forward.m: -------------------------------------------------------------------------------- 1 | % Euler-forward algorithm 2 | % ======================= 3 | % 4 | % Circuit diagram: 5 | % 6 | % R L 7 | % _____ 8 | % ----|_____|-------UUUUUU---- 9 | % | | 10 | % | ^ | 11 | % | | | 12 | % | | i(t) | 13 | % | | 14 | % o ---------------------> o 15 | % ^ 16 | % u(t) = u * sin(omega * t) 17 | % 18 | % State variable: i(t) 19 | % Output : i(t) 20 | % 21 | % State space representation: 22 | % di/dt = 1/L * (u - R*i) 23 | 24 | clear 25 | close all 26 | format 27 | 28 | % Parameters 29 | R = 1; % Ohm 30 | L = 1; % Henry 31 | i0 = 0; % Initial current (A) 32 | h = 0.01; % Time step (s) 33 | t_start = 0; % Start time (s) 34 | t_end = 5; % Stop time (s) 35 | f = 5; % Frequency of the input voltage u(t) (Hz) 36 | u_amp = 10; % Amplitude of the input voltage u(t) (V) 37 | 38 | % Initialize time and current 39 | i = i0; 40 | t = t_start; 41 | 42 | % Resulting i(t) 43 | it = []; 44 | 45 | % Numerical integration using Euler forward 46 | while t <= t_end 47 | % Input voltage 48 | u = u_amp * sin(2*pi*f * t); 49 | % Time derivative of the current 50 | ip = 1/L * (u - R*i); 51 | % Integrate 52 | i = i + ip*h; 53 | % Save result 54 | it = [it i]; 55 | % Add time step 56 | t = t + h; 57 | end 58 | 59 | % Plot result 60 | figure(1); 61 | t = [0:length(it)-1] * h; 62 | plot(t, it); 63 | xlabel('t / s'); 64 | ylabel('i(t) / A'); 65 | -------------------------------------------------------------------------------- /matlab/pendulum.m: -------------------------------------------------------------------------------- 1 | % Pendulum 2 | 3 | clc 4 | clear 5 | close all 6 | format 7 | 8 | % Set default interpreter for xlabel, ylabel, title, ... 9 | set(0, 'defaultTextInterpreter', 'latex'); 10 | set(groot, 'defaultAxesTickLabelInterpreter', 'latex'); 11 | set(groot, 'defaultLegendInterpreter', 'latex'); 12 | 13 | % Parameters 14 | w0 = 1; 15 | w = 0.6; 16 | alpha = 0.0; 17 | F = 0.; 18 | 19 | % Initial conditions 20 | x0 = [0.1, 0]; 21 | 22 | t_span = [0:0.1:6*pi]; 23 | 24 | % x1 = theta 25 | % x2 = d/dt theta 26 | % 27 | % d/dt x1 = x2 28 | % d/dt x2 = -w0^2 * sin(x1) - alpha * x2 + F*cos(w*t) 29 | 30 | f = @(t, x) [x(2); ... 31 | -w0^2 * sin(x(1)) - alpha * x(2) + F * cos(w*t)]; 32 | 33 | [t, x] = ode45(f, t_span, x0); 34 | 35 | figure 36 | plot(t, x(:,1)) 37 | xlabel('$$t / \mathrm{s}$$', 'FontSize', 15); 38 | ylabel('$$\theta / \mathrm{rad}$$', 'FontSize', 15); 39 | set(gca, 'FontSize', 15) 40 | title('Time-Angle plot') 41 | 42 | N = 200; 43 | P = zeros(N, length(t_span), 2); 44 | 45 | for i=1:N 46 | % Random initial conditions 47 | x0 = [10*rand - 5, 10*rand - 5]; 48 | 49 | [t, x] = ode45(f, t_span, x0); 50 | 51 | P(i, :, 1) = x(:, 1); 52 | P(i, :, 2) = x(:, 2); 53 | end 54 | 55 | figure 56 | hold on 57 | for i=1:N 58 | plot(P(i, :, 1), P(i, :, 2)) 59 | end 60 | hold off 61 | xlabel('$$\theta / \mathrm{rad}$$', 'FontSize', 15) 62 | ylabel('$$\dot{\theta} / \mathrm{rad}\cdot\mathrm{s}^{-1}$$', 'FontSize', 15) 63 | set(gca, 'FontSize', 15) 64 | title('Phase space plot') 65 | xlim([-10 10]) 66 | ylim([-5 5]) -------------------------------------------------------------------------------- /python/newton-fractal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | @author: m-thu 5 | """ 6 | 7 | import numpy as np 8 | from numpy import sqrt 9 | import matplotlib.pyplot as plt 10 | 11 | # Newton fractal 12 | # 13 | # f(z) 14 | # z_n = z_{n-1} - -------- 15 | # f'(z) 16 | # 17 | # f(z) = z^3 - 1 18 | # f'(z) = 3 z^2 19 | # 20 | # Roots: 21 | # z_0 = 1 22 | # z_1 = -0.5 + sqrt(3)/2 i 23 | # z_2 = -0.5 - sqrt(3)/2 i 24 | 25 | def f(z): 26 | return z**3 - 1 27 | 28 | def fd(z): 29 | if z != 0.: 30 | return 3 * z**2 31 | else: 32 | return 1.e-12 33 | 34 | if __name__ == "__main__": 35 | plt.close("all") 36 | 37 | N = 500 38 | max_iter = 20 39 | eps = 1.e-9 40 | z0 = [1., -0.5+1j*sqrt(3)/2., -0.5-1j*sqrt(3)/2.] 41 | 42 | x_min = -2. 43 | x_max = 2. 44 | y_min = -2. 45 | y_max = 2. 46 | 47 | x = np.linspace(x_min, x_max, N) 48 | y = np.linspace(y_min, y_max, N) 49 | M = np.zeros((N, N)) 50 | 51 | for i in range(0, N): 52 | for j in range(0, N): 53 | z = complex(x[i], y[j]) 54 | iteration = 0 55 | 56 | while iteration < max_iter: 57 | z -= f(z)/fd(z) 58 | iteration += 1 59 | 60 | color = 1 61 | breakout = False 62 | for w in z0: 63 | if abs(z - w) < eps: 64 | M[i, j] = color 65 | breakout = True 66 | break 67 | color += 1 68 | if breakout: 69 | break 70 | 71 | plt.close("all") 72 | fig = plt.figure() 73 | ax = fig.add_subplot(111) 74 | ax.matshow(M) 75 | ax.set_xticks([]) 76 | ax.set_yticks([]) 77 | ax.set(xlabel = r"$\Re\{z\}$", ylabel = r"$\Im\{z\}$") 78 | plt.show() -------------------------------------------------------------------------------- /matlab/sierpinski.m: -------------------------------------------------------------------------------- 1 | % Sierpinski 2 | 3 | clc 4 | clear 5 | close all 6 | format 7 | 8 | % Set default interpreter for xlabel, ylabel, title, ... 9 | set(0, 'defaultTextInterpreter', 'latex'); 10 | set(groot, 'defaultAxesTickLabelInterpreter', 'latex'); 11 | set(groot, 'defaultLegendInterpreter', 'latex'); 12 | 13 | % Vertices of triangle 14 | v = [-1 0; 1 0; 0 1]; 15 | 16 | figure 17 | hold on 18 | 19 | % Triangle 20 | plot([v(1,1), v(2,1)], [v(1,2), v(2,2)]) 21 | plot([v(2,1), v(3,1)], [v(2,2), v(3,2)]) 22 | plot([v(3,1), v(1,1)], [v(3,2), v(1,2)]) 23 | 24 | % Number of points 25 | N = 10e3; 26 | 27 | p = [.2 .2]; 28 | points = zeros(N, 2); 29 | points(1,:) = p; 30 | plot(p(1), p(2), '.') 31 | 32 | for i=2:N 33 | p = (p + v(randi([1,3]),:)) ./ 2; 34 | plot(p(1), p(2), '.') 35 | points(i,:) = p; 36 | end 37 | 38 | hold off 39 | 40 | % Box counting 41 | M = 10; 42 | densities = zeros(1, M); 43 | 44 | for k=1:M 45 | N_boxes = 2^k; 46 | boxes = zeros(N_boxes, N_boxes); 47 | 48 | for i=1:N 49 | boxes(ceil((points(i,1) - v(1,1)) / (v(2,1) - v(1,1)) * N_boxes), ... 50 | ceil((points(i,2) - v(1,2)) / (v(3,2) - v(1,2)) * N_boxes)) = 1; 51 | end 52 | 53 | densities(k) = sum(boxes(:) == 1) / N_boxes^2; 54 | end 55 | 56 | figure 57 | semilogy(densities) 58 | xlabel('Log(Number of boxes)') 59 | ylabel('Density') 60 | 61 | P = polyfit([1:6], log(densities(1:6))/log(2), 1); 62 | hold on 63 | plot([1:10], 2.^(P(2) + P(1)*[1:10])) 64 | hold off 65 | 66 | legend('Measured density', 'Linear fit for first six values') 67 | 68 | display(['Boxcounting dimension: ', num2str(P(1)+2)]) -------------------------------------------------------------------------------- /mt7681/build-andes-toolchain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | TARGET=nds32le-elf 3 | PREFIX=~/${TARGET} 4 | BINUTILS=2.27 5 | GCC=6.2.0 6 | GMP=6.1.0 7 | MPC=1.0.3 8 | MPFR=3.1.4 9 | GDB=7.11.1 10 | 11 | HOME=$(pwd) 12 | mkdir -p $PREFIX 13 | cd $PREFIX 14 | 15 | wget -c http://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS}.tar.gz 16 | wget -c http://ftp.gnu.org/gnu/gcc/gcc-${GCC}/gcc-${GCC}.tar.gz 17 | wget -c http://ftp.gnu.org/gnu/gmp/gmp-${GMP}.tar.bz2 18 | wget -c http://ftp.gnu.org/gnu/mpc/mpc-${MPC}.tar.gz 19 | wget -c http://ftp.gnu.org/gnu/mpfr/mpfr-${MPFR}.tar.gz 20 | 21 | rm -rf binutils-${BINUTILS} 22 | rm -rf gcc-${GCC} 23 | rm -rf build-binutils 24 | rm -rf build-gcc 25 | rm -rf bin include lib libexec share $TARGET 26 | rm -rf gdb-${GDB} 27 | 28 | tar xzf binutils-${BINUTILS}.tar.gz 29 | 30 | mkdir build-binutils 31 | cd build-binutils 32 | ../binutils-${BINUTILS}/configure --target=${TARGET} --prefix=${PREFIX} --disable-nls --disable-werror 33 | make all -j$(nproc) 34 | make install 35 | cd .. 36 | 37 | tar xzf gcc-${GCC}.tar.gz 38 | tar xjf gmp-${GMP}.tar.bz2 39 | tar xzf mpc-${MPC}.tar.gz 40 | tar xzf mpfr-${MPFR}.tar.gz 41 | 42 | mv gmp-${GMP} gcc-${GCC}/gmp 43 | mv mpc-${MPC} gcc-${GCC}/mpc 44 | mv mpfr-${MPFR} gcc-${GCC}/mpfr 45 | 46 | mkdir build-gcc 47 | cd build-gcc 48 | export PATH=${PATH}:${PREFIX}/bin 49 | ../gcc-${GCC}/configure --target=${TARGET} --prefix=${PREFIX} --disable-nls --without-headers --enable-languages=c --disable-werror 50 | make all-gcc -j$(nproc) 51 | make all-target-libgcc -j$(nproc) 52 | make install-gcc 53 | make install-target-libgcc 54 | cd .. 55 | 56 | rm -rf build-binutils 57 | rm -rf build-gcc 58 | rm -rf binutils-${BINUTILS} 59 | rm -rf gcc-${GCC} 60 | 61 | cd $HOME 62 | -------------------------------------------------------------------------------- /nbody/nbody/Makefile: -------------------------------------------------------------------------------- 1 | # target executable 2 | TARGET:=nbody 3 | # Host C sources 4 | SRC:= 5 | # Host C++ sources 6 | SRC_CPP:= 7 | # GPU sources 8 | SRC_CUDA:=kernel.cu 9 | 10 | # OpenGL 11 | USE_OPENGL:=0 12 | 13 | # CUDA compute capability 14 | CUDA_ARCHITECTURE:=-arch=compute_11 -code=sm_11 15 | # Optimization flags 16 | NVCC_OPTFLAGS:=-use_fast_math 17 | # CUDA install paths 18 | CUDA_INSTALL_PATH:=/opt/cuda 19 | CUDA_SDK:=/opt/cudasdk 20 | 21 | # Compiler flags 22 | GCC_WARNINGS:=-Wall -Wextra -pedantic 23 | GCC_OPTFLAGS:=-Os -march=native -mtune=native -fomit-frame-pointer 24 | CFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 25 | CXXFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 26 | NVCC_FLAGS:=-O2 $(CUDA_ARCHITECTURE) $(NVCC_OPTFLAGS) 27 | 28 | # OpenGL libs 29 | ifeq ($(USE_OPENGL), 1) 30 | LIB:=-lGL -lGLU -lglut 31 | #LIB+=-lX11 -lXi -lXmu 32 | ifeq ($(shell uname -m), x86_64) 33 | LIB+=-lGLEW_x86_64 -L$(CUDA_SDK)/shared/lib/linux 34 | else 35 | LIB+=-lGLEW -L$(CUDA_SDK)/shared/lib/linux 36 | endif 37 | endif 38 | 39 | # Includes 40 | INCLUDES:=-I. -I$(CUDA_INSTALL_PATH)/include -I$(CUDA_SDK)/C/common/inc \ 41 | -I$(CUDA_SDK)/shared/inc 42 | CFLAGS+=$(INCLUDES) 43 | CXXFLAGS+=$(INCLUDES) 44 | NVCC_FLAGS+=$(INCLUDES) 45 | 46 | CC:=gcc 47 | CXX:=g++ 48 | NVCC:=$(CUDA_INSTALL_PATH)/bin/nvcc 49 | 50 | OBJ:=$(SRC:.c=.o) 51 | OBJ_CPP:=$(SRC_CPP:.cpp=.o) 52 | OBJ_CUDA:=$(SRC_CUDA:.cu=.o) 53 | 54 | # link all object files 55 | $(TARGET): $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 56 | $(NVCC) $(LIB) -o $(TARGET) $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 57 | 58 | # compile CUDA files 59 | %.o: %.cu 60 | $(NVCC) $(NVCC_FLAGS) -c -o $@ $< 61 | 62 | clean: 63 | rm -f $(TARGET) $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 64 | 65 | .PHONY: clean 66 | .SUFFIXES: .cu 67 | -------------------------------------------------------------------------------- /nbody/leapfrog/Makefile: -------------------------------------------------------------------------------- 1 | # target executable 2 | TARGET:=leapfrog 3 | # Host C sources 4 | SRC:= 5 | # Host C++ sources 6 | SRC_CPP:= 7 | # GPU sources 8 | SRC_CUDA:=kernel.cu 9 | 10 | # OpenGL 11 | USE_OPENGL:=0 12 | 13 | # CUDA compute capability 14 | CUDA_ARCHITECTURE:=-arch=compute_11 -code=sm_11 15 | # Optimization flags 16 | NVCC_OPTFLAGS:=-use_fast_math 17 | # CUDA install paths 18 | CUDA_INSTALL_PATH:=/opt/cuda 19 | CUDA_SDK:=/opt/cudasdk 20 | 21 | # Compiler flags 22 | GCC_WARNINGS:=-Wall -Wextra -pedantic 23 | GCC_OPTFLAGS:=-Os -march=native -mtune=native -fomit-frame-pointer 24 | CFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 25 | CXXFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 26 | NVCC_FLAGS:=-O2 $(CUDA_ARCHITECTURE) $(NVCC_OPTFLAGS) 27 | 28 | # OpenGL libs 29 | ifeq ($(USE_OPENGL), 1) 30 | LIB:=-lGL -lGLU -lglut 31 | #LIB+=-lX11 -lXi -lXmu 32 | ifeq ($(shell uname -m), x86_64) 33 | LIB+=-lGLEW_x86_64 -L$(CUDA_SDK)/shared/lib/linux 34 | else 35 | LIB+=-lGLEW -L$(CUDA_SDK)/shared/lib/linux 36 | endif 37 | endif 38 | 39 | # Includes 40 | INCLUDES:=-I. -I$(CUDA_INSTALL_PATH)/include -I$(CUDA_SDK)/C/common/inc \ 41 | -I$(CUDA_SDK)/shared/inc 42 | CFLAGS+=$(INCLUDES) 43 | CXXFLAGS+=$(INCLUDES) 44 | NVCC_FLAGS+=$(INCLUDES) 45 | 46 | CC:=gcc 47 | CXX:=g++ 48 | NVCC:=$(CUDA_INSTALL_PATH)/bin/nvcc 49 | 50 | OBJ:=$(SRC:.c=.o) 51 | OBJ_CPP:=$(SRC_CPP:.cpp=.o) 52 | OBJ_CUDA:=$(SRC_CUDA:.cu=.o) 53 | 54 | # link all object files 55 | $(TARGET): $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 56 | $(NVCC) $(LIB) -o $(TARGET) $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 57 | 58 | # compile CUDA files 59 | %.o: %.cu 60 | $(NVCC) $(NVCC_FLAGS) -c -o $@ $< 61 | 62 | clean: 63 | rm -f $(TARGET) $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 64 | 65 | .PHONY: clean 66 | .SUFFIXES: .cu 67 | -------------------------------------------------------------------------------- /matlab/julia.m: -------------------------------------------------------------------------------- 1 | % Julia set (Matlab/Octave) 2 | 3 | % z_{n+1} = z_n^2 + c 4 | 5 | clear 6 | close all 7 | format 8 | 9 | % Maximum absolute value of z_{n+1} 10 | s = 2; 11 | 12 | % Maximum number of iterations 13 | max_iter = 23; 14 | 15 | % Plot area (Mandelbrot set) 16 | m_x_min = -2.1; 17 | m_x_max = 0.9; 18 | m_y_min = -1.2; 19 | m_y_max = 1.2; 20 | 21 | % Plot area (Julia set) 22 | x_min = -2; 23 | x_max = 2; 24 | y_min = -2; 25 | y_max = 2; 26 | 27 | % Resolution (Mandelbrot set) 28 | m_res = 0.01; 29 | 30 | % Resolution (Julia set) 31 | res = 0.005; 32 | 33 | % Plot Mandelbrot set 34 | m_x = m_x_min:m_res:m_x_max; 35 | m_y = m_y_min:m_res:m_y_max; 36 | [c_x, c_y] = meshgrid(m_x, m_y); 37 | m_c = complex(c_x, c_y); 38 | m_z = zeros(size(m_c)); 39 | M = zeros(size(m_c)); 40 | 41 | for i = 1:max_iter 42 | m_z = m_z.^2 + m_c; 43 | M = M + (abs(m_z) <= s); 44 | end 45 | 46 | figure(1) 47 | imagesc(M) 48 | colormap jet 49 | axis off 50 | 51 | pos = get(gcf, 'Position'); 52 | msgbox('Select any point from the Mandelbrot set by clicking!'); 53 | [mouse_x, mouse_y] = ginput(1); 54 | c = m_x_min + (mouse_x/pos(1))*(m_x_max-m_x_min) ... 55 | + 1j*m_y_min + 1j*(mouse_y/pos(2))*(m_y_max-m_y_min); 56 | 57 | % Plot Julia set 58 | x = x_min:res:x_max; 59 | y = y_min:res:y_max; 60 | [z_x, z_y] = meshgrid(x, y); 61 | z = complex(z_x, z_y); 62 | J = zeros(size(z)); 63 | 64 | tic() 65 | 66 | for i = 1:max_iter 67 | z = z.^2 + c; 68 | J = J + (abs(z) <= s); 69 | end 70 | 71 | disp(['Time elapsed: ', num2str(toc()), ' s']) 72 | 73 | figure(2) 74 | imagesc(J) 75 | %colormap jet 76 | colormap([jet(); flipud(jet()); 0 0 0]) 77 | axis off 78 | title(['Julia set for c = ', num2str(c)]) 79 | -------------------------------------------------------------------------------- /nbody/opengl/Makefile: -------------------------------------------------------------------------------- 1 | # target executable 2 | TARGET:=nbody 3 | # Host C sources 4 | SRC:= 5 | # Host C++ sources 6 | SRC_CPP:= 7 | # GPU sources 8 | SRC_CUDA:=kernel.cu 9 | 10 | # OpenGL 11 | USE_OPENGL:=1 12 | 13 | # CUDA compute capability 14 | CUDA_ARCHITECTURE:=-arch=compute_11 -code=sm_11 15 | # Optimization flags 16 | NVCC_OPTFLAGS:=-use_fast_math 17 | # CUDA install paths 18 | CUDA_INSTALL_PATH:=/opt/cuda 19 | CUDA_SDK:=/opt/cudasdk 20 | 21 | # Compiler flags 22 | GCC_WARNINGS:=-Wall -Wextra -pedantic 23 | GCC_OPTFLAGS:=-Os -march=native -mtune=native -fomit-frame-pointer 24 | CFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 25 | CXXFLAGS:=$(GCC_WARNINGS) $(GCC_OPTFLAGS) -pipe 26 | NVCC_FLAGS:=-O2 $(CUDA_ARCHITECTURE) $(NVCC_OPTFLAGS) 27 | 28 | # OpenGL libs 29 | ifeq ($(USE_OPENGL), 1) 30 | LIB:=-lGL -lGLU -lglut 31 | #LIB+=-lX11 -lXi -lXmu 32 | ifeq ($(shell uname -m), x86_64) 33 | LIB+=-lGLEW_x86_64 -L$(CUDA_SDK)/shared/lib/linux 34 | else 35 | LIB+=-lGLEW -L$(CUDA_SDK)/shared/lib/linux 36 | endif 37 | endif 38 | 39 | LIB+=-lgsl -lgslcblas 40 | 41 | # Includes 42 | INCLUDES:=-I. -I$(CUDA_INSTALL_PATH)/include -I$(CUDA_SDK)/C/common/inc \ 43 | -I$(CUDA_SDK)/shared/inc 44 | CFLAGS+=$(INCLUDES) 45 | CXXFLAGS+=$(INCLUDES) 46 | NVCC_FLAGS+=$(INCLUDES) 47 | 48 | CC:=gcc 49 | CXX:=g++ 50 | NVCC:=$(CUDA_INSTALL_PATH)/bin/nvcc 51 | 52 | OBJ:=$(SRC:.c=.o) 53 | OBJ_CPP:=$(SRC_CPP:.cpp=.o) 54 | OBJ_CUDA:=$(SRC_CUDA:.cu=.o) 55 | 56 | # link all object files 57 | $(TARGET): $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 58 | $(NVCC) $(LIB) -o $(TARGET) $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 59 | 60 | # compile CUDA files 61 | %.o: %.cu 62 | $(NVCC) $(NVCC_FLAGS) -c -o $@ $< 63 | 64 | clean: 65 | rm -f $(TARGET) $(OBJ) $(OBJ_CPP) $(OBJ_CUDA) 66 | 67 | .PHONY: clean 68 | .SUFFIXES: .cu 69 | -------------------------------------------------------------------------------- /mt7681/README.md: -------------------------------------------------------------------------------- 1 | MediaTek MT7681 2 | =============== 3 | 4 | ## Hardware 5 | Module pinout (top view): 6 | ``` 7 | __________________________ 8 | RST |o o| GND 9 | | | 10 | GPIO0 |o -------- o| FL_CLK 11 | | | MT | | 12 | GPIO1 |o |.7681 | o| FL_CS 13 | | -------- | 14 | GPIO2 |o o| FL_MOSI 15 | | | 16 | GPIO3 |o o| FL_MISO 17 | | UFL ----------- | 18 | GPIO4 |o | | Winbond.| o| 3.3V_2 \ 19 | | v | 25x40 | | - not connected 20 | TXD |o --- ----------- o| 3.3V_1 / together 21 | | |O| | 22 | RXD |o --- o| GND 23 | -------------------------- 24 | 25 | 3.3V_1: Flash 26 | 3.3V_2: Microcontroller 27 | ``` 28 | 29 | ## Software 30 | 31 | ## References 32 | [MediaTek MT7681](http://labs.mediatek.com/site/global/developer_tools/mediatek_7681/whatis_7681/index.gsp) 33 | 34 | [MediaTek MT7681 Datasheet](https://labs.mediatek.com/fileMedia/download/1231ba72-bdb8-4b7c-bd3c-e9990d4fe1e1) 35 | 36 | [MediaTek LinkIt(TM) Connect 7681 Developer's Guide](http://labs.mediatek.com/fileMedia/download/60b77480-f08e-46de-b4ab-513916dcff75) 37 | 38 | [MediaTek LinkIt(TM) Connect 7681 API Reference](http://labs.mediatek.com/fileMedia/download/5a44333c-f56a-47e6-ad03-9acfa33c9561) 39 | 40 | [AndeStar ISA Manual, v1.9, 2015/06/12](http://www.andestech.com/en/products/documentation.htm) 41 | 42 | [Andes Programming Guide for ISA V3, v1.2, 2015/07/30](http://www.andestech.com/en/products/documentation.htm) 43 | 44 | [Winbond W25x40 Flash Datasheet](http://pdf1.alldatasheet.com/datasheet-pdf/view/197512/WINBOND/W25X40.html) 45 | -------------------------------------------------------------------------------- /PODCASTS.md: -------------------------------------------------------------------------------- 1 | Podcasts 2 | ======== 3 | 4 | Still active 5 | ------------ 6 | 7 | * [The Amp Hour](https://theamphour.com/) 8 | * [omega tau](https://omegataupodcast.net/) 9 | * [Unnamed Reverse Engineering Podcast (UREP)](https://unnamedre.com/) 10 | * [Binärgewitter](http://blog.binaergewitter.de/) 11 | * [Pentaradio 24](https://www.c3d2.de/radio.html) 12 | * [Pentacast](https://www.c3d2.de/podcast.html) 13 | * [Robotiklabor](https://robotiklabor.de/) 14 | * [Come Fly With Us](https://www.comeflywithus.de/) 15 | * [Airline Pilot Guy ](http://airlinepilotguy.com/) 16 | * [Fingers elektrische Welt (Podcast)](https://www.fingers-welt.de/podcast/podcast.htm), 17 | * [Lex Fridman Podcast](https://lexfridman.com/podcast/) 18 | * [Welt der Werkstoffe - talk](https://anchor.fm/martin-bonnet), 19 | 20 | Inactive 21 | -------- 22 | 23 | * [The Spark Gap Podcast](http://thesparkgap.net/) 24 | * [KonScience](http://www.konscience.de/) 25 | * [Chaosradio München](http://radio.muc.ccc.de/) 26 | * [Software Defined Radio](https://sdr.entropia.de/) 27 | * [/dev/radio](https://ulm.ccc.de/dev/radio/) 28 | * [Mandelbrot Talks](http://mandelbrot-talks.de/) 29 | * [The Titanium Physicists Podcast](http://titaniumphysicists.brachiolopemedia.com/) 30 | * [The Contextual Electronics Podcast](https://contextualelectronics.com/category/podcast/) 31 | * 32 | 33 | Removed 34 | ------- 35 | * [Nature Podcast](https://www.nature.com/nature/articles?type=nature-podcast) 36 | * [Science Podcasts](https://www.sciencemag.org/podcasts) 37 | * [Daniel and Jorge Explain the Universe](https://www.iheart.com/podcast/105-daniel-and-jorge-explain-t-29862087/) 38 | * [CLEANELECTRIC](https://www.cleanelectric.de/) 39 | -------------------------------------------------------------------------------- /matlab/wave.m: -------------------------------------------------------------------------------- 1 | % Numerical solution of wave equation 2 | % Matthias Thumann, 2021 3 | 4 | clc 5 | clear 6 | close all 7 | format 8 | 9 | % Parameters 10 | L = 1; 11 | c = 1; 12 | N_x = 100; 13 | N_t = 250; 14 | 15 | Delta_x = L / N_x; 16 | Delta_t = 2 / N_t; 17 | 18 | % Solution y(x,t) 19 | % Boundary condition y(t, x=0) = x(t, x=L) = 0 20 | y = zeros(N_x, N_t); 21 | 22 | % Initial condition for x 23 | for i=0:N_x-1 24 | x = i * Delta_x; 25 | if x <= 4/5*L 26 | y(i+1, 1) = 5/4 * x / L; 27 | else 28 | y(i+1, 1) = 5 - 5 * x / L; 29 | end 30 | end 31 | y(N_x,1) = 0; 32 | 33 | for j=1:N_t-1 34 | for i=2:N_x-1 35 | if j == 1 36 | y(i,j+1) = 2*y(i,j) - y(i,1) ... 37 | + c^2 / ((Delta_x / Delta_t)^2) ... 38 | * (y(i-1,j) + y(i+1,j) - 2*y(i,j)); 39 | else 40 | y(i,j+1) = 2*y(i,j) - y(i,j-1) ... 41 | + c^2 / ((Delta_x / Delta_t)^2) ... 42 | * (y(i-1,j) + y(i+1,j) - 2*y(i,j)); 43 | end 44 | end 45 | end 46 | 47 | figure(1) 48 | 49 | for i=0:9 50 | subplot(5,2,i+1) 51 | plot(y(:,i*25+1)) 52 | ylim([-1 1]) 53 | xlabel('x') 54 | ylabel('y(x,t)') 55 | title(strcat('Time step: ', num2str(i*25))) 56 | end 57 | 58 | % figure 59 | % vid = VideoWriter('wave', 'MPEG-4'); 60 | % set(vid, 'FrameRate', 10); 61 | % vid.Quality = 100; 62 | % open(vid) 63 | % for i=1:N_t 64 | % plot(y(:,i)); 65 | % ylim([-1 1]) 66 | % frame = getframe; 67 | % writeVideo(vid, frame); 68 | % end 69 | % close(vid) 70 | 71 | figure(2) 72 | for i=1:N_t 73 | plot(y(:,i)); 74 | xlabel('x') 75 | ylabel(strcat('y(x, t = ', num2str((i-1)*Delta_t, '%1.2f'), ')')) 76 | ylim([-1 1]) 77 | pause(0.1) 78 | end -------------------------------------------------------------------------------- /vhdl/ex_6_12.vhd: -------------------------------------------------------------------------------- 1 | -- ghdl -a --std=08 ex_6_12.vhd 2 | -- ghdl -r --std=08 ex_6_12 3 | 4 | entity ex_6_12 is 5 | end entity ex_6_12; 6 | 7 | architecture behav of ex_6_12 is 8 | subtype size_type is integer range 1 to 8; 9 | 10 | procedure align_address(addr : out bit_vector; 11 | size : in size_type := 4) is 12 | begin 13 | assert size = 1 or size = 2 or size = 4 or size = 8 14 | severity failure; 15 | assert addr'ascending = false 16 | severity failure; 17 | 18 | case size is 19 | when 1 => 20 | null; 21 | when 2 => 22 | addr(addr'right) := '0'; 23 | when 4 => 24 | --addr(addr'right) := '0'; 25 | --addr(addr'right+1) := '0'; 26 | addr(addr'right+1 downto addr'right) := "00"; 27 | when 8 => 28 | --addr(addr'right) := '0'; 29 | --addr(addr'right+1) := '0'; 30 | --addr(addr'right+2) := '0'; 31 | addr(addr'right+2 downto addr'right) := "000"; 32 | when others => 33 | null; 34 | end case; 35 | end procedure align_address; 36 | begin 37 | process is 38 | variable address : bit_vector(7 downto 0) := (others => '1'); 39 | begin 40 | align_address(addr => address, size => 1); 41 | report "address = " & to_string(address) & ", size = 1"; 42 | align_address(address, 2); 43 | report "address = " & to_string(address) & ", size = 2"; 44 | align_address(address); 45 | report "address = " & to_string(address) & ", size = 4"; 46 | address := (others => '1'); 47 | align_address(address, 4); 48 | report "address = " & to_string(address) & ", size = 4"; 49 | address := (others => '1'); 50 | align_address(address, open); 51 | report "address = " & to_string(address) & ", size = 4"; 52 | align_address(address, 8); 53 | report "address = " & to_string(address) & ", size = 8"; 54 | wait; 55 | end process; 56 | end architecture behav; 57 | -------------------------------------------------------------------------------- /socketcan/sender.c: -------------------------------------------------------------------------------- 1 | // https://www.kernel.org/doc/Documentation/networking/can.txt 2 | 3 | // Introduction to the Controller Area Network (CAN): 4 | // http://www.ti.com/lit/an/sloa101a/sloa101a.pdf 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | int main(int argc, char *argv[]) 20 | { 21 | int s = 0; 22 | int retval = EXIT_SUCCESS; 23 | struct sockaddr_can addr; 24 | struct ifreq ifr; 25 | 26 | if (argc != 2) { 27 | fprintf(stderr, "usage: %s canX\n", argv[0]); 28 | retval = EXIT_FAILURE; 29 | goto exit; 30 | } 31 | 32 | if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { 33 | perror("socker"); 34 | retval = EXIT_FAILURE; 35 | goto exit; 36 | } 37 | 38 | memset(&ifr, 0, sizeof ifr); 39 | strncpy(ifr.ifr_name, argv[1], sizeof ifr.ifr_name); 40 | if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { 41 | perror("ioctl"); 42 | retval = EXIT_FAILURE; 43 | goto exit; 44 | } 45 | 46 | addr.can_family = AF_CAN; 47 | addr.can_ifindex = ifr.ifr_ifindex; 48 | 49 | if (bind(s, (struct sockaddr *)&addr, sizeof addr) < 0) { 50 | perror("bind"); 51 | retval = EXIT_FAILURE; 52 | goto exit; 53 | } 54 | printf("[ Interface: %s ]\n", argv[1]); 55 | 56 | struct can_frame msg = { 57 | .can_id = 0x01, 58 | .can_dlc = 5, 59 | .data = {'H','e','l','l','o'} 60 | }; 61 | 62 | for (int i = 0; i < 10; ++i) { 63 | printf("[ Sending message ... ]\n"); 64 | if (write(s, &msg, sizeof msg) < 0) { 65 | perror("write"); 66 | retval = EXIT_FAILURE; 67 | goto exit; 68 | } 69 | 70 | sleep(5); 71 | } 72 | 73 | exit: 74 | if (s) { 75 | if (close(s) < 0) 76 | perror("close"); 77 | } 78 | return retval; 79 | } 80 | -------------------------------------------------------------------------------- /matsq.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | /* 6 | 7 | ! 8 | ( a b )^2 = ( aa bb ) 9 | ( c d ) ( cc dd ) 10 | 11 | ( a b ) * ( a b ) = ( a*a + b*c a*b + b*d ) 12 | ( c d ) ( c d ) ( c*a + d*c c*b + d*d ) 13 | 14 | */ 15 | 16 | int main() 17 | { 18 | uint64_t a, b, c, d; 19 | uint64_t x11, x12, x21, x22; 20 | 21 | for (a = 0; a < 10; ++a) 22 | for (b = 0; b < 10; ++b) 23 | for (c = 0; c < 10; ++c) 24 | for (d = 0; d < 10; ++d) { 25 | x11 = a*a + b*c; 26 | x12 = a*b + b*d; 27 | x21 = c*a + d*c; 28 | x22 = c*b + d*d; 29 | 30 | if ((a*11 == x11) && 31 | (b*11 == x12) && 32 | (c*11 == x21) && 33 | (d*11 == x22)) { 34 | 35 | printf(" ( %"PRIu64" %"PRIu64" )\n", a, b); 36 | printf(" ( %"PRIu64" %"PRIu64" )\n\n", c, d); 37 | } 38 | } 39 | 40 | return 0; 41 | } 42 | 43 | /* 44 | gcc -Wall -std=c99 -pedantic -Wextra -O3 matsq.c 45 | ./a.out 46 | 47 | ( 0 0 ) 48 | ( 0 0 ) 49 | 50 | ( 2 2 ) 51 | ( 9 9 ) 52 | 53 | ( 2 3 ) 54 | ( 6 9 ) 55 | 56 | ( 2 6 ) 57 | ( 3 9 ) 58 | 59 | ( 2 9 ) 60 | ( 2 9 ) 61 | 62 | ( 3 3 ) 63 | ( 8 8 ) 64 | 65 | ( 3 4 ) 66 | ( 6 8 ) 67 | 68 | ( 3 6 ) 69 | ( 4 8 ) 70 | 71 | ( 3 8 ) 72 | ( 3 8 ) 73 | 74 | ( 4 4 ) 75 | ( 7 7 ) 76 | 77 | ( 4 7 ) 78 | ( 4 7 ) 79 | 80 | ( 5 5 ) 81 | ( 6 6 ) 82 | 83 | ( 5 6 ) 84 | ( 5 6 ) 85 | 86 | ( 6 5 ) 87 | ( 6 5 ) 88 | 89 | ( 6 6 ) 90 | ( 5 5 ) 91 | 92 | ( 7 4 ) 93 | ( 7 4 ) 94 | 95 | ( 7 7 ) 96 | ( 4 4 ) 97 | 98 | ( 8 3 ) 99 | ( 8 3 ) 100 | 101 | ( 8 4 ) 102 | ( 6 3 ) 103 | 104 | ( 8 6 ) 105 | ( 4 3 ) 106 | 107 | ( 8 8 ) 108 | ( 3 3 ) 109 | 110 | ( 9 2 ) 111 | ( 9 2 ) 112 | 113 | ( 9 3 ) 114 | ( 6 2 ) 115 | 116 | ( 9 6 ) 117 | ( 3 2 ) 118 | 119 | ( 9 9 ) 120 | ( 2 2 ) 121 | */ 122 | -------------------------------------------------------------------------------- /matlab/conway.m: -------------------------------------------------------------------------------- 1 | % Conway's game of life 2 | % Matthias Thumann, 2021 3 | 4 | clc 5 | clear 6 | close all 7 | format 8 | 9 | %%%%%%%%%%%%% 10 | % Example 1 % 11 | %%%%%%%%%%%%% 12 | N = 30; 13 | cells = zeros(N, N); 14 | cells(14,13) = 1; 15 | cells(14,14) = 1; 16 | cells(14,15) = 1; 17 | cells(13,14) = 1; 18 | cells(15,14) = 1; 19 | 20 | %%%%%%%%%%%%% 21 | % Example 2 % 22 | %%%%%%%%%%%%% 23 | % N = 40; 24 | % cells = zeros(N, N); 25 | % cells(2:N-1, 2:N-1) = randi([0 1], N-2); 26 | 27 | %%%%%%%%%%%%% 28 | % Example 3 % 29 | %%%%%%%%%%%%% 30 | % N = 40; 31 | % cells = zeros(N, N); 32 | % for i=2:N-1 33 | % for j=2:N-1 34 | % if rand < 0.1 35 | % cells(i,j) = 1; 36 | % end 37 | % end 38 | % end 39 | 40 | figure 41 | while true 42 | next_cells = cells; 43 | imagesc(cells) 44 | pause(0.3) 45 | 46 | % Boundary condition: all edges are zero 47 | for i=2:N-1 48 | for j=2:N-1 49 | % Each cell has eight neighbors 50 | neighbors = sum(sum(cells(i-1:i+1, j-1:j+1))) - cells(i, j); 51 | 52 | % Cell alive 53 | if cells(i,j) == 1 54 | % Cell alive, only one neighbor alive 55 | % -> die 56 | if neighbors < 2 57 | next_cells(i,j) = 0; 58 | % Cell alive, two or three neighbors alive 59 | % -> stay alive 60 | 61 | % Cell alive, > three neighbors alive 62 | % -> die 63 | elseif neighbors > 3 64 | next_cells(i,j) = 0; 65 | end 66 | % Cell dead 67 | else 68 | % Cell dead, 3 neighbors alive 69 | % -> revive 70 | if neighbors == 3 71 | next_cells(i,j) = 1; 72 | end 73 | end 74 | end 75 | end 76 | 77 | % If nothing changed, stop 78 | if next_cells == cells 79 | break 80 | end 81 | 82 | cells = next_cells; 83 | end -------------------------------------------------------------------------------- /socketcan/receiver.c: -------------------------------------------------------------------------------- 1 | // https://www.kernel.org/doc/Documentation/networking/can.txt 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int main(int argc, char *argv[]) 18 | { 19 | int s = 0; 20 | int retval = EXIT_SUCCESS; 21 | struct sockaddr_can addr; 22 | struct ifreq ifr; 23 | 24 | if (argc != 2) { 25 | fprintf(stderr, "usage: %s canX\n", argv[0]); 26 | retval = EXIT_FAILURE; 27 | goto exit; 28 | } 29 | 30 | if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) { 31 | perror("socker"); 32 | retval = EXIT_FAILURE; 33 | goto exit; 34 | } 35 | 36 | memset(&ifr, 0, sizeof ifr); 37 | strncpy(ifr.ifr_name, argv[1], sizeof ifr.ifr_name); 38 | if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { 39 | perror("ioctl"); 40 | retval = EXIT_FAILURE; 41 | goto exit; 42 | } 43 | 44 | addr.can_family = AF_CAN; 45 | addr.can_ifindex = ifr.ifr_ifindex; 46 | 47 | if (bind(s, (struct sockaddr *)&addr, sizeof addr) < 0) { 48 | perror("bind"); 49 | retval = EXIT_FAILURE; 50 | goto exit; 51 | } 52 | printf("[ Interface: %s ]\n", argv[1]); 53 | 54 | uint8_t buf[sizeof(struct can_frame)]; 55 | ssize_t len; 56 | 57 | for (;;) { 58 | if ((len = read(s, &buf, sizeof buf)) < 0) { 59 | perror("read"); 60 | retval = EXIT_FAILURE; 61 | goto exit; 62 | } 63 | 64 | if ((size_t)len < sizeof(struct can_frame)) { 65 | printf("[ Received invalid CAN frame ]\n"); 66 | continue; 67 | } 68 | 69 | struct can_frame *msg = (struct can_frame *)buf; 70 | printf("[ Received CAN frame, ID: %04x, LEN: %02x, ", 71 | msg->can_id, msg->can_dlc); 72 | printf("PAYLOAD: "); 73 | for (uint8_t i = 0; i < msg->can_dlc; ++i) { 74 | if (isprint(msg->data[i])) 75 | putchar(msg->data[i]); 76 | } 77 | printf(" ]\n"); 78 | } 79 | 80 | exit: 81 | if (s) { 82 | if (close(s) < 0) 83 | perror("close"); 84 | } 85 | return retval; 86 | } 87 | -------------------------------------------------------------------------------- /rdm6300/README.md: -------------------------------------------------------------------------------- 1 | RDM6300 125 kHz RFID reader module 2 | ================================== 3 | 4 | ## Hardware 5 | Top view: 6 | ``` 7 | --------------- +5V (in) 8 | ----------- GND | ------------ GND 9 | | -------- Pin 3 | | 10 | | | ----- Pin 4 | | ------ RX 11 | | | | -- Pin 5 | | | --- TX (9600,N,8,1) 12 | | | | | | | | | 3.3V 13 | v v v v v v v v 14 | _______________________________________ 15 | | o o o o P1 o o o o o | 16 | | 1 2 3 4 5 4 3 2 1 | 17 | | (unpopulated) | 18 | | | 19 | | | 20 | | 1 2 3 2 1 | 21 | | o o P2 P3 o o o | 22 | --------------------------------------- 23 | ^ ^ ^ ^ ^ 24 | | | | | | 25 | | --- Antenna 2 | | --- LED 26 | ------ Antenna 1 | ------ +5V (out) 27 | --------- GND 28 | 29 | Silicon Labs F330: 30 | ------------------ 31 | Pin 3: VDD (+3.3V) 32 | Pin 4: /RST, C2CK 33 | Pin 5: P2.0, C2D 34 | ``` 35 | 36 | UART output format: 37 | ``` 38 | Tag number: 0x12 0x34 0x56 0x78 0x9a 39 | || 40 | _______|| . . . . 41 | ASCII | _____| 42 | | | 43 | v v 44 | 0x02 | A B C D E F G H I J | A B | 0x03 45 | ^ ^ 46 | | | 47 | ASCII ----| 48 | || 49 | || 50 | Checksum: 0x12 XOR 0x34 ... XOR 0x9a = 0xXX 51 | ``` 52 | 53 | Parts: 54 | * TI LM358: [LMx58-N Low-Power, Dual-Operational Amplifiers](http://www.ti.com/lit/ds/symlink/lm158-n.pdf) 55 | * Silicon Labs F330: [Silicon Labs C8051F330/1/2/3/4/5](https://www.silabs.com/Support%20Documents/TechnicalDocs/C8051F33x.pdf) 56 | 57 | ## Links 58 | [EM4100 RFID Cloner Kit](https://github.com/kbembedded/EM4100_Cloner) 59 | -------------------------------------------------------------------------------- /matlab/ising/ising.m: -------------------------------------------------------------------------------- 1 | % One dimesional Ising model 2 | 3 | clc 4 | clear 5 | close all 6 | format 7 | 8 | rng('shuffle', 'Twister') 9 | 10 | % Number of spins 11 | N = 50; 12 | % Number of time steps 13 | n = 200; 14 | % Temperature 15 | kT = 0.1; 16 | % External magnetic field 17 | B = 0.; 18 | 19 | % Generate random configuration 20 | S = randi([0,1], 1, N); 21 | S(S == 0) = -1.; 22 | 23 | % Matrix with results 24 | Result = zeros(N, n); 25 | E_result = zeros(1, n); 26 | 27 | for i=1:n 28 | % Generate trial configuration by flipping one spin 29 | S_trial = S; 30 | k = randi([1,N]); 31 | S_trial(k) = -S_trial(k); 32 | 33 | % Energy of new and old configuration 34 | E = energy(S, B); 35 | E_result(i) = E; 36 | E_trial = energy(S_trial, B); 37 | Delta_E = E_trial - E; 38 | 39 | % Probability of accepting new configuration 40 | p = exp(-Delta_E / kT); 41 | 42 | if rand <= p 43 | Result(:,i) = S_trial.'; 44 | S = S_trial; 45 | else 46 | Result(:,i) = S.'; 47 | end 48 | end 49 | 50 | imagesc(Result) 51 | xlabel('Time step') 52 | ylabel('Spin orientation') 53 | 54 | figure 55 | plot(E_result) 56 | xlabel('Time step') 57 | ylabel('Energy') 58 | 59 | % Calculate histogram of resulting energies 60 | 61 | tries = 1000; 62 | E_hist = zeros(1, tries); 63 | 64 | for j = 1:tries 65 | % Generate random configuration 66 | S = randi([0,1], 1, N); 67 | S(S == 0) = -1.; 68 | 69 | for i=1:n 70 | % Generate trial configuration by flipping one spin 71 | S_trial = S; 72 | k = randi([1,N]); 73 | S_trial(k) = -S_trial(k); 74 | 75 | % Energy of new and old configuration 76 | E = energy(S, B); 77 | E_trial = energy(S_trial, B); 78 | Delta_E = E_trial - E; 79 | 80 | % Probability of accepting new configuration 81 | p = exp(-Delta_E / kT); 82 | 83 | if rand <= p 84 | S = S_trial; 85 | end 86 | end 87 | 88 | E_hist(j) = energy(S, B); 89 | end 90 | 91 | figure 92 | histogram(E_hist) 93 | xlabel('Energy') 94 | ylabel('# Occurrences') -------------------------------------------------------------------------------- /matlab/Quaternion.m: -------------------------------------------------------------------------------- 1 | % Quaternions (Matlab/Octave) 2 | 3 | classdef Quaternion 4 | properties 5 | % Q = a + b i + c j + d k 6 | Q = [0 0 0 0]; 7 | end 8 | 9 | properties (Dependent) 10 | Norm 11 | Conjugate 12 | end 13 | 14 | methods 15 | % Constructor 16 | function obj = Quaternion(q) 17 | if nargin > 0 18 | if isa(q, 'Quaternion') 19 | obj.Q = q.Q; 20 | else 21 | if (length(q) >=1) && (length(q) <= 4) 22 | obj.Q = [q, zeros(1, 4-length(q))]; 23 | else 24 | error('Invalid length'); 25 | end 26 | end 27 | end 28 | end 29 | 30 | % Override disp function 31 | function disp(obj) 32 | disp([num2str(obj.Q(1)), ' + ', ... 33 | num2str(obj.Q(2)), 'i + ', ... 34 | num2str(obj.Q(3)), 'j + ', ... 35 | num2str(obj.Q(4)), 'k']) 36 | end 37 | 38 | % Getter for Norm property 39 | function val = get.Norm(obj) 40 | val = sqrt(obj.Q(1)^2 + obj.Q(2)^2 + obj.Q(3)^2 ... 41 | + obj.Q(4)^2); 42 | end 43 | 44 | % Getter for Conjugate property 45 | function q = get.Conjugate(obj) 46 | q = Quaternion([obj.Q(1), -obj.Q(2), -obj.Q(3), ... 47 | -obj.Q(4)]); 48 | end 49 | 50 | % Override binary plus 51 | function q = plus(q1, q2) 52 | q = Quaternion([q1.Q(1) + q2.Q(1), ... 53 | q1.Q(2) + q2.Q(2), ... 54 | q1.Q(3) + q2.Q(3), ... 55 | q1.Q(4) + q2.Q(4)]); 56 | end 57 | 58 | % Override binary minus 59 | function q = minus(q1, q2) 60 | q = Quaternion([q1.Q(1) - q2.Q(1), ... 61 | q1.Q(2) - q2.Q(2), ... 62 | q1.Q(3) - q2.Q(3), ... 63 | q1.Q(4) - q2.Q(4)]); 64 | end 65 | 66 | % Override unary plus 67 | function q = uplus(q1) 68 | q = Quaternion([q1.Q(1), q1.Q(2), q1.Q(3), ... 69 | q1.Q(4)]); 70 | end 71 | 72 | % Override unary minus 73 | function q = uminus(q1) 74 | q = Quaternion([-q1.Q(1), -q1.Q(2), -q1.Q(3), ... 75 | -q1.Q(4)]); 76 | end 77 | 78 | % Override multiplication 79 | function q = mtimes(q1, q2) 80 | q = Quaternion( ... 81 | [q1.Q(1)*q2.Q(1)-q1.Q(2)*q2.Q(2)-q1.Q(3)*q2.Q(3)-q1.Q(4)*q2.Q(4), 82 | q1.Q(1)*q2.Q(2)+q1.Q(2)*q2.Q(1)+q1.Q(3)*q2.Q(4)-q1.Q(4)*q2.Q(3), 83 | q1.Q(1)*q2.Q(3)-q1.Q(2)*q2.Q(4)+q1.Q(3)*q2.Q(1)+q1.Q(4)*q2.Q(2), 84 | q1.Q(1)*q2.Q(4)+q1.Q(2)*q2.Q(3)-q1.Q(3)*q2.Q(2)+q1.Q(4)*q2.Q(1)]); 85 | end 86 | end 87 | end 88 | -------------------------------------------------------------------------------- /coq/Induction.v: -------------------------------------------------------------------------------- 1 | Theorem plus_n_0 : forall n : nat, 2 | n = n + 0. 3 | 4 | Proof. 5 | induction n as [| n' IHn']. 6 | - reflexivity. 7 | - simpl. rewrite <- IHn'. reflexivity. 8 | Qed. 9 | 10 | (*******************) 11 | (* basic_induction *) 12 | (*******************) 13 | 14 | Theorem mult_0_r : forall n : nat, 15 | n * 0 = 0. 16 | 17 | Proof. 18 | induction n as [| n' IHn']. 19 | - reflexivity. 20 | - simpl. rewrite -> IHn'. reflexivity. 21 | Qed. 22 | 23 | Theorem plus_n_Sm : forall n m : nat, 24 | S (n + m) = n + (S m). 25 | 26 | Proof. 27 | intros n m. 28 | induction n as [| n' IHn']. 29 | - reflexivity. 30 | - simpl. rewrite -> IHn'. reflexivity. 31 | Qed. 32 | 33 | Theorem plus_comm : forall n m : nat, 34 | n + m = m + n. 35 | 36 | Proof. 37 | intros n m. 38 | induction n as [| n' IHn']. 39 | - simpl. rewrite <- plus_n_0. reflexivity. 40 | - simpl. rewrite <- plus_n_Sm. rewrite -> IHn'. reflexivity. 41 | Qed. 42 | 43 | Theorem plus_assoc : forall n m p : nat, 44 | n + (m + p) = (n + m) + p. 45 | 46 | Proof. 47 | intros n m p. 48 | induction n as [| n' IHn']. 49 | - reflexivity. 50 | - simpl. rewrite -> IHn'. reflexivity. 51 | Qed. 52 | 53 | (***************) 54 | (* double_plus *) 55 | (***************) 56 | 57 | Fixpoint double (n : nat) : nat := 58 | match n with 59 | | O => O 60 | | S n' => S (S (double n')) 61 | end. 62 | 63 | Lemma double_plus : forall n : nat, 64 | double n = n + n. 65 | 66 | Proof. 67 | induction n as [| n' IHn']. 68 | - reflexivity. 69 | - simpl. rewrite -> IHn'. rewrite -> plus_n_Sm. reflexivity. 70 | Qed. 71 | 72 | (***********) 73 | (* evenb_S *) 74 | (***********) 75 | 76 | Fixpoint evenb (n : nat) : bool := 77 | match n with 78 | | O => true 79 | | S O => false 80 | | S (S n') => evenb n' 81 | end. 82 | 83 | Lemma evenb_lemma : forall n : nat, 84 | evenb (S (S n)) = evenb n. 85 | 86 | Proof. 87 | destruct n. 88 | - reflexivity. 89 | - reflexivity. 90 | Qed. 91 | 92 | Lemma negb_lemma : forall b : bool, 93 | negb (negb b) = b. 94 | 95 | Proof. 96 | destruct b. 97 | - reflexivity. 98 | - reflexivity. 99 | Qed. 100 | 101 | Theorem evenb_S : forall n : nat, 102 | evenb (S n) = negb (evenb n). 103 | 104 | Proof. 105 | induction n as [| n' IHn']. 106 | - reflexivity. 107 | - rewrite -> evenb_lemma. rewrite -> IHn'. 108 | rewrite -> negb_lemma. reflexivity. 109 | Qed. -------------------------------------------------------------------------------- /python/einstein-solid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | @author: m-thu 5 | """ 6 | 7 | import numpy as np 8 | from numpy import exp, pi, sqrt, log 9 | import matplotlib.pyplot as plt 10 | import scipy as sp 11 | import scipy.special 12 | 13 | # N: number of oscillators 14 | # q: units of energy 15 | # 16 | # Multiplicity: 17 | # 18 | # (q+N-1) (q+N-1)! 19 | # Omega(N, q) = ( ) = ----------- 20 | # ( q ) q! (N-1)! 21 | 22 | def mult(N, q): 23 | return sp.special.comb(q+N-1, q, exact = True) 24 | 25 | 26 | # q_total: total energy 27 | # NA : number of oscillators in A 28 | # NB : number of oscillators in B 29 | 30 | def macrostate(q_total, NA, NB): 31 | print("qA\tOmegaA\tqB\tOmegaB\tOmegaTotal") 32 | print("-"*50, end = "\n") 33 | 34 | OmegaSum = 0 35 | x = [] 36 | y = [] 37 | S_A = [] 38 | S_B = [] 39 | S_total = [] 40 | 41 | for qA in range(q_total+1): 42 | qB = q_total - qA 43 | OmegaA = mult(NA, qA) 44 | OmegaB = mult(NB, qB) 45 | OmegaTotal = OmegaA * OmegaB 46 | OmegaSum += OmegaTotal 47 | 48 | print(qA, OmegaA, qB, OmegaB, OmegaTotal, sep = "\t") 49 | 50 | x.append(qA) 51 | y.append(OmegaTotal) 52 | 53 | # Entropy: 54 | # S_A = k_B * ln(OmegaA) 55 | # S_B = k_B * ln(OmegaB) 56 | # S_total = S_A + S_B 57 | 58 | # 1 dS 59 | # - = -- 60 | # T dU 61 | 62 | S_A.append(log(float(OmegaA))) 63 | S_B.append(log(float(OmegaB))) 64 | S_total.append(log(float(OmegaA)) + log(float(OmegaB))) 65 | 66 | print("\nTotal number of microstates:", OmegaSum, end = "\n\n") 67 | return x, y, S_A, S_B, S_total 68 | 69 | if __name__ == "__main__": 70 | plt.close("all") 71 | 72 | x, y, S_A, S_B, S_total = macrostate(6, 3, 3) 73 | fig = plt.figure() 74 | fig.suptitle("NA = 3, NB = 3, q_total = 6") 75 | ax = fig.add_subplot(111) 76 | ax.bar(x, y) 77 | ax.set(xlabel = "qA", ylabel = "OmegaTotal") 78 | 79 | x, y, S_A, S_B, S_total = macrostate(100, 300, 200) 80 | fig2 = plt.figure() 81 | fig2.suptitle("NA = 300, NB = 200, q_total = 100") 82 | ax2 = fig2.add_subplot(111) 83 | ax2.bar(x, y) 84 | ax2.set(xlabel = "qA", ylabel = "OmegaTotal") 85 | 86 | fig3 = plt.figure() 87 | ax3 = fig3.add_subplot(111) 88 | ax3.plot(x, S_A, "r", label = r"$S_A$") 89 | ax3.plot(x, S_B, "g", label = r"$S_B$") 90 | ax3.plot(x, S_total, "b", label = r"$S_{total}$") 91 | ax3.legend() 92 | ax3.set(xlabel = "qA", ylabel = "Entropy") 93 | 94 | plt.show() 95 | -------------------------------------------------------------------------------- /libnfc/list-devices.c: -------------------------------------------------------------------------------- 1 | /* Lists all connected devices supported by libnfc */ 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | static const int MAX_DEVICES = 32; 10 | 11 | int main() 12 | { 13 | nfc_context *context = NULL; 14 | nfc_connstring connstrings[MAX_DEVICES]; 15 | nfc_device *dev; 16 | const nfc_modulation_type *supported_mt; 17 | const nfc_baud_rate *supported_br; 18 | int err = EXIT_SUCCESS; 19 | 20 | nfc_init(&context); 21 | if (context == NULL) { 22 | printf("Error initializing libnfc!\n"); 23 | err = EXIT_FAILURE; 24 | goto error; 25 | } 26 | printf("libnfc version: %s\n", nfc_version()); 27 | 28 | for (size_t i = 0; i < nfc_list_devices(context, connstrings, MAX_DEVICES); ++i) { 29 | printf("\nDevice %zd: %s\n", i, connstrings[i]); 30 | if ((dev = nfc_open(context, connstrings[i])) == NULL) { 31 | printf("Failed to open device!\n"); 32 | continue; 33 | } 34 | 35 | printf("Supported modulation types as initiator:"); 36 | if (nfc_device_get_supported_modulation(dev, N_INITIATOR, &supported_mt) < 0) { 37 | nfc_perror(dev, "nfc_device_get_supported_modulation"); 38 | err = EXIT_FAILURE; 39 | goto error; 40 | } 41 | 42 | while (*supported_mt) { 43 | printf("\n\t* %s: ", str_nfc_modulation_type(*supported_mt)); 44 | 45 | if (nfc_device_get_supported_baud_rate(dev, *supported_mt, &supported_br) < 0) { 46 | nfc_perror(dev, "nfc_device_get_supported_baud_rate"); 47 | err = EXIT_FAILURE; 48 | goto error; 49 | } 50 | 51 | while (*supported_br) { 52 | printf("%s%s", str_nfc_baud_rate(*supported_br), supported_br[1]?", ":""); 53 | ++supported_br; 54 | } 55 | 56 | ++supported_mt; 57 | } 58 | 59 | printf("\nSupported modulation types as target:"); 60 | if (nfc_device_get_supported_modulation(dev, N_TARGET, &supported_mt) < 0) { 61 | nfc_perror(dev, "nfc_device_get_supported_modulation"); 62 | err = EXIT_FAILURE; 63 | goto error; 64 | } 65 | 66 | while (*supported_mt) { 67 | printf("\n\t* %s: ", str_nfc_modulation_type(*supported_mt)); 68 | 69 | if (nfc_device_get_supported_baud_rate(dev, *supported_mt, &supported_br) < 0) { 70 | nfc_perror(dev, "nfc_device_get_supported_baud_rate"); 71 | err = EXIT_FAILURE; 72 | goto error; 73 | } 74 | 75 | while (*supported_br) { 76 | printf("%s%s", str_nfc_baud_rate(*supported_br), supported_br[1]?", ":""); 77 | ++supported_br; 78 | } 79 | 80 | ++supported_mt; 81 | } 82 | 83 | puts("\n"); 84 | nfc_close(dev); 85 | } 86 | 87 | error: 88 | if (context) 89 | nfc_exit(context); 90 | return err; 91 | } 92 | -------------------------------------------------------------------------------- /icestick/ws2812/ledstring.v: -------------------------------------------------------------------------------- 1 | // Lint with: verilator --lint-only ledstring.v 2 | 3 | `default_nettype none 4 | `timescale 1ns / 1ps 5 | 6 | module ledstring #( 7 | // Clock frequency / Hz 8 | parameter F_CLK = 48e6, 9 | // Number of LEDs - 1 10 | parameter N = 7 11 | )( 12 | input clk, rst, 13 | output dout 14 | ); 15 | // T_reset: > 50 us 16 | localparam T_WAIT = 250e-3; 17 | //localparam T_WAIT = 1e-6; // for testbench 18 | //localparam N_WAIT = $ceil(T_WAIT * F_CLK); 19 | localparam N_WAIT = 12_000_000; 20 | // Counter width for delay 21 | localparam CNT = $clog2($rtoi($ceil(T_WAIT * F_CLK))); 22 | 23 | // FSM states 24 | localparam WAIT = 4'b0001, 25 | LOAD_DATA = 4'b0010, 26 | START = 4'b0100, 27 | BUSY = 4'b1000; 28 | 29 | reg [3:0] state, next_state; 30 | reg [$clog2(N+1)-1:0] led, next_led; 31 | reg [$clog2(N+1)-1:0] n, next_n; 32 | reg [CNT-1:0] cnt, next_cnt; 33 | 34 | wire busy; 35 | reg start; 36 | reg [23:0] data; 37 | 38 | ws2812_tx #( 39 | //.F_CLK(F_CLK) 40 | ) ws2812 ( 41 | .clk(clk), 42 | .rst(rst), 43 | .start(start), 44 | .bsy(busy), 45 | .dout(dout), 46 | .data(data) 47 | ); 48 | 49 | // State register 50 | always @(posedge clk, negedge rst) begin 51 | if (!rst) begin 52 | state <= WAIT; 53 | cnt <= 0; 54 | led <= N; 55 | end 56 | else begin 57 | state <= next_state; 58 | cnt <= next_cnt; 59 | n <= next_n; 60 | led <= next_led; 61 | end 62 | end 63 | 64 | // Next state logic 65 | always @* begin 66 | // Defaults 67 | next_state = state; 68 | next_cnt = cnt; 69 | next_n = n; 70 | next_led = led; 71 | start = 1'b0; 72 | 73 | /* verilator lint_off CASEINCOMPLETE */ 74 | case (state) 75 | WAIT: begin 76 | if (cnt < N_WAIT) 77 | next_cnt = cnt + 1'b1; 78 | else begin 79 | // Reset counter 80 | next_cnt = 0; 81 | // Reset LED counter 82 | next_n = 0; 83 | // Light up next LED in the string 84 | if (led == N) 85 | next_led = 0; 86 | else 87 | next_led = led + 1'b1; 88 | // Refresh all LEDs in the string 89 | next_state = LOAD_DATA; 90 | end 91 | end 92 | 93 | LOAD_DATA: begin 94 | if (n == led) 95 | data = 24'h005500; 96 | else 97 | data = 24'h000000; 98 | // Start transmission 99 | start = 1'b1; 100 | next_state = START; 101 | end 102 | 103 | START: begin 104 | start = 1'b1; 105 | next_state = BUSY; 106 | end 107 | 108 | BUSY: begin 109 | start = 1'b0; 110 | 111 | if (busy == 1'b0) begin 112 | // Last LED in string? 113 | if (n == N) 114 | next_state = WAIT; 115 | else begin 116 | next_n = n + 1'b1; 117 | next_state = LOAD_DATA; 118 | end 119 | end 120 | end 121 | endcase 122 | /* verilator lint_on CASEINCOMPLETE */ 123 | end 124 | endmodule 125 | -------------------------------------------------------------------------------- /cowrie/cowrie-stats.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Cowrie logfile 4 | LOG=/home/cowrie/cowrie/log/audit.log 5 | 6 | # Generated HTML file 7 | HTML=/home/pi/cowrie-stats.html 8 | 9 | # HTML header 10 | 11 | cat >$HTML < 13 | 14 | 15 | 16 | Cowrie SSH Honeypot Statistics 17 | 32 | 33 | 34 |

Cowrie SSH Honeypot Statistics

35 | HTML_HEADER 36 | 37 | # Table: connections / IP 38 | 39 | cat >>$HTML < 41 | 42 | IP (Hostname) 43 | Connections 44 | 45 | IP_TABLE_HEADER 46 | 47 | <$LOG awk '/New connection/ { 48 | split($4, ip_address, ":") 49 | ip[ip_address[1]]++ 50 | } 51 | 52 | END { 53 | for (i in ip) { 54 | "dig +noall +answer -x " i | getline rev_dns 55 | if (rev_dns != "") { 56 | split(rev_dns, host) 57 | gsub(/\.$/, "", host[5]) 58 | print i " (" host[5] "):" ip[i] 59 | } else { 60 | print i ":" ip[i] 61 | } 62 | } 63 | }' | sort -nr -k 2,2 -t ':' | sed 's/\(.*\):\(.*\)/\1<\/td>\2<\/td><\/tr>/' >>$HTML 64 | 65 | cat >>$HTML < 67 |
68 | IP_TABLE_FOOTER 69 | 70 | # Table: username/password 71 | 72 | cat >>$HTML < 74 | 75 | Username/password 76 | Attempts 77 | 78 | USER_TABLE_HEADER 79 | 80 | <$LOG awk '/login attempt/ { 81 | user_password[$4]++ 82 | } 83 | 84 | END { 85 | for (i in user_password) { 86 | count = user_password[i] 87 | gsub(/&/, "\\&", i) 88 | gsub(/>/, "\\>", i) 89 | gsub(/\1<\/td>\2<\/td><\/tr>/' >>$HTML 94 | 95 | cat >>$HTML < 97 |
98 | USER_TABLE_FOOTER 99 | 100 | # Table: remote SSH versions 101 | 102 | cat >>$HTML < 104 | 105 | Remote SSH version 106 | Count 107 | 108 | VERSION_TABLE_HEADER 109 | 110 | <$LOG awk '/Remote SSH version/ { 111 | ssh_version[$5]++ 112 | } 113 | 114 | END { 115 | for (i in ssh_version) 116 | print i ":" ssh_version[i] 117 | }' | sort -nr -k 2,2 -t ':' | sed 's/\(.*\):\(.*\)/\1<\/td>\2<\/td><\/tr>/' >>$HTML 118 | 119 | cat >>$HTML < 121 | VERSION_TABLE_FOOTER 122 | 123 | # HTML footer 124 | 125 | DATE=`date` 126 | 127 | cat >>$HTML < 129 |

Generated on ${DATE}

130 | 131 | 132 | HTML_FOOTER 133 | -------------------------------------------------------------------------------- /nbody/nbody_cpu/nbody_cpu.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | typedef struct { 7 | float x, y, z; 8 | } float3; 9 | 10 | typedef struct { 11 | float x, y, z, w; 12 | } float4; 13 | 14 | /* #bodies */ 15 | long N; 16 | 17 | /* #timesteps */ 18 | static const long TIMESTEPS = 100; 19 | /* softening factor (square), G, \Delta t */ 20 | static const float EPS = 0.1, G = 2., DELTA_T = 0.01; 21 | 22 | /* acceleration */ 23 | float3 *a; 24 | /* mass */ 25 | float *m; 26 | /* position */ 27 | float3 *r; 28 | /* velocity */ 29 | float3 *v; 30 | 31 | /* random number in [0,1] */ 32 | static inline float rnd() 33 | { 34 | return (float)rand() / RAND_MAX; 35 | } 36 | 37 | void init(); 38 | 39 | int main(int argc, char *argv[]) 40 | { 41 | clock_t start, stop; 42 | double time; 43 | long i, j, timestep; 44 | 45 | float3 rij, ai; 46 | float dst_sqr, cube, inv_sqrt; 47 | 48 | if (argc != 2) { 49 | printf("usage: nbody_cpu #bodies\n"); 50 | exit(1); 51 | } 52 | 53 | N = atol(argv[1]); 54 | 55 | /* alloc host memory */ 56 | a = (float3 *)malloc(N*sizeof(float3)); 57 | m = (float *)malloc(N*sizeof(float)); 58 | r = (float3 *)malloc(N*sizeof(float3)); 59 | v = (float3 *)malloc(N*sizeof(float3)); 60 | 61 | srand(1); 62 | init(); 63 | 64 | /* measure execution time */ 65 | start = clock(); 66 | 67 | /* integration steps */ 68 | for (timestep = 0; timestep < TIMESTEPS; ++timestep) { 69 | /* update accelerations */ 70 | for (i = 0; i < N; ++i) { 71 | ai.x = 0; 72 | ai.y = 0; 73 | ai.z = 0; 74 | 75 | for (j = 0; j < N; ++j) { 76 | /* distance vector */ 77 | rij.x = r[j].x - r[i].x; 78 | rij.y = r[j].y - r[i].y; 79 | rij.z = r[j].z - r[i].z; 80 | /* compute acceleration */ 81 | dst_sqr = rij.x * rij.x + 82 | rij.y * rij.y + rij.z * rij.z + EPS; 83 | cube = dst_sqr * dst_sqr * dst_sqr; 84 | inv_sqrt = 1. / sqrtf(cube); 85 | inv_sqrt *= m[j]; 86 | /* acceleration a_i */ 87 | ai.x += rij.x * inv_sqrt; 88 | ai.y += rij.y * inv_sqrt; 89 | ai.z += rij.z * inv_sqrt; 90 | } 91 | 92 | /* store result */ 93 | a[i].x = G * ai.x; 94 | a[i].y = G * ai.y; 95 | a[i].z = G * ai.z; 96 | } 97 | 98 | /* leap frog */ 99 | for (i = 0; i < N; ++i) { 100 | /* step with constant coordinates */ 101 | v[i].x = v[i].x + a[i].x * DELTA_T; 102 | v[i].y = v[i].y + a[i].y * DELTA_T; 103 | v[i].z = v[i].z + a[i].z * DELTA_T; 104 | /* step with constant velocities */ 105 | r[i].x = r[i].x + v[i].x * DELTA_T; 106 | r[i].y = r[i].y + v[i].y * DELTA_T; 107 | r[i].z = r[i].z + v[i].z * DELTA_T; 108 | } 109 | /*printf("#1: x: %f, y: %f, z: %f\n", r[0].x, r[0].y, r[0].z); 110 | printf("#2: x: %f, y: %f, z: %f\n", r[1].x, r[1].y, r[1].z);*/ 111 | } 112 | 113 | stop = clock(); 114 | time = (double)(stop - start) / CLOCKS_PER_SEC; 115 | /*printf("elapsed time: %f\n", time); 116 | printf("interactions/second: %f\n", (TIMESTEPS*(N*N))/time);*/ 117 | printf("%li\t%f\n", N, (TIMESTEPS*(N*N))/time); 118 | 119 | /* free host memory */ 120 | free(a); 121 | free(m); 122 | free(r); 123 | free(v); 124 | 125 | return 0; 126 | } 127 | 128 | /* generate initial configuration */ 129 | void init() 130 | { 131 | long i; 132 | 133 | for (i = 0; i < N; ++i) { 134 | /* mass */ 135 | m[i] = rnd()>0.5 ? 1. : 10.; 136 | 137 | /* velocity */ 138 | v[i].x = 3.; 139 | v[i].y = rnd() * 10; 140 | v[i].z = -5.; 141 | 142 | /* position */ 143 | r[i].x = rnd() * 50; 144 | r[i].y = rnd() * 50; 145 | r[i].z = rnd() * 50; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /python/scipy.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | """ 4 | @author: m-thu 5 | """ 6 | 7 | # (1, 2, 3): tuple (immutable) 8 | # [1, 2, 3]: list (mutable) 9 | # {"key": value}: dictionary (mutable) 10 | 11 | 12 | 13 | import numpy as np 14 | 15 | # help: np.array? 16 | # np.con*? 17 | # np.lookfor("create array") 18 | 19 | 20 | 21 | # 1D array 22 | 23 | a = np.array([0, 1, 2, 3]) 24 | print("a:", a) 25 | print("a.ndim:", a.ndim) 26 | print("a.shape:", a.shape) 27 | print("len(a):", len(a), "\n\n") 28 | 29 | 30 | 31 | # 2D array 32 | 33 | b = np.array([[0, 1, 2], [3, 4, 5]]) 34 | print("b:", b) 35 | print("b.ndim:", b.ndim) 36 | print("b.shape:", b.shape) 37 | print("len(b):", len(b), "\n\n") # first dimension 38 | 39 | 40 | 41 | # Creating arrays 42 | 43 | a = np.arange(10) # 0, 1, ..., n-1 44 | print("arange(10):", a, "\n") 45 | 46 | b = np.arange(1, 10, 2) # start, start+2, ..., stop-1 47 | print("arange(1, 10, 2):", b, "\n") 48 | 49 | c = np.linspace(0, 1, 10) # start; stop; number of points 50 | print("linspace(0, 1, 10):\n", c, "\n") 51 | 52 | d = np.ones((4, 4)) # all ones 53 | print("ones((4, 4)):\n", d, "\n") 54 | 55 | e = np.zeros((3, 3)) # all zeros 56 | print("zeros((3, 3)):\n", e, "\n") 57 | 58 | f = np.eye(2) # unity matrix (nxn) 59 | print("eye((2, 2)\n", f, "\n") 60 | 61 | g = np.diag(np.array([1, 2, 3, 4])) # diagonal matrix 62 | print("diag(np.array([1, 2, 3, 4])):\n", g, "\n\n") 63 | 64 | 65 | 66 | # Data types 67 | 68 | a = np.array([1, 2, 3]) 69 | print("[1, 2, 3] dtype:", a.dtype, "\n") 70 | 71 | b = np.array([1., 2., 3.]) 72 | print("[1., 2., 3.] dtype:", b.dtype, "\n") 73 | 74 | c = np.array([1, 2, 3], dtype=float) # specify type 75 | print("[1, 2, 3], dtype=float dtype:", c.dtype, "\n") 76 | 77 | d = np.array([1+1j, 2+2j, 3+3j]) # complex numbers 78 | print("[1+1j, 2+2j, 3+3j] dtype:", d.dtype, "\n\n") 79 | 80 | 81 | 82 | # pyplot: procedural interface 83 | import matplotlib.pyplot as plt 84 | 85 | # 1D plots 86 | 87 | x = np.linspace(0., 10., 20) 88 | y = x**2 89 | plt.figure() 90 | plt.plot(x, y) 91 | plt.plot(x, y, 'o') 92 | #plt.show() 93 | 94 | x = np.linspace(0., 2*np.pi) 95 | y1 = np.sin(x) 96 | y2 = np.cos(x) 97 | plt.figure() 98 | plt.plot(x, y1) 99 | plt.plot(x, y2) 100 | #plt.show() 101 | 102 | 103 | 104 | # Indexing, slicing 105 | 106 | a = np.arange(5) 107 | print("a:", a, "\n") 108 | print("a[0]:", a[0], "a[1]:", a[1], "a[-1]:", a[-1], "\n") 109 | 110 | print("a[::-1]: ", a[::-1], "\n\n") # reverse 111 | 112 | 113 | 114 | # Operations on arrays 115 | 116 | a = np.eye(3) 117 | a += 1 118 | print("a:", a, "\n") 119 | 120 | a = np.arange(5) 121 | b = a[::-1] 122 | c = b-a 123 | print("c:", c, "\n") 124 | 125 | d = a*a # elementwise multiplication 126 | print("d:", d, "\n") 127 | 128 | e = np.ones((2,2)) 129 | print("e*e:\n", e*e, "\n") # elementwise 130 | print("e.dot(e):\n", e.dot(e), "\n") # matrix multiplication 131 | 132 | f = np.array([[0, -1j], [1j, 0]]) 133 | print("f:\n", f, "\n") 134 | print("f^t:\n", f.T, "\n") # transpose (view of the original array) 135 | 136 | a = np.array([1, 2, 3, 4]) 137 | print(np.sum(a), end="\n") 138 | 139 | b = np.array([[1, 2], [3, 4]]) 140 | print("b:\n", b, "\n") 141 | print("sum of columns: ", np.sum(b, axis=0), "\n") 142 | print("sum of rows:", np.sum(b, axis=1), "\n") 143 | print("min:", np.min(b), "\tmax:", np.max(b), "\n") 144 | print("b.ravel():\n", b.ravel(), "\n") # flatten array 145 | 146 | 147 | 148 | # Polynomials 149 | 150 | # x^2 + 2*x + 3 151 | p = np.poly1d([1, 2, 3]) 152 | 153 | print("p(0) =", p(0)) 154 | print("order:", p.order) 155 | print("roots:", p.roots) 156 | print("p(root1) =", p(p.roots[0])) 157 | print("p(root2) =", p(p.roots[1]), "\n") -------------------------------------------------------------------------------- /tempcontrol/README.md: -------------------------------------------------------------------------------- 1 | Temperature control (Raspberry Pi 2) 2 | ==================================== 3 | 4 | ### raspi-config 5 | * 3 Boot Options -> Wait for network at boot -> No 6 | * 5 Interfacing Options -> P5 I2C -> Yes 7 | * 5 Interfacing Options -> P7 1-Wire -> Yes 8 | 9 | ### DS1307 module 10 | Remove `R2` and `R3` pullups (3K3) 11 | 12 | Wiring: 13 | ``` 14 | 5 V ------ VCC (DS1307) 15 | I2C1 SCL <----> SCL (DS1307) 16 | I2C1 SDA <----> SDA (DS1307) 17 | GND ------ GND (DS1307) 18 | ``` 19 | 20 | Test module with `sudo i2cdetect -y 1`: 21 | ``` 22 | 0 1 2 3 4 5 6 7 8 9 a b c d e f 23 | 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 24 | 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 25 | 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 26 | 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 27 | 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 28 | 50: 50 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 29 | 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 30 | 70: -- -- -- -- -- -- -- -- 31 | ``` 32 | 33 | `/etc/rc.local`: 34 | ```sh 35 | /bin/echo ds1307 0x68 >/sys/class/i2c-adapter/i2c-1/new_device 36 | hwclock -s 37 | ``` 38 | 39 | ```sh 40 | sudo sh -c 'echo rtc-ds1307 >>/etc/modules' 41 | ``` 42 | 43 | ### GPIOs 44 | `/etc/rc.local`: 45 | ```sh 46 | # GPIO 27: Heater 1 (output) 47 | /bin/echo 27 >/sys/class/gpio/export 48 | /bin/echo out >/sys/class/gpio/gpio27/direction 49 | 50 | # GPIO 22: Heater 2 (output) 51 | /bin/echo 22 >/sys/class/gpio/export 52 | /bin/echo out >/sys/class/gpio/gpio22/direction 53 | 54 | # GPIO 16: Status LED (output) 55 | /bin/echo 16 >/sys/class/gpio/export 56 | /bin/echo out >/sys/class/gpio/gpio16/direction 57 | 58 | # GPIO 26: Shutdown switch (input, active low) 59 | /bin/echo 26 >/sys/class/gpio/export 60 | /bin/echo in >/sys/class/gpio/gpio26/direction 61 | ``` 62 | 63 | `/etc/rc.local`: 64 | ```sh 65 | nohup /home/pi/shutdown.sh & 66 | date >>/root/startup.log 67 | ``` 68 | 69 | ### DS18B20 70 | Wiring: 71 | ``` 72 | 3.3 V --o---- VDD (DS18B20) 73 | | 74 | --- 75 | | | 4K7 76 | | | Pullup 77 | | | 78 | --- 79 | | 80 | GPIO 4 <-o---> DQ (DS18B20) 81 | GND ------- GND (DS18B20) 82 | ``` 83 | 84 | Test sensors: 85 | ```sh 86 | pi@raspberrypi:~ $ ls -l /sys/bus/w1/devices/ 87 | total 0 88 | lrwxrwxrwx 1 root root 0 May 12 20:52 28-0416927dcdff -> ../../../devices/w1_bus_master1/28-0416927dcdff 89 | lrwxrwxrwx 1 root root 0 May 12 20:52 28-051691e2c3ff -> ../../../devices/w1_bus_master1/28-051691e2c3ff 90 | lrwxrwxrwx 1 root root 0 May 12 20:52 28-051691e533ff -> ../../../devices/w1_bus_master1/28-051691e533ff 91 | lrwxrwxrwx 1 root root 0 May 12 20:52 w1_bus_master1 -> ../../../devices/w1_bus_master1 92 | pi@raspberrypi:~ $ cat /sys/bus/w1/devices/28-0416927dcdff/ 93 | driver/ id name power/ subsystem/ uevent w1_slave 94 | pi@raspberrypi:~ $ cat /sys/bus/w1/devices/28-0416927dcdff/w1_slave 95 | 6e 01 4b 46 7f ff 0c 10 ad : crc=ad YES 96 | 6e 01 4b 46 7f ff 0c 10 ad t=22875 97 | pi@raspberrypi:~ $ cat /sys/bus/w1/devices/28-051691e2c3ff/w1_slave 98 | 6e 01 4b 46 7f ff 0c 10 ad : crc=ad YES 99 | 6e 01 4b 46 7f ff 0c 10 ad t=22875 100 | pi@raspberrypi:~ $ cat /sys/bus/w1/devices/28-051691e533ff/w1_slave 101 | 69 01 4b 46 7f ff 0c 10 7d : crc=7d YES 102 | 69 01 4b 46 7f ff 0c 10 7d t=22562 103 | ``` 104 | 105 | ### tempcontrol.c 106 | `/etc/rc.local`: 107 | ```sh 108 | nohup /home/pi/tempcontrol 0.5 & 109 | ``` 110 | 111 | ### References 112 | [DS1307 datasheet](https://datasheets.maximintegrated.com/en/ds/DS1307.pdf) 113 | 114 | [DS18B20 datasheet](https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf) 115 | -------------------------------------------------------------------------------- /icestick/ws2812/ws2812_tx.v: -------------------------------------------------------------------------------- 1 | // Lint with: verilator --lint-only ws2812_tx.v 2 | 3 | `default_nettype none 4 | `timescale 1ns / 1ps 5 | 6 | module ws2812_tx #( 7 | // Clock frequency / Hz 8 | parameter F_CLK = 48e6 9 | )( 10 | input [23:0] data, 11 | input clk, rst, start, 12 | output reg dout, 13 | output wire bsy 14 | ); 15 | // FSM states 16 | localparam IDLE = 5'b00001, 17 | T0H = 5'b00010, 18 | T0L = 5'b00100, 19 | T1H = 5'b01000, 20 | T1L = 5'b10000; 21 | 22 | localparam 23 | // Bit 0, high time 24 | T_T0H = 350e-9, // s 25 | // Bit 0, low time 26 | T_T0L = 800e-9, // s 27 | // Bit 1, high time 28 | T_T1H = 700e-9, // s 29 | // Bit 1, low time 30 | T_T1L = 600e-9; // S 31 | 32 | localparam 33 | // Bit 0, cycles high time 34 | N_T0H = 17,//$ceil(T_T0H * F_CLK), 35 | // Bit 0, cycles low time 36 | N_T0L = 39,//$ceil(T_T0L * F_CLK), 37 | // Bit 1, cycles high time 38 | N_T1H = 34,//$ceil(T_T1H * F_CLK), 39 | // Bit 1, cycles low time 40 | N_T1L = 29;//$ceil(T_T1L * F_CLK); 41 | 42 | // Counter width for delay 43 | localparam CNT = $clog2($rtoi($ceil(T_T0L * F_CLK))); 44 | 45 | reg [4:0] state, next_state; 46 | reg [4:0] n, next_n; 47 | reg [CNT-1:0] cnt, next_cnt; 48 | 49 | // State register 50 | always @(posedge clk, negedge rst) begin 51 | if (!rst) 52 | state <= IDLE; 53 | else begin 54 | state <= next_state; 55 | n <= next_n; 56 | cnt <= next_cnt; 57 | end 58 | end 59 | 60 | // Next state logic 61 | always @* begin 62 | // Defaults 63 | next_state = state; 64 | next_n = n; 65 | next_cnt = cnt; 66 | dout = 1'b0; 67 | 68 | /* verilator lint_off CASEINCOMPLETE */ 69 | case (state) 70 | IDLE: begin 71 | // Begin transmission when start signal gets asserted 72 | if (start) 73 | // MSB is transmitted first 74 | if (data[23] == 1'b1) 75 | next_state = T1H; 76 | else 77 | next_state = T0H; 78 | next_n = 5'd23; 79 | next_cnt = 0; 80 | dout = 1'b0; 81 | end 82 | 83 | T0H: begin 84 | if (cnt < N_T0H) 85 | next_cnt = cnt + 1'b1; 86 | else begin 87 | // Reset counter 88 | next_cnt = 0; 89 | next_state = T0L; 90 | end 91 | 92 | dout = 1'b1; 93 | end 94 | 95 | T0L: begin 96 | if (cnt < N_T0L) 97 | next_cnt = cnt + 1'b1; 98 | else begin 99 | // Reset counter 100 | next_cnt = 0; 101 | 102 | // Jump to idle state if this was the last bit 103 | if (n == 0) 104 | next_state = IDLE; 105 | else begin 106 | // Determine if next bit is 1 or 0 107 | next_n = n - 1'b1; 108 | if (data[next_n] == 1'b1) 109 | next_state = T1H; 110 | else 111 | next_state = T0H; 112 | end 113 | 114 | end 115 | 116 | dout = 1'b0; 117 | end 118 | 119 | T1H: begin 120 | if (cnt < N_T1H) 121 | next_cnt = cnt + 1'b1; 122 | else begin 123 | // Reset counter 124 | next_cnt = 0; 125 | next_state = T1L; 126 | end 127 | 128 | dout = 1'b1; 129 | end 130 | 131 | T1L: begin 132 | if (cnt < N_T1L) 133 | next_cnt = cnt + 1'b1; 134 | else begin 135 | // Reset counter 136 | next_cnt = 0; 137 | 138 | // Jump to idle state if this was the last bit 139 | if (n == 0) 140 | next_state = IDLE; 141 | else begin 142 | // Determine if next bit is 1 or 0 143 | next_n = n - 1'b1; 144 | if (data[next_n] == 1'b1) 145 | next_state = T1H; 146 | else 147 | next_state = T0H; 148 | end 149 | end 150 | 151 | dout = 1'b0; 152 | end 153 | endcase 154 | /* verilator lint_on CASEINCOMPLETE */ 155 | end 156 | 157 | assign bsy = !(state == IDLE); 158 | endmodule 159 | -------------------------------------------------------------------------------- /libnfc/list-tags.c: -------------------------------------------------------------------------------- 1 | /* Lists all tags in vicinity of reader */ 2 | 3 | /* for sigaction */ 4 | #define _POSIX_SOURCE 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static const int MAX_DEVICES = 32; 14 | static const int MAX_TARGETS = 32; 15 | 16 | /* global, so signal handler can access those */ 17 | nfc_context *context; 18 | nfc_device *dev; 19 | 20 | static void handler_sigint(int); 21 | 22 | int main() 23 | { 24 | struct sigaction sa; 25 | nfc_connstring connstrings[MAX_DEVICES]; 26 | int err = EXIT_SUCCESS; 27 | 28 | /* Init libnfc */ 29 | nfc_init(&context); 30 | if (context == NULL) { 31 | printf("Error initializing libnfc!\n"); 32 | err = EXIT_FAILURE; 33 | goto error; 34 | } 35 | printf("libnfc version: %s\n", nfc_version()); 36 | 37 | /* Install signal handler for SIGINT */ 38 | sigemptyset(&sa.sa_mask); 39 | sa.sa_handler = handler_sigint; 40 | if (sigaction(SIGINT, &sa, NULL) < 0) { 41 | perror("sigaction"); 42 | err = EXIT_FAILURE; 43 | goto error; 44 | } 45 | 46 | /* Find supported device */ 47 | for (size_t i = 0; i < nfc_list_devices(context, connstrings, MAX_DEVICES); ++i) { 48 | if ((dev = nfc_open(context, connstrings[i])) == NULL) { 49 | printf("Failed to open device '%s'!\n", connstrings[i]); 50 | continue; 51 | } else { 52 | printf("Opened device '%s'\n", connstrings[i]); 53 | break; 54 | } 55 | } 56 | if (dev == NULL) { 57 | printf("No supported device found!\n"); 58 | err = EXIT_FAILURE; 59 | goto error; 60 | } 61 | 62 | const nfc_modulation_type *supported_mt; 63 | 64 | /* Get supported modulation types as initiator */ 65 | if (nfc_device_get_supported_modulation(dev, N_INITIATOR, &supported_mt) < 0) { 66 | nfc_perror(dev, "nfc_device_get_supported_modulation"); 67 | err = EXIT_FAILURE; 68 | goto error; 69 | } 70 | 71 | /* Initialize NFC device as initiator */ 72 | if (nfc_initiator_init(dev) < 0) { 73 | nfc_perror(dev, "nfc_initiator_init"); 74 | err = EXIT_FAILURE; 75 | goto error; 76 | } 77 | /* Disable forced ISO 14443-A mode (enabled by default) */ 78 | if (nfc_device_set_property_bool(dev, NP_FORCE_ISO14443_A, false) < 0) { 79 | nfc_perror(dev, "nfc_device_set_property"); 80 | err = EXIT_FAILURE; 81 | goto error; 82 | } 83 | /* Disable forced speed of 106 kbps (enabled by default) */ 84 | if (nfc_device_set_property_bool(dev, NP_FORCE_SPEED_106, false) < 0) { 85 | nfc_perror(dev, "nfc_device_set_property"); 86 | err = EXIT_FAILURE; 87 | goto error; 88 | } 89 | 90 | /* List all targets for each modulation type */ 91 | while (*supported_mt) { 92 | const nfc_baud_rate *supported_br; 93 | 94 | printf("\nListing tags with modulation type %s, ", str_nfc_modulation_type(*supported_mt)); 95 | 96 | /* Get supported baud rates for mod. type and find the slowest */ 97 | if (nfc_device_get_supported_baud_rate(dev, *supported_mt, &supported_br) < 0) { 98 | nfc_perror(dev, "nfc_device_get_supported_baud_rate"); 99 | err = EXIT_FAILURE; 100 | goto error; 101 | } 102 | while (*supported_br) 103 | ++supported_br; 104 | --supported_br; 105 | printf("baud rate: %s\n", str_nfc_baud_rate(*supported_br)); 106 | 107 | 108 | nfc_modulation mod; 109 | mod.nmt = *supported_mt; 110 | mod.nbr = *supported_br; 111 | 112 | int tags; 113 | nfc_target targets[MAX_TARGETS]; 114 | 115 | /* List passive targets */ 116 | if ((tags = nfc_initiator_list_passive_targets(dev, mod, targets, MAX_TARGETS)) < 0) { 117 | nfc_perror(dev, "nfc_initiator_list_passive_targets"); 118 | err = EXIT_FAILURE; 119 | goto error; 120 | } 121 | 122 | for (int i = 0; i < tags; ++i) { 123 | char *target_info; 124 | 125 | if (str_nfc_target(&target_info, &targets[i], true) < 0) { 126 | nfc_perror(dev, "str_nfc_target"); 127 | err = EXIT_FAILURE; 128 | goto error; 129 | } 130 | 131 | printf("\n=====\nTag %i\n=====\n%s\n", i, target_info); 132 | nfc_free(target_info); 133 | } 134 | 135 | ++supported_mt; 136 | } 137 | 138 | error: 139 | if (dev) 140 | nfc_close(dev); 141 | if (context) 142 | nfc_exit(context); 143 | return err; 144 | } 145 | 146 | static void handler_sigint(int signal) 147 | { 148 | (void)signal; 149 | 150 | if (dev != NULL) { 151 | nfc_abort_command(dev); 152 | } else { 153 | if (context) 154 | nfc_exit(context); 155 | exit(EXIT_FAILURE); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /openscad/parallela.scad: -------------------------------------------------------------------------------- 1 | // Baseplate for Parallela board with fan 2 | 3 | $fa = 1; 4 | $fs = 0.4; 5 | 6 | eps = 0.01; 7 | 8 | // Dimensions rectangular plate 9 | // with standoff holes 10 | length = 110.0; 11 | width = 95.0; 12 | height = 4.0; 13 | standoff_diameter = 3.0; 14 | standoff_x_dist = length - 4*standoff_diameter; 15 | standoff_y_dist = width - 4*standoff_diameter; 16 | standoff_counterbore = 2.0; 17 | standoff_counterbore_r = 3.0; 18 | 19 | // Parallela board mounting holes 20 | parallela_hole_diameter = 3.0; 21 | parallela_hole_x_dist = 83.0 - 2*parallela_hole_diameter; 22 | parallela_hole_y_dist = 51.4 - 2*parallela_hole_diameter; 23 | parallela_counterbore = 2.0; 24 | parallela_counterbore_r = 6.0 / 2; 25 | 26 | // Fan mounting holes (rectangular) 27 | fan_hole_diameter = 3.0; 28 | fan_hole_dist = 76.0 - 2*fan_hole_diameter; 29 | fan_counterbore_r = 6.0 / 2; 30 | fan_counterbore = 2.0; 31 | 32 | difference() { 33 | cube([length,width,height], center=true); 34 | 35 | union() { 36 | // Standoff holes 37 | union() { 38 | // Screw holes 39 | translate([standoff_x_dist/2,standoff_y_dist/2,0]) cylinder(h=height+2*eps, r=standoff_diameter/2, center=true); 40 | translate([-standoff_x_dist/2,standoff_y_dist/2,0]) cylinder(h=height+2*eps, r=standoff_diameter/2, center=true); 41 | translate([standoff_x_dist/2,-standoff_y_dist/2,0]) cylinder(h=height+2*eps, r=standoff_diameter/2, center=true); 42 | translate([-standoff_x_dist/2,-standoff_y_dist/2,0]) cylinder(h=height+2*eps, r=standoff_diameter/2, center=true); 43 | 44 | // Counterbore 45 | translate([standoff_x_dist/2,standoff_y_dist/2,0]) cylinder(r=standoff_counterbore_r, h=standoff_counterbore+eps, $fn=6); 46 | translate([-standoff_x_dist/2,standoff_y_dist/2,0]) cylinder(r=standoff_counterbore_r, h=standoff_counterbore+eps, $fn=6); 47 | translate([standoff_x_dist/2,-standoff_y_dist/2,0]) cylinder(r=standoff_counterbore_r, h=standoff_counterbore+eps, $fn=6); 48 | translate([-standoff_x_dist/2,-standoff_y_dist/2,0]) cylinder(r=standoff_counterbore_r, h=standoff_counterbore+eps, $fn=6); 49 | } 50 | 51 | // Parallela board mounting holes 52 | union() { 53 | // Screw holes 54 | translate([parallela_hole_x_dist/2,parallela_hole_y_dist/2,0]) cylinder(h=height+2*eps, r=parallela_hole_diameter/2, center=true); 55 | translate([-parallela_hole_x_dist/2,parallela_hole_y_dist/2,0]) cylinder(h=height+2*eps, r=parallela_hole_diameter/2, center=true); 56 | translate([parallela_hole_x_dist/2,-parallela_hole_y_dist/2,0]) cylinder(h=height+2*eps, r=parallela_hole_diameter/2, center=true); 57 | translate([-parallela_hole_x_dist/2,-parallela_hole_y_dist/2,0]) cylinder(h=height+2*eps, r=parallela_hole_diameter/2, center=true); 58 | 59 | // Counterbore 60 | translate([parallela_hole_x_dist/2,parallela_hole_y_dist/2,-height/2-eps]) cylinder(r=parallela_counterbore_r, h=parallela_counterbore); 61 | translate([-parallela_hole_x_dist/2,parallela_hole_y_dist/2,-height/2-eps]) cylinder(r=parallela_counterbore_r, h=parallela_counterbore); 62 | translate([parallela_hole_x_dist/2,-parallela_hole_y_dist/2,-height/2-eps]) cylinder(r=parallela_counterbore_r, h=parallela_counterbore); 63 | translate([-parallela_hole_x_dist/2,-parallela_hole_y_dist/2,-height/2-eps]) cylinder(r=parallela_counterbore_r, h=parallela_counterbore); 64 | } 65 | 66 | // Fan mounting holes 67 | union() { 68 | // Screw holes 69 | translate([fan_hole_dist/2,fan_hole_dist/2,0]) cylinder(h=height+2*eps, r=fan_hole_diameter/2, center=true); 70 | translate([-fan_hole_dist/2,fan_hole_dist/2,0]) cylinder(h=height+2*eps, r=fan_hole_diameter/2, center=true); 71 | translate([fan_hole_dist/2,-fan_hole_dist/2,0]) cylinder(h=height+2*eps, r=fan_hole_diameter/2, center=true); 72 | translate([-fan_hole_dist/2,-fan_hole_dist/2,0]) cylinder(h=height+2*eps, r=fan_hole_diameter/2, center=true); 73 | 74 | // Counterbore 75 | translate([fan_hole_dist/2,fan_hole_dist/2,-height/2-eps]) cylinder(r=fan_counterbore_r, h=fan_counterbore); 76 | translate([-fan_hole_dist/2,fan_hole_dist/2,-height/2-eps]) cylinder(r=fan_counterbore_r, h=fan_counterbore); 77 | translate([fan_hole_dist/2,-fan_hole_dist/2,-height/2-eps]) cylinder(r=fan_counterbore_r, h=fan_counterbore); 78 | translate([-fan_hole_dist/2,-fan_hole_dist/2,-height/2-eps]) cylinder(r=fan_counterbore_r, h=fan_counterbore); 79 | } 80 | } 81 | } -------------------------------------------------------------------------------- /libnfc/dump-girogo.c: -------------------------------------------------------------------------------- 1 | /* Dumps girogo cards */ 2 | 3 | /* for sigaction */ 4 | #define _POSIX_SOURCE 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | static const int MAX_DEVICES = 32; 15 | static const int MAX_TARGETS = 32; 16 | 17 | static const size_t MAX_RX_LENGTH = 64; 18 | 19 | /* girogo commands */ 20 | static const uint8_t DF_BOERSE[] = {0x00, 0xa4, 0x04, 0x0c, 0x09, 0xd2, 0x76, 0x00, 0x00, 0x25, 0x45, 0x50, 0x02, 0x00}; 21 | static const uint8_t EF_BETRAG[] = {0x00, 0xb2, 0x01, 0xc4, 0x00}; 22 | static const uint8_t EF_ID[] = {0x00, 0xb2, 0x01, 0xbc, 0x00}; 23 | static const uint8_t EF_BOERSE[] = {0x00, 0xb2, 0x01, 0xcc, 0x00}; 24 | 25 | /* Global, so signal handler can access those */ 26 | nfc_context *context; 27 | nfc_device *dev; 28 | 29 | static void handler_sigint(int); 30 | static void dump_girogo(void); 31 | static void dump_buffer(char *, uint8_t *, int); 32 | 33 | int main() 34 | { 35 | struct sigaction sa; 36 | nfc_connstring connstrings[MAX_DEVICES]; 37 | int err = EXIT_SUCCESS; 38 | 39 | /* Init libnfc */ 40 | nfc_init(&context); 41 | if (context == NULL) { 42 | printf("Error initializing libnfc!\n"); 43 | err = EXIT_FAILURE; 44 | goto error; 45 | } 46 | printf("libnfc version: %s\n", nfc_version()); 47 | 48 | /* Install signal handler for SIGINT */ 49 | sigemptyset(&sa.sa_mask); 50 | sa.sa_handler = handler_sigint; 51 | if (sigaction(SIGINT, &sa, NULL) < 0) { 52 | perror("sigaction"); 53 | err = EXIT_FAILURE; 54 | goto error; 55 | } 56 | 57 | /* Find supported device */ 58 | for (size_t i = 0; i < nfc_list_devices(context, connstrings, MAX_DEVICES); ++i) { 59 | if ((dev = nfc_open(context, connstrings[i])) == NULL) { 60 | printf("Failed to open device '%s'!\n", connstrings[i]); 61 | continue; 62 | } else { 63 | printf("Opened device '%s'\n", connstrings[i]); 64 | break; 65 | } 66 | } 67 | if (dev == NULL) { 68 | printf("No supported device found!\n"); 69 | err = EXIT_FAILURE; 70 | goto error; 71 | } 72 | 73 | /* Initialize NFC device as initiator */ 74 | if (nfc_initiator_init(dev) < 0) { 75 | nfc_perror(dev, "nfc_initiator_init"); 76 | err = EXIT_FAILURE; 77 | goto error; 78 | } 79 | 80 | /* Set modulation type */ 81 | nfc_modulation mod; 82 | mod.nmt = NMT_ISO14443A; 83 | mod.nbr = NBR_106; 84 | 85 | int tags; 86 | nfc_target target; 87 | 88 | /* Poll for targets */ 89 | printf("\nPolling for tags ...\n"); 90 | if ((tags = nfc_initiator_poll_target(dev, &mod, 1, 10, 0x01, &target)) < 0) { 91 | nfc_perror(dev, "nfc_initiator_poll_target"); 92 | err = EXIT_FAILURE; 93 | goto error; 94 | } 95 | printf("... %i tag%s found.\n", tags, tags==1?"":"s"); 96 | 97 | if (tags > 0) 98 | dump_girogo(); 99 | 100 | error: 101 | if (dev) 102 | nfc_close(dev); 103 | if (context) 104 | nfc_exit(context); 105 | return err; 106 | } 107 | 108 | static void dump_girogo(void) 109 | { 110 | uint8_t rx_buf[MAX_RX_LENGTH]; 111 | int rx; 112 | 113 | /* Select DF_BOERSE */ 114 | if ((rx = nfc_initiator_transceive_bytes(dev, DF_BOERSE, sizeof DF_BOERSE, rx_buf, MAX_RX_LENGTH, 1000)) < 0) { 115 | nfc_perror(dev, "nfc_initiator_transceive_bytes"); 116 | return; 117 | } 118 | if (rx >= 2 && rx_buf[0] == 0x90 && rx_buf[1] == 0x00) { 119 | printf("\nGeldKarte found!\n\n"); 120 | } else { 121 | printf("\nCard not supported!\n\n"); 122 | return; 123 | } 124 | 125 | /* EF_BETRAG */ 126 | if ((rx = nfc_initiator_transceive_bytes(dev, EF_BETRAG, sizeof EF_BETRAG, rx_buf, MAX_RX_LENGTH, 1000)) < 0) { 127 | nfc_perror(dev, "nfc_initiator_transceive_bytes"); 128 | return; 129 | } 130 | dump_buffer("EF_BETRAG", rx_buf, rx); 131 | 132 | /* EF_ID */ 133 | if ((rx = nfc_initiator_transceive_bytes(dev, EF_ID, sizeof EF_ID, rx_buf, MAX_RX_LENGTH, 1000)) < 0) { 134 | nfc_perror(dev, "nfc_initiator_transceive_bytes"); 135 | return; 136 | } 137 | dump_buffer("EF_ID", rx_buf, rx); 138 | 139 | /* EF_BOERSE */ 140 | if ((rx = nfc_initiator_transceive_bytes(dev, EF_BOERSE, sizeof EF_BOERSE, rx_buf, MAX_RX_LENGTH, 1000)) < 0) { 141 | nfc_perror(dev, "nfc_initiator_transceive_bytes"); 142 | return; 143 | } 144 | dump_buffer("EF_BOERSE", rx_buf, rx); 145 | } 146 | 147 | static void handler_sigint(int signal) 148 | { 149 | (void)signal; 150 | 151 | if (dev != NULL) { 152 | nfc_abort_command(dev); 153 | } else { 154 | if (context) 155 | nfc_exit(context); 156 | exit(EXIT_FAILURE); 157 | } 158 | } 159 | 160 | static void dump_buffer(char *buf_name, uint8_t *buf, int size) 161 | { 162 | printf("%s, length: %i\n\t", buf_name, size); 163 | for (int i = 0; i < size; ++i) 164 | printf("%02x%s", buf[i], i 26 | * [Yosys Manual](http://www.clifford.at/yosys/files/yosys_manual.pdf) 27 | 28 | Dialog Semiconductors Resources: 29 | * [GreenPAK](https://www.dialog-semiconductor.com/products/greenpak) 30 | 31 | Development Board (old name: GreenPAK 4 Dev Board): 32 | * [GreenPAK Advanced Development 33 | Board](https://www.dialog-semiconductor.com/products/greenpak/slg4dvkadv) 34 | 35 | Dialog Datasheets: 36 | * [SLG46140V](https://www.dialog-semiconductor.com/products/greenpak/slg46140): 37 | 38 | * [SLG46620](https://www.dialog-semiconductor.com/products/greenpak/slg46620): 39 | 40 | 41 | Go Configure Software Hub (includes GreenPAK Designer) 6.27.001: 42 | * Windows 32 bit: 43 | 44 | * Windows 64 bit: 45 | 46 | * Ubuntu 18.04/20.04 64 bit: 47 | 48 | * Debian-testing 32 bit: 49 | 50 | * Debian-testing 64 bit: 51 | 52 | * macOS: 53 | 54 | Ubuntu 20.04: 55 | 56 | Openfpga project: 57 | 58 | ``` 59 | openfpga/src/gpcosim/gpcosim.cpp:20:10: fatal error: vpi_user.h: No 60 | such file or directory 61 | 20 | #include 62 | | ^~~~~~~~~~~~ 63 | compilation terminated. 64 | make[2]: *** [src/gpcosim/CMakeFiles/gpcosim.dir/build.make:63: 65 | src/gpcosim/CMakeFiles/gpcosim.dir/gpcosim.cpp.o] Error 1 66 | make[1]: *** [CMakeFiles/Makefile2:582: 67 | src/gpcosim/CMakeFiles/gpcosim.dir/all] Error 2 68 | make: *** [Makefile:141: all] Error 2 69 | ``` 70 | 71 | Install Icarus Verilog: 72 | ```sh 73 | git clone git://github.com/steveicarus/iverilog.git 74 | sudo apt-get install build-essential autoconf gperf flex bison 75 | cd iverilog && sh autoconf.sh 76 | ./configure && make && sudo make install 77 | ``` 78 | 79 | ``` 80 | yosys -p 'read_verilog counter.v' \ 81 | -p 'synth_greenpak4 -part SLG46620V -json counter.json' 82 | make: yosys: Command not found 83 | make: *** [../../Makefile.common:9: counter.json] Error 127 84 | ``` 85 | 86 | Install Yosys: 87 | ```sh 88 | git clone https://github.com/YosysHQ/yosys.git 89 | sudo apt-get install build-essential clang bison flex \ 90 | libreadline-dev gawk tcl-dev libffi-dev git \ 91 | graphviz xdot pkg-config python3 libboost-system-dev \ 92 | libboost-python-dev libboost-filesystem-dev zlib1g-dev 93 | cd yosys && make && sudo make install 94 | ``` 95 | 96 | Compile counter example: 97 | ```sh 98 | cd primitives/counter_inference 99 | make 100 | 101 | ... 102 | 103 | 2.29. Executing JSON backend. 104 | 105 | End of script. Logfile hash: 7ad2fcd95c, CPU: user 0.13s system 0.03s, 106 | MEM: 16.83 MB peak 107 | Yosys 0.11+52 (git sha1 UNKNOWN, clang 10.0.0-4ubuntu1 -fPIC -Os) 108 | Time spent: 60% 4x abc (0 sec), 12% 7x read_verilog (0 sec), ... 109 | gp4par counter.json -o counter.txt -p SLG46620V 110 | GreenPAK 4 place-and-route by Andrew D. Zonenberg. 111 | 112 | License: LGPL v2.1+ 113 | This is free software: you are free to change and redistribute it. 114 | There is NO WARRANTY, to the extent permitted by law. 115 | 116 | Loading Yosys JSON file "counter.json". 117 | Netlist creator: Yosys 0.11+52 (git sha1 UNKNOWN, clang 10.0.0-4ubuntu1 -fPIC -O 118 | s) 119 | 120 | Loading modules... 121 | ERROR: module child should be of type object but isn't 122 | make: *** [../../Makefile.common:13: counter.txt] Segmentation fault 123 | (core dumped) 124 | ``` 125 | 126 | Versions: 127 | * Icarus Verilog: commit 71c36d1289873e9e9fb2699c1de1f22dee2021e6 128 | * Yosys: commit 2be110cb0ba645f95f62ee01b6a6fa46a85d5b26 129 | * openfpga: commit f9ae535d0372c246075d1bb05a409adaed0135e7 130 | -------------------------------------------------------------------------------- /coq/Basics.v: -------------------------------------------------------------------------------- 1 | (* Enumerated Type *) 2 | (*Inductive bool : Type := 3 | (* Constructors *) 4 | | false : bool 5 | | true : bool. 6 | 7 | Definition negb (b : bool) : bool := 8 | match b with 9 | | false => true 10 | | true => false 11 | end. 12 | 13 | (* Definition andb (b1 : bool) (b2 : bool) : bool := *) 14 | Definition andb (b1 b2 : bool) : bool := 15 | match b1 with 16 | | true => b2 17 | | false => false 18 | end. 19 | 20 | Infix "&&" := andb.*) 21 | 22 | (*********) 23 | (* nandb *) 24 | (*********) 25 | 26 | Definition nandb (b1 b2 : bool) : bool := 27 | negb (b1 && b2). 28 | 29 | Example ex_nandb1 : (nandb false false) = true. 30 | Proof. simpl. reflexivity. Qed. 31 | Example ex_nandb2 : (nandb false true ) = true. 32 | Proof. simpl. reflexivity. Qed. 33 | Example ex_nandb3 : (nandb true false) = true. 34 | Proof. simpl. reflexivity. Qed. 35 | Example ex_nandb4 : (nandb true true ) = false. 36 | Proof. simpl. reflexivity. Qed. 37 | 38 | (*********) 39 | (* andb3 *) 40 | (*********) 41 | 42 | Definition andb3 (b1 b2 b3 : bool) : bool := 43 | b1 && b2 && b3. 44 | 45 | Example ex_andb3_1 : (andb3 false false false) = false. 46 | Proof. simpl. reflexivity. Qed. 47 | Example ex_andb3_2 : (andb3 false false true ) = false. 48 | Proof. simpl. reflexivity. Qed. 49 | Example ex_andb3_3 : (andb3 false true false) = false. 50 | Proof. simpl. reflexivity. Qed. 51 | Example ex_andb3_4 : (andb3 false true true ) = false. 52 | Proof. simpl. reflexivity. Qed. 53 | Example ex_andb3_5 : (andb3 true false false) = false. 54 | Proof. simpl. reflexivity. Qed. 55 | Example ex_andb3_6 : (andb3 true false true ) = false. 56 | Proof. simpl. reflexivity. Qed. 57 | Example ex_andb3_7 : (andb3 true true false) = false. 58 | Proof. simpl. reflexivity. Qed. 59 | Example ex_andb3_8 : (andb3 true true true ) = true. 60 | Proof. simpl. reflexivity. Qed. 61 | 62 | (*************) 63 | (* factorial *) 64 | (*************) 65 | 66 | (* Fixpoint: recursive definition *) 67 | Fixpoint factorial (n : nat) : nat := 68 | match n with 69 | | O => 1 70 | (* | S n' => mult n (factorial n') *) 71 | | S n' => n * (factorial n') 72 | end. 73 | 74 | Example ex_factorial1 : (factorial 0) = 1. 75 | Proof. simpl. reflexivity. Qed. 76 | Example ex_factorial2 : (factorial 1) = 1. 77 | Proof. simpl. reflexivity. Qed. 78 | Example ex_factorial3 : (factorial 5) = 120. 79 | Proof. simpl. reflexivity. Qed. 80 | 81 | (***********) 82 | (* blt_nat *) 83 | (***********) 84 | 85 | Fixpoint be_nat (n m : nat) : bool := 86 | match n with 87 | | O => match m with 88 | | O => true 89 | | S m' => false 90 | end 91 | | S n' => match m with 92 | | O => true 93 | | S m' => be_nat n' m' 94 | end 95 | end. 96 | 97 | Fixpoint ble_nat (n m : nat) : bool := 98 | match n with 99 | | O => true 100 | | S n' => match m with 101 | | O => false 102 | | S m' => ble_nat n' m' 103 | end 104 | end. 105 | 106 | Definition blt_nat (n m : nat) : bool := 107 | (negb (be_nat n m)) && (ble_nat n m). 108 | 109 | Example ex_blt_nat_1 : (blt_nat 2 2) = false. 110 | Proof. simpl. reflexivity. Qed. 111 | Example ex_blt_nat_2 : (blt_nat 2 4) = true. 112 | Proof. simpl. reflexivity. Qed. 113 | Example ex_blt_nat_3 : (blt_nat 4 2) = false. 114 | Proof. simpl. reflexivity. Qed. 115 | 116 | (********************) 117 | (* plus_id_exercise *) 118 | (********************) 119 | 120 | (* -> "implies" *) 121 | 122 | Theorem plus_id_exercise : forall n m o : nat, 123 | n = m -> m = o -> n + m = m + o. 124 | 125 | Proof. 126 | (* Move n, m, o from goal to assumptions *) 127 | intros n m o. 128 | (* Move hypotheses into context *) 129 | intros H H'. 130 | (* Rewrite goal using hypotheses from left to right *) 131 | rewrite -> H. 132 | rewrite -> H'. 133 | (* Check both sides for equal values *) 134 | reflexivity. 135 | Qed. 136 | 137 | (************) 138 | (* mult_S_1 *) 139 | (************) 140 | 141 | Theorem mult_S_1 : forall n m : nat, 142 | m = S n -> 143 | m * (1 + n) = m * m. 144 | 145 | Proof. 146 | (* Move n, m into context *) 147 | intros n m. 148 | (* Move hypothesis into context *) 149 | intros H. 150 | rewrite -> H. 151 | reflexivity. 152 | Qed. 153 | 154 | (*******************) 155 | (* andb_true_elim2 *) 156 | (*******************) 157 | 158 | Theorem andb_true_elim2 : forall b c : bool, 159 | andb b c = true -> c = true. 160 | 161 | Proof. 162 | intros b. destruct b. 163 | - destruct c. 164 | + simpl. reflexivity. 165 | + simpl. intros H. rewrite -> H. reflexivity. 166 | - destruct c. 167 | + simpl. reflexivity. 168 | + simpl. intros H. rewrite -> H. reflexivity. 169 | Qed. 170 | 171 | (* Alternative: 172 | Proof. 173 | intros [] []. 174 | - simpl. intros H. rewrite -> H. reflexivity. 175 | - reflexivity. 176 | - simpl. intros H. rewrite -> H. reflexivity. 177 | - reflexivity. Qed.*) 178 | 179 | (********************) 180 | (* zero_nbeq_plus_1 *) 181 | (********************) 182 | 183 | Theorem zero_nbeq_plus_1 : forall n : nat, 184 | be_nat 0 (n + 1) = false. 185 | 186 | Proof. 187 | intros [|n]. 188 | - reflexivity. 189 | - simpl. reflexivity. 190 | Qed. 191 | 192 | (*********************) 193 | (* boolean_functions *) 194 | (*********************) 195 | 196 | Theorem identity_fn_applied_twice : 197 | forall (f : bool -> bool), 198 | (forall (x : bool), f x = x) -> 199 | forall (b : bool), f (f b) = b. 200 | 201 | Proof. 202 | intros f H. 203 | intros []. 204 | - rewrite -> H. rewrite -> H. reflexivity. 205 | - rewrite -> H. rewrite -> H. reflexivity. 206 | Qed. 207 | 208 | Theorem negation_fn_applied_twice : 209 | forall (f : bool -> bool), 210 | (forall (x : bool), f x = negb x) -> 211 | forall (b : bool), f (f b) = b. 212 | 213 | Proof. 214 | intros f H. 215 | intros []. 216 | - rewrite -> H. rewrite -> H. reflexivity. 217 | - rewrite -> H. rewrite -> H. reflexivity. 218 | Qed. 219 | 220 | (***************) 221 | (* andb_eq_orb *) 222 | (***************) 223 | 224 | Theorem andb_eq_orb : 225 | forall (b c : bool), 226 | (andb b c = orb b c) -> 227 | b = c. 228 | 229 | Proof. 230 | intros b c. 231 | destruct b. 232 | - simpl. intros H. rewrite -> H. reflexivity. 233 | - simpl. intros H. rewrite -> H. reflexivity. 234 | Qed. 235 | 236 | (**********) 237 | (* binary *) 238 | (**********) -------------------------------------------------------------------------------- /sds011/sds011.c: -------------------------------------------------------------------------------- 1 | /* 2 | SDS011 dust sensor 3 | 4 | Compile with: 5 | ------------- 6 | make 7 | 8 | References: 9 | ----------- 10 | * https://web.archive.org/web/20160705112431/http://inovafitness.com/software/SDS011%20laser%20PM2.5%20sensor%20specification-V1.3.pdf 11 | * https://github.com/ryszard/sds011/blob/master/doc/Protocol_V1.3.docx 12 | * http://www.tldp.org/HOWTO/Serial-Programming-HOWTO/ 13 | * https://www.cmrr.umn.edu/~strupp/serial.html 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | static const bool debug = false; 31 | 32 | /* size of data packet */ 33 | #define MSG_SIZE 10 34 | /* message header */ 35 | #define MSG_HDR 0xaa 36 | /* data command */ 37 | #define MSG_DATA 0xc0 38 | /* message tail */ 39 | #define MSG_TAIL 0xab 40 | 41 | /* offsets in data packets */ 42 | enum { 43 | OFF_HDR = 0, /* message header */ 44 | OFF_CMD, /* command */ 45 | OFF_DATA1, /* PM 2.5 low byte */ 46 | OFF_DATA2, /* PM 2.5 high byte */ 47 | OFF_DATA3, /* PM 10 low byte */ 48 | OFF_DATA4, /* PM 10 high byte */ 49 | OFF_DATA5, /* ID byte 1 */ 50 | OFF_DATA6, /* ID byte 2 */ 51 | OFF_CHK, /* checksum = DATA1 + DATA2 + ... + DATA6 */ 52 | OFF_TAIL /* message tail */ 53 | }; 54 | 55 | /* directory for log files */ 56 | #define LOGDIR "/home/pi/sds011/logs" 57 | 58 | int main(int argc, char *argv[]) 59 | { 60 | int fd, ret = EXIT_SUCCESS; 61 | struct termios opt; 62 | 63 | if (argc < 2) { 64 | fprintf(stderr, "Usage: %s /dev/ttyUSBx\n", argv[0]); 65 | ret = EXIT_FAILURE; 66 | goto error; 67 | } 68 | 69 | /* process isn't a controlling terminal, ignore DCD state */ 70 | if ((fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY)) == -1) { 71 | perror("Error opening serial port"); 72 | ret = EXIT_FAILURE; 73 | goto error; 74 | } 75 | 76 | /* we want read calls to block */ 77 | if (fcntl(fd, F_SETFL, 0) == -1) { 78 | perror("fcntl"); 79 | ret = EXIT_FAILURE; 80 | goto error; 81 | } 82 | 83 | /* get settings for serial port */ 84 | if (tcgetattr(fd, &opt) == -1) { 85 | perror("tcgetattr"); 86 | ret = EXIT_FAILURE; 87 | goto error; 88 | } 89 | 90 | /* 8 data bits */ 91 | opt.c_cflag &= ~CSIZE; 92 | opt.c_cflag |= CS8; 93 | /* 1 stop bit */ 94 | opt.c_cflag &= ~CSTOPB; 95 | /* enable receiver */ 96 | opt.c_cflag |= CREAD; 97 | /* disable parity */ 98 | opt.c_cflag &= ~PARENB; 99 | /* local line */ 100 | opt.c_cflag |= CLOCAL; 101 | /* disable hardware control flow */ 102 | #ifdef CNEW_RTSCTS 103 | opt.c_cflag &= ~CNEW_RTSCTS; 104 | #endif 105 | 106 | /* set baud rate to 9600 */ 107 | if (cfsetispeed(&opt, B9600) == -1 || cfsetospeed(&opt, B9600) == -1) { 108 | perror("cfsetispeed/cfsetospeed"); 109 | ret = EXIT_FAILURE; 110 | goto error; 111 | } 112 | 113 | /* raw input */ 114 | opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 115 | /* no software flow control */ 116 | opt.c_iflag &= ~(IXON | IXOFF | IXANY); 117 | 118 | /* raw output */ 119 | opt.c_oflag &= ~OPOST; 120 | 121 | /* receive at least PACKET_SIZE bytes with read, no timer */ 122 | opt.c_cc[VMIN] = MSG_SIZE; 123 | opt.c_cc[VTIME] = 0; 124 | 125 | /* apply settings to serial port and flush input/output buffers */ 126 | if (tcsetattr(fd, TCSAFLUSH, &opt) == -1) { 127 | perror("tcsetattr"); 128 | ret = EXIT_FAILURE; 129 | goto error; 130 | } 131 | 132 | uint8_t buf[MSG_SIZE]; 133 | FILE *log = NULL; 134 | 135 | while (read(fd, buf, MSG_SIZE) > 0) { 136 | /* calculate checksum */ 137 | uint8_t chksum = 0; 138 | for (int i = OFF_DATA1; i <= OFF_DATA6; ++i) 139 | chksum += buf[i]; 140 | /* check if message is valid, otherwise discard it 141 | and flush input buffer */ 142 | if (buf[OFF_HDR] == MSG_HDR && buf[OFF_CMD] == MSG_DATA && 143 | buf[OFF_CHK] == chksum && buf[OFF_TAIL] == MSG_TAIL) { 144 | /* PM 2.5 (ug/m^3) */ 145 | float pm25 = (buf[OFF_DATA2]<<8 | buf[OFF_DATA1])/10.f; 146 | /* PM 10 (ug/m^3) */ 147 | float pm10 = (buf[OFF_DATA4]<<8 | buf[OFF_DATA3])/10.f; 148 | if (debug) 149 | printf("PM 2.5: %f, PM 10: %f\n", pm25, pm10); 150 | 151 | /* get time and date */ 152 | time_t unix_time = time(NULL); 153 | struct tm *t = localtime(&unix_time); 154 | struct stat st; 155 | char logname[strlen(LOGDIR)+1+strlen("yyyy-mm-dd")+1]; 156 | 157 | snprintf(logname, sizeof logname, "%s/%04d-%02d-%02d", 158 | LOGDIR, 159 | t->tm_year + 1900, t->tm_mon + 1, t->tm_mday); 160 | 161 | /* append to logfile if it already exists */ 162 | if (log == NULL && stat(logname, &st) == 0) 163 | log = fopen(logname, "a"); 164 | /* create new logfile at midnight */ 165 | if (stat(logname, &st) == -1) { 166 | /* close old logfile */ 167 | if (log) 168 | (void)fclose(log); 169 | if ((log = fopen(logname, "w"))) 170 | fprintf(log, "# hh:mm:ss" 171 | "\tPM2.5 / ug/m^3" 172 | "\tPM10 / ug/m^3\n"); 173 | } 174 | 175 | if (log) 176 | fprintf(log, "%02i:%02i:%02i\t%f\t%f\n", 177 | t->tm_hour, t->tm_min, t->tm_sec, 178 | pm25, pm10); 179 | } else { 180 | tcflush(fd, TCIFLUSH); 181 | if (debug) { 182 | /* PM 2.5 (ug/m^3) */ 183 | float pm25 = (buf[OFF_DATA2]<<8 | buf[OFF_DATA1])/10.f; 184 | /* PM 10 (ug/m^3) */ 185 | float pm10 = (buf[OFF_DATA4]<<8 | buf[OFF_DATA3])/10.f; 186 | 187 | printf("PM 2.5: %f, PM 10: %f\n", pm25, pm10); 188 | fprintf(stderr, "header : %s\n", 189 | buf[OFF_HDR]==MSG_HDR?"pass":"fail"); 190 | fprintf(stderr, "cmd : %s\n", 191 | buf[OFF_CMD]==MSG_DATA?"pass":"fail"); 192 | fprintf(stderr, "checksum: %s\n", 193 | buf[OFF_CHK]==chksum?"pass":"fail"); 194 | if (buf[OFF_CHK] != chksum) 195 | fprintf(stderr, "checksum: 0x%02x, " 196 | "expected: 0x%02x\n", 197 | buf[OFF_CHK], chksum); 198 | fprintf(stderr, "tail : %s\n", 199 | buf[OFF_TAIL]==MSG_TAIL?"pass":"fail"); 200 | } 201 | } 202 | } 203 | 204 | error: 205 | return ret; 206 | } 207 | -------------------------------------------------------------------------------- /nbody/nbody/kernel.cu: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* check for CUDA error */ 5 | #define CHECK_ERROR check_cuda_error(__LINE__-1, __FILE__) 6 | 7 | /* #bodies */ 8 | static int N; 9 | 10 | /* #threads/block (leapfrog) */ 11 | static int TPB = 128; 12 | 13 | /* #tiles (acceleration kernel) */ 14 | static int P; 15 | 16 | /* #timesteps */ 17 | static int TIMESTEPS = 1000; 18 | /* softening factor (square), G, \Delta t */ 19 | static const float EPS = 0.1f, G = 2.f, DELTA_T = 0.01f; 20 | 21 | /* acceleration */ 22 | __device__ float4 *a; 23 | /* x,y,z: position; w: mass */ 24 | static float4 *r_host; 25 | __device__ float4 *r; 26 | /* velocity */ 27 | static float4 *v_host; 28 | __device__ float4 *v; 29 | 30 | /* random number in [0,1] */ 31 | static inline float rnd() 32 | { 33 | return (float)rand() / RAND_MAX; 34 | } 35 | 36 | /* check for CUDA error */ 37 | static void check_cuda_error(const int line, const char *file) 38 | { 39 | cudaError_t e; 40 | 41 | e = cudaGetLastError(); 42 | if (e != cudaSuccess) { 43 | printf("CUDA error: %s, line %i, file '%s'\n", 44 | cudaGetErrorString(e), line, file); 45 | exit(1); 46 | } 47 | } 48 | 49 | /* leap frog integration kernel (1 particle/thread) */ 50 | __global__ void leap_frog_1p_2(float4 *a, float4 *v, float4 *r, float delta_t) 51 | { 52 | int i = threadIdx.x + __mul24(blockIdx.x, blockDim.x); 53 | float3 v_tmp; 54 | 55 | v_tmp.x = v[i].x; 56 | v_tmp.y = v[i].y; 57 | v_tmp.z = v[i].z; 58 | 59 | v_tmp.x += a[i].x * delta_t; 60 | v_tmp.y += a[i].y * delta_t; 61 | v_tmp.z += a[i].z * delta_t; 62 | 63 | r[i].x += v_tmp.x * delta_t; 64 | r[i].y += v_tmp.y * delta_t; 65 | r[i].z += v_tmp.z * delta_t; 66 | 67 | v[i] = make_float4(v_tmp.x, v_tmp.y, v_tmp.z, 0.f); 68 | } 69 | 70 | /* body-body interaction, returns a_i */ 71 | __device__ float3 interaction(float3 ri, float4 rj, float eps) 72 | { 73 | float3 rij, ai; 74 | float dst_sqr, cube, inv_sqrt; 75 | 76 | /* distance vector */ 77 | rij.x = rj.x - ri.x; 78 | rij.y = rj.y - ri.y; 79 | rij.z = rj.z - ri.z; 80 | 81 | /* compute acceleration */ 82 | dst_sqr = rij.x*rij.x + rij.y*rij.y + rij.z*rij.z + eps; 83 | cube = dst_sqr * dst_sqr * dst_sqr; 84 | inv_sqrt = rsqrtf(cube) * rj.w; 85 | 86 | /* acceleration a_i */ 87 | ai.x = rij.x * inv_sqrt; 88 | ai.y = rij.y * inv_sqrt; 89 | ai.z = rij.z * inv_sqrt; 90 | 91 | return ai; 92 | } 93 | 94 | /* calculate accelerations */ 95 | __global__ void acc(float4 *r, float4 *a, float eps, float g) 96 | { 97 | /* dynamically allocated shared memory */ 98 | extern __shared__ float4 shared[]; 99 | /* acceleration a_i */ 100 | float3 ai = make_float3(0.f, 0.f, 0.f), tmp; 101 | /* position particle i */ 102 | float3 ri; 103 | /* particle i */ 104 | int i = threadIdx.x + __mul24(blockIdx.x, blockDim.x); 105 | int k, l; 106 | 107 | /* get position of particle i */ 108 | ri.x = r[i].x; 109 | ri.y = r[i].y; 110 | ri.z = r[i].z; 111 | 112 | /* loop over tiles */ 113 | for (k = 0; k < gridDim.x; ++k) { 114 | /* load position and mass into shared memory */ 115 | shared[threadIdx.x] = r[__mul24(k, blockDim.x) + threadIdx.x]; 116 | __syncthreads(); 117 | 118 | /* loop over particles in a tile */ 119 | #pragma unroll 32 120 | for (l = 0; l < blockDim.x; ++l) { 121 | tmp = interaction(ri, shared[l], eps); 122 | ai.x += tmp.x; 123 | ai.y += tmp.y; 124 | ai.z += tmp.z; 125 | } 126 | 127 | /* wait for other threads to finish calculation */ 128 | __syncthreads(); 129 | } 130 | 131 | /* save acceleration a_i in global memory */ 132 | a[i] = make_float4(ai.x*g, ai.y*g, ai.z*g, 0.f); 133 | } 134 | 135 | void init(); 136 | 137 | int main(int argc, char *argv[]) 138 | { 139 | cudaEvent_t start, stop; 140 | float time; 141 | int i, timestep; 142 | 143 | if (argc < 2) { 144 | printf("usage: nbody -N#bodies [-T#threads/block] [-S#timesteps] -P#tiles\n"); 145 | exit(1); 146 | } 147 | 148 | /* get command line parameters */ 149 | for (i = 1; i < argc; ++i) { 150 | if (argv[i][0] == '-') { 151 | switch (argv[i][1]) { 152 | case 'N': 153 | N = atoi(argv[i]+2); 154 | break; 155 | case 'T': 156 | TPB = atoi(argv[i]+2); 157 | break; 158 | case 'S': 159 | TIMESTEPS = atoi(argv[i]+2); 160 | break; 161 | case 'P': 162 | P = atoi(argv[i]+2); 163 | break; 164 | default: 165 | break; 166 | } 167 | } 168 | } 169 | 170 | /*printf("N: %i, TPB: %i, TIMESTEPS: %i, P: %i\n", N, TPB, TIMESTEPS, P);*/ 171 | 172 | if (N % TPB) { 173 | printf("#bodies must be a multiple of #threads/block!\n"); 174 | exit(1); 175 | } 176 | 177 | if (N % P) { 178 | printf("#bodies must be a multiple of #p!\n"); 179 | exit(1); 180 | } 181 | 182 | /* alloc host memory */ 183 | r_host = (float4 *)malloc(N*sizeof(float4)); 184 | v_host = (float4 *)malloc(N*sizeof(float4)); 185 | /* alloc device memory */ 186 | cudaMalloc((void **)&a, N*sizeof(float4)); 187 | cudaMalloc((void **)&r, N*sizeof(float4)); 188 | cudaMalloc((void **)&v, N*sizeof(float4)); 189 | CHECK_ERROR; 190 | 191 | /* generate initial configuration */ 192 | srand(1); 193 | init(); 194 | 195 | /* copy config to device memory */ 196 | cudaMemcpy(r, r_host, N*sizeof(float4), cudaMemcpyHostToDevice); 197 | cudaMemcpy(v, v_host, N*sizeof(float4), cudaMemcpyHostToDevice); 198 | CHECK_ERROR; 199 | 200 | /* start counter */ 201 | cudaEventCreate(&start); 202 | cudaEventCreate(&stop); 203 | cudaEventRecord(start, 0); 204 | 205 | /* integration steps */ 206 | for (timestep = 0; timestep < TIMESTEPS; ++timestep) { 207 | /* update accelerations */ 208 | acc<<>>(r, a, EPS, G); 209 | 210 | /* leap frog */ 211 | leap_frog_1p_2<<>>(a, v, r, DELTA_T); 212 | /*cudaMemcpy(r_host, r, N * sizeof(float3), cudaMemcpyDeviceToHost); 213 | printf("#1: x: %f, y: %f, z: %f\n", r_host[0].x, r_host[0].y, r_host[0].z); 214 | printf("#2: x: %f, y: %f, z: %f\n", r_host[1].x, r_host[1].y, r_host[1].z);*/ 215 | } 216 | 217 | /* stop counter */ 218 | cudaEventRecord(stop, 0); 219 | cudaEventSynchronize(stop); 220 | /* unit: milliseconds */ 221 | cudaEventElapsedTime(&time, start, stop); 222 | CHECK_ERROR; 223 | /*printf("elapsed time: %f\n", time); 224 | printf("#interactions/s: %f\n", ((float)TIMESTEPS*N*N) / time * 1000);*/ 225 | printf("%f\n", ((float)TIMESTEPS*N*N) / time * 1000); 226 | 227 | /* free host memory */ 228 | free(r_host); 229 | free(v_host); 230 | /* free device memory */ 231 | cudaFree(a); 232 | cudaFree(r); 233 | cudaFree(v); 234 | CHECK_ERROR; 235 | 236 | return 0; 237 | } 238 | 239 | /* generate initial configuration */ 240 | void init() 241 | { 242 | int i; 243 | 244 | for (i = 0; i < N; ++i) { 245 | /* mass */ 246 | r_host[i].w = rnd()>0.5 ? 1.f : 10.f; 247 | 248 | /* velocity */ 249 | v_host[i].x = 3.f; 250 | v_host[i].y = rnd() * 10.f; 251 | v_host[i].z = -5.f; 252 | 253 | /* position */ 254 | r_host[i].x = rnd() * 50.f; 255 | r_host[i].y = rnd() * 50.f; 256 | r_host[i].z = rnd() * 50.f; 257 | } 258 | } 259 | --------------------------------------------------------------------------------