Skip to content

Instantly share code, notes, and snippets.

View pavel-kirienko's full-sized avatar
:shipit:

Pavel Kirienko pavel-kirienko

:shipit:
View GitHub Profile
@pavel-kirienko
pavel-kirienko / dump-sram.sh
Last active March 27, 2025 15:05
Dump SRAM into a file via Black Magic Probe with GDB
#!/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"
@pavel-kirienko
pavel-kirienko / disable_windows_security.ps1
Created May 11, 2025 21:44
A script that disables Windows security features: Defender, Firewall, UAC, SmartScreen, Windows Update, etc.
# 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) {
@pavel-kirienko
pavel-kirienko / stack_canary.hpp
Created May 26, 2025 19:56
A simple utility class for debugging stack memory corruption.
#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
@pavel-kirienko
pavel-kirienko / nco.v
Created August 6, 2025 12:33
A simple numerically controlled oscillator (NCO) in Verilog
/// 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
///
@pavel-kirienko
pavel-kirienko / sine_lookup_1024.v
Created August 6, 2025 17:17
10-bit sin(x) lookup table in Verilog based on a 256x9-bit ROM cell
/// 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.