Skip to content

Instantly share code, notes, and snippets.

@GaryLee
GaryLee / ReadOnlyMergedDict
Created January 23, 2026 00:15
A read-only merged dictionary class that can merge multiple dict-like objects.
import collections.abc
from typing import Any, Dict, Iterator, Union
class ReadOnlyMergedDict(collections.abc.Mapping):
"""
A read-only merged dictionary class that can merge multiple dict-like objects
"""
def __init__(self, *dicts: Dict[Any, Any]):
"""
@GaryLee
GaryLee / signed_add_signed.sv
Created January 18, 2026 08:51
Added two signed signal and check if the result is overflow or underflow.
/// Signed extension macro. The add_width must be larger than 1.
`define SIGNEXT(signal, from_width, add_width) {{(add_width){signal[(from_width)-1]}}, signal}
/// Unsigned extension macro. The add_width must be larger than 1.
`define ZEROEXT(signal, from_width, add_width) {{(add_width){1'b0}}, signal}
module value_adder #(BITS=16) (
input logic [BITS-1:0] a, // Unsigned.
input logic [BITS-1:0] b, // Signed.
output logic [BITS-1:0] c,
@GaryLee
GaryLee / unsigned_add_signed.sv
Last active January 18, 2026 08:46
Add a unsigned and signed value. Check if the result if overflow or underflow.
/// Signed extension macro. The add_width must be larger than 1.
`define SIGNEXT(signal, from_width, add_width) {{(add_width){signal[(from_width)-1]}}, signal}
/// Unsigned extension macro. The add_width must be larger than 1.
`define ZEROEXT(signal, from_width, add_width) {{(add_width){1'b0}}, signal}
module value_adder #(BITS=16) (
input logic [BITS-1:0] a, // Unsigned.
input logic [BITS-1:0] b, // Signed.
output logic [BITS-1:0] c,
@GaryLee
GaryLee / codegen.py
Last active January 10, 2026 13:38
A codegen tool which can put the generating code in comment.
#!python
# coding: utf-8
"""
Utility functions for comment-based code generation.
Single line code generation:
- Lines starting with the specified print symbol (default: '//? ') within the block are treated as f-string of Python.
- The leading spaces of f-string lines is for indentation in Python code. Not the generated code.
- The generated code is inserted into the output at the position of the code generation block.
@GaryLee
GaryLee / remove_duplicate_path.sh
Created December 25, 2025 00:17
Re-export the PATH, removing duplicate paths.
# Following line can be added to your .?shrc file.
export PATH=`python -c "print(':'.join(dict.fromkeys('''$PATH'''.split(':'))))"`
@GaryLee
GaryLee / drawio-export.py
Created December 18, 2025 07:47
A python script to list the pages of drawio file and export the specified pages.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from pathlib import Path
import platform
import xml.etree.ElementTree as ET
# Define the output figure format. Could be 'svg', 'png', 'pdf', etc.
// Problem: https://hdlbits.01xz.net/wiki/Fsm_serialdp.
module top_module(
input clk,
input in,
input reset, // Synchronous reset
output [7:0] out_byte,
output done
); //
parameter START = 4'd0, BIT0 = 4'd1, BIT1 = 4'd2, BIT2 = 4'd3, BIT3 = 4'd4,
BIT4 = 4'd5, BIT5 = 4'd6, BIT6 = 4'd7, BIT7 = 4'd8, PARITY = 4'd9,
// Problem: https://hdlbits.01xz.net/wiki/Fsm_serialdata
module top_module(
input clk,
input in,
input reset, // Synchronous reset
output [7:0] out_byte,
output done
); //
parameter START = 4'd0, BIT0 = 4'd1, BIT1 = 4'd2, BIT2 = 4'd3, BIT3 = 4'd4,
BIT4 = 4'd5, BIT5 = 4'd6, BIT6 = 4'd7, BIT7 = 4'd8, STOP = 4'd9,
module counter #(parameter BGN, parameter END, parameter LOAD=0) (
input clk,
input reset,
input ena,
output c,
output [7:0] d
);
logic c0, c1;
assign c0 = (d[3:0] == 4'd9);
assign c = ena & (d == 8'(END)); // AND `ena` here to make sure `c` is set when previous digit is counting.
@GaryLee
GaryLee / ece241_2014_q1c.sv
Created May 13, 2025 06:52
The answer of HDLBits
module top_module (
input [7:0] a,
input [7:0] b,
output [7:0] s,
output overflow
);
logic c;
assign {c, s} = a + b;
assign overflow = ~(a[7] ^ b[7]) & (c ^ s[7]);
endmodule