CTF/0ctf/EzLogic/run.py

62 lines
1.9 KiB
Python

import re
import math
from datetime import datetime
def parse_time(ts):
microseconds = int((ts - ts.microsecond) * 1e6)
return datetime.now().timestamp() * 1e6 + microseconds
def read_vcd(filename, data_string):
with open(filename, 'r') as f:
lines = [line for line in f]
time_re = re.compile(r"(\d{3}):(\d{2}:(\d{2}.\d{6})|(\d{6}))")
times = []
data = {}
waveform = {}
for line in lines:
match = time_re.match(line)
if not match:
continue
time = parse_time(datetime.strptime(match.group(1), "%H:%M:%S"))
times.append(time)
waveform_name = match.group(5) if match.group(5) else match.group(4)
data[waveform_name] = []
for line in lines:
match = time_re.match(line)
if not match:
continue
time = parse_time(datetime.strptime(match.group(1), "%H:%M:%S"))
if time < min(times):
continue
waveform_name = match.group(5) if match.group(5) else match.group(4)
value = int(line.split()[2])
data[waveform_name].append((time, value))
# Extract the required data bits from the waveform
data_bits = []
for time, value in data["data_out"]:
if int(time) <= len(data_string) * 1e9:
data_bits.append(value)
# Reverse the order of the bits to match the hexadecimal string format
data_bits.reverse()
return times, data["data_out"], data_bits
if __name__ == "__main__":
filename = "shift_register.vcd"
data_string = "30789d5692f2fe23bb2c5d9e16406653b6cb217c952998ce17b7143788d949952680b4bce4c30a96c753"
times, data_out, data_bits = read_vcd(filename, data_string)
# Print the hexadecimal string and the corresponding waveform
print("Hexadecimal String:", data_string.upper())
for i in range(len(times)):
print("Time: {}\nData output: {}".format(datetime.fromtimestamp(times[i] / 1e6), " ".join(map(str, data_bits[i]))))