This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Stateless, purely combinational sin(x) look-up table. Can be used as the phase-to-amplitude converter of an NCO. | |
/// It stores 256 9-bit items in a look-up table representing the first quarter of the wave. | |
/// The input x is expected to be normalized into [0, 1024) that maps to [0, 2 pi). | |
/// The signed output is normalized into [-511, +511] (sic! -512 not used). | |
module sine_lookup_1024( | |
input wire [9:0] x, | |
output wire signed [9:0] out | |
); | |
reg [8:0] entry; // Not an actual register, just a wire assignable from the always block. | |
assign out = x[9] ? -$signed({1'b0, entry}) : $signed({1'b0, entry}); // The MSb represents the half-wave index. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A numerically controlled oscillator (NCO) that outputs a sawtooth wave, whose frequency is a function of | |
/// clk rate and frequency_control_word, and the amplitude spans the range [0, 2**OUTPUT_WIDTH). | |
/// The output frequency is defined as: | |
/// | |
/// f_out = (f_clk * frequency_control_word) / (2**PHASE_ACCUMULATOR_WIDTH) | |
/// | |
/// Solve for frequency_control_word: | |
/// | |
/// frequency_control_word = ((2**PHASE_ACCUMULATOR_WIDTH) * f_out) / f_clk | |
/// |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <cstdint> | |
#include <cstdlib> | |
namespace stack_canary | |
{ | |
/// Do not use this directly; see the StackCanary definition below. | |
template <std::size_t n_bytes, std::uint8_t seed = 0xC9> | |
class StackCanary_Impl final |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script will disable most of the Windows security-related features. | |
# It is mostly intended for use in disposable VMs, such as simulation and CI/CD runners. | |
# Read the source to see what exactly is done. | |
# Author: Pavel Kirienko <[email protected]> | |
# Relaunch elevated if needed | |
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent() | |
).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) | |
if (-not $IsAdmin) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
ln -fs /dev/serial/by-id/usb-*Black*Magic*-if00 .bmp.tmp | |
# Replace arm-zephyr-eabi-gdb with the correct toolchain prefix. | |
# Update the end address depending on the SRAM size. | |
arm-zephyr-eabi-gdb --batch -ex 'tar ext .bmp.tmp' -ex 'mon swdp_scan' -ex 'attach 1' -ex 'dump bin mem ram.bin 0x20000000 0x20008000' | |
# To read all Cortex-M4 core peripheral registers: | |
# mem 0xE000E000 0xE000F000 rw 32 # This is not necessary if "set mem inaccessible-by-default off" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
A simple script for extracting numerical data from plots: load an image and click on it to record point coordinates. | |
Usage example: | |
./trace_image.py my_plot.jpg | |
Copyright (C) 2025 Pavel Kirienko <[email protected]> | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Pseudo-inverse of a matrix. There may be a more efficient solution for square matrices; this is a general one. | |
/// Tolerance less than epsilon*10 may cause numerical instability. | |
template <auto R, auto C, typename S> | |
[[nodiscard]] Eigen::Matrix<S, C, R> pinv(const Eigen::Matrix<S, R, C>& a, const S tolerance = std::numeric_limits<S>::epsilon() * 10) | |
{ | |
const Eigen::JacobiSVD<Eigen::Matrix<S, R, C>> svd(a, Eigen::ComputeFullU | Eigen::ComputeFullV); | |
Eigen::Matrix<S, C, R> si; | |
si.setZero(); | |
for (int i = 0; i < svd.singularValues().size(); ++i) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# https://gist.github.com/pavel-kirienko/32e395683e8b7f49e71413aebf5e1a89 | |
# Pavel Kirienko <[email protected]> | |
HELP="Register slcan-enabled serial-to-CAN adapters as network interfaces. | |
Usage: | |
`basename $0` [options] <tty0> [[options] <tty1> ...] | |
Interface indexes will be assigned automatically in ascending order, i.e., the | |
first device will be mapped to slcan0, second to slcan1, and so on. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Bulk usage: | |
# find . -name '*.[ch]pp' -exec bash -c 'recomment.py < "{}" > "{}.out" ; mv "{}.out" "{}"' \; | |
import sys, re | |
RE_REPLACE = re.compile(r"(?m)^(\s*?) ?\*(.*)") | |
def replace_one(match) -> str: | |
use_doc = bool(match[1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Capture high-frame-rate, high-resolution video of the desktop using ffmpeg. | |
# The key options are: -c:v libx264 -f flv -vb 1000M -deadline realtime -cpu-used 12 | |
# Without these, the output video peaks at ~5 fps or so. | |
ffmpeg -video_size 3840x2080 -f x11grab -i :0.0+3840,0 -c:v libx264 -f flv -vb 1000M -deadline realtime -cpu-used 12 output.mp4 -y |
NewerOlder