Skip to content

Instantly share code, notes, and snippets.

@tmk

tmk/csv2vcd.py Secret

Created March 21, 2022 11:31
Show Gist options
  • Save tmk/89dd2241a4410ddc31abd439a8389954 to your computer and use it in GitHub Desktop.
Save tmk/89dd2241a4410ddc31abd439a8389954 to your computer and use it in GitHub Desktop.
Convert Saleae CSV into sigrok pulseview VCD file format
#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Converts CSV into VCD format
# https://github.com/10110111/usb2ps2conv/issues/3#issuecomment-1073074094
#
# Time [s],Channel 1,Channel 1,Channel 2,Channel 3,Channel 4,Channel 5,Channel 6,Channel 7
# 1.000000000,1,1,1,1,1,1,1,1
# 3.325394000,0,0,1,1,1,1,1,1
# 3.325407000,0,1,1,1,1,1,1,1
# 3.325460000,0,0,1,1,1,1,1,1
# 3.325599000,0,1,1,1,1,1,1,1
#
import sys
# https://github.com/westerndigitalcorporation/pyvcd
from vcd import VCDWriter
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} infile")
sys.exit(1)
"""
# read csv file
line_no = 0
with open(sys.argv[1], "r") as file:
for line in file:
line_no += 1
#print(line.strip())
record = line.strip().split(",")
#print(record)
try:
time = int(float(record[0]) * 1000000) # nano sec
logic0 = int(record[1], 10)
logic1 = int(record[2], 10)
print(time, logic0, logic1)
except ValueError:
print("Ignore line {}: {}".format(line_no, line.strip()), file=sys.stderr)
sys.exit(1)
"""
# write VCD
line_no = 0
from datetime import datetime
with VCDWriter(sys.stdout, timescale='1 us', date=datetime.utcnow().ctime()) as writer:
# prams: scope, name, var_type, size
port = writer.register_var('csv', 'D', 'wire', size=8)
# read csv file
with open(sys.argv[1], "r") as file:
for line in file:
line_no += 1
#print(line.strip())
record = line.strip().split(",")
#print(record)
try:
time = int(float(record[0]) * 1000000) # sec -> micro sec
logic0 = int(record[1], 10)
logic1 = int(record[2], 10)
logic2 = int(record[3], 10)
logic3 = int(record[4], 10)
logic4 = int(record[5], 10)
logic5 = int(record[6], 10)
logic6 = int(record[7], 10)
logic7 = int(record[8], 10)
stat = logic7 << 7 | logic6 << 6 | logic5 << 5 | logic4 << 4 | logic3 << 3 | logic2 << 2 | logic1 << 1 | logic0
writer.change(port, time, stat)
except ValueError:
print("Ignore line {}: {}".format(line_no, line.strip()), file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment