-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgaldraw.py
More file actions
executable file
·525 lines (448 loc) · 16.8 KB
/
galdraw.py
File metadata and controls
executable file
·525 lines (448 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env python3
# Drawing parameters for the Galois LFSR visualization
# Note: All parameters are independent to provide flexibility in customization
# For example: you can adjust line thickness or box sizes independently
# Background color
backgroundColor = "white"
# Border size around the figure in mm
borderSize = 15
# Line widths
lineWidth = "0.1" # Standard line width for boxes and connections
circleLineWidth = "0.12" # Slightly thicker for XOR circles
arrowHead = "0.5"
# Size of the LFSR box cm (adjusted based on length)
def get_box_size(length):
if length <= 8:
return 4
elif length <= 16:
return 2.5
else:
return 2
# box Color use Tikz format
boxColor = "{{rgb:black,0;white,5}}"
# used for the distance of the box variable names
boxBelowTextDistance = 6 # Default, will be adjusted dynamically
# size of the x-or drawing cm
xorSize = 1
# Get font size based on box size
def get_font_size(box_size):
scale = box_size / 4.0
return f"{int(30 * scale)}pt"
# Global variables for drawing
drawInitValues = True # Show values in boxes
printBoxNames = True # Show box names
# These will be set based on LFSR length
boxSize = 4 # Default box size
xorDistance = 2 # Default XOR distance
BoxValuesFontSize = "30pt" # Default font sizes
BoxIndexFontSize = "20pt"
# Starting position of the first box
x = 25
y = 20
# Distance the feedback line extends to the left (will be set based on box size)
leftFeedbackDistance = 8
# LaTeX preamble for standalone TikZ
def printPreample():
print(
"\\documentclass[magick={density=300,outext=.png},tikz]{standalone}" # Removed fixed size
)
print("\\usepackage{xcolor}")
print("\\usepackage{scalerel}")
print("\\usepackage{amsmath}")
print("\\usetikzlibrary{arrows.meta,backgrounds}")
print(
"\\tikzset{{white background/.style={{show background rectangle,tight background,background rectangle/.style={{fill={0} }} }} }}".format(
backgroundColor
)
)
print("")
print("\\begin{document}")
print("\\begin{tikzpicture}[white background]\n\n")
# LaTeX end
def printPrologue():
# https://tex.stackexchange.com/a/596158/62865
print(
"\\path (current bounding box.north east) +({0}mm,{0}mm) (current bounding box.south west) +(-{0}mm,-{0}mm);".format(
borderSize
)
)
print("\n\\end{tikzpicture}")
print("\\end{document}")
# Prints the Galois LFSR boxes. If printBoxNames is True, box names are also printed
def printGaloisRegister(x1, y1, value, index):
print("%node {0}".format(index))
if drawInitValues:
if printBoxNames:
print(
"\\draw node[draw, fill={8}, minimum size={3}cm,line width={4}cm, label={{ [yshift=-{9}cm] {{ $\\scaleto{{x_{{ {7} }} }}{{ {6} }}$ }} }} ] at ({0}, {1}) {{ $\\scaleto{{ {2} }}{{ {5} }}$ }};".format(
x1,
y1,
value,
boxSize,
lineWidth,
BoxValuesFontSize,
BoxIndexFontSize,
index,
boxColor,
boxBelowTextDistance,
)
)
else:
print(
"\\draw node[draw, fill={7}, minimum size={3}cm,line width={4}cm] at ({0}, {1}) {{ $\\scaleto{{ {2} }}{{ {5} }}$ }};".format(
x1,
y1,
value,
boxSize,
lineWidth,
BoxValuesFontSize,
BoxIndexFontSize,
boxColor,
)
)
else:
if printBoxNames:
print(
"\\draw node[draw, fill={8}, minimum size={3}cm,line width={4}cm, label={{ [yshift=-{9}cm] {{ $\\scaleto{{x_{{ {7} }} }}{{ {6} }}$ }} }} ] at ({0}, {1}) {{ }} ;".format(
x1,
y1,
value,
boxSize,
lineWidth,
BoxValuesFontSize,
BoxIndexFontSize,
index,
boxColor,
boxBelowTextDistance,
)
)
else:
print(
"\\draw node[draw, fill={7}, minimum size={3}cm,line width={4}cm] at ({0}, {1}) {{ }};".format(
x1,
y1,
value,
boxSize,
lineWidth,
BoxValuesFontSize,
BoxIndexFontSize,
boxColor,
)
)
# Draws a line from a register to the XOR gate for Galois LFSR tap points
def printGaloisTapLine(x1, y1, length, is_last_cell=False):
# Draw line up to feedback line position
feedback_y = y1 + length + xorSize # Use the provided length parameter directly
print(
"\t\\draw [line width={0}cm]({1},{2}) -- ({1},{3});".format(
lineWidth, x1 + boxSize / 2, y1, feedback_y
)
)
# Make the half circle smaller
radius = boxSize / 6
if is_last_cell:
# For the last cell, draw the top half circle and bottom right quadrant
# Draw the top half circle
print(
"\t\\draw [line width={0}cm] ({1},{2}) arc(180:0:{3});".format(
lineWidth, x1 + boxSize / 2 - radius, y1, radius
)
)
# Draw the bottom right quadrant
print(
"\t\\draw [line width={0}cm] ({1},{2}) arc(0:-90:{3});".format(
lineWidth, x1 + boxSize / 2 + radius, y1, radius
)
)
else:
# For other cells, draw just the top half circle
print(
"\t\\draw [line width={0}cm] ({1},{2}) arc(180:0:{3});".format(
lineWidth, x1 + boxSize / 2 - radius, y1, radius
)
)
# Draw the line at the bottom of the half circle
print(
"\t\\draw [line width={0}cm] ({1},{2}) -- ({3},{2});".format(
lineWidth, x1 + boxSize / 2 - radius, y1, x1 + boxSize / 2 + radius
)
)
# Position arrow to point exactly to the half circle
arrow_start_y = y1 + radius * 3 # Start position
arrow_end_y = y1 + radius # End position at exact top of half circle
print(
"\t\\draw [arrows={{-Triangle[angle=90:{0}cm,black,fill=black,line width={1}cm]}}]({2},{3}) -- ({2},{4});".format(
arrowHead, lineWidth, x1 + boxSize / 2, arrow_start_y, arrow_end_y
)
)
def printGaloisFeedbackPath(x1, x2, x3, y1, y2, feedback, boxsizehalf):
if drawInitValues:
print(
"\\draw[->,line width=0.1cm,arrows={{-Triangle[angle=90:{6}cm,black,fill=black,line width={5}cm]}}] ({0},{3}) -- ({1},{3}) -- ({1},{4}) -- ({2},{4});".format(
x1,
x2,
x3,
y1,
y2,
lineWidth,
arrowHead,
boxsizehalf,
)
)
else:
print(
"\\draw[->,line width=0.1cm,arrows={{-Triangle[angle=90:{6}cm,black,fill=black,line width={5}cm]}}] ({0},{3}) -- ({1},{3}) -- ({1},{4}) -- ({2},{4});".format(
x1,
x2,
x3,
y1,
y2,
lineWidth,
arrowHead,
boxsizehalf,
)
)
def printGaloisOutput(x1, x2, y1, value, boxsizehalf):
if drawInitValues:
print(
"\\draw[->,line width=0.1cm,arrows={{-Triangle[angle=90:{4}cm,black,fill=black,line width={3}cm]}}] ({0},{2}) -- ({1},{2}) node[right]{{ $\\scaleto{{ {6} }}{{ {5} }}$ }};".format(
x1, x2, y1, lineWidth, arrowHead, BoxValuesFontSize, value, boxsizehalf
)
)
else:
print(
"\\draw[->,line width=0.1cm,arrows={{-Triangle[angle=90:{4}cm,black,fill=black,line width={3}cm]}}] ({0},{2}) -- ({1},{2}) node[midway,above]{{}};".format(
x1, x2, y1, lineWidth, arrowHead, BoxValuesFontSize
)
)
# Feedback calculator for the Galois LFSR
def calculateGaloisFeedback(taps, values):
feedback = 0
for i, tap in enumerate(taps):
if tap == 1:
feedback = feedback ^ values[i]
return feedback
# Main function to print the complete LFSR diagram
def drawGaloisLFSR(taps, values, hideValues, hideBoxNames):
global drawInitValues, printBoxNames
global boxSize, xorDistance, BoxValuesFontSize, BoxIndexFontSize, leftFeedbackDistance, boxBelowTextDistance # Added boxBelowTextDistance
# Update drawing parameters based on LFSR length
boxSize = get_box_size(len(taps))
xorDistance = boxSize / 2
BoxValuesFontSize = get_font_size(boxSize)
BoxIndexFontSize = get_font_size(boxSize)
boxBelowTextDistance = 1.5 * boxSize # Adjusted dynamically based on box size
leftFeedbackDistance = 2 * boxSize
drawInitValues = not hideValues
printBoxNames = not hideBoxNames
lastTapPos = 0
printPreample()
count = taps.count(1)
for i, val in enumerate(taps):
printGaloisRegister(x + i * boxSize, y, values[i], i)
if val == 1:
if i == len(taps) - 1:
# For the last cell
printGaloisTapLine(
x + i * boxSize, y + boxSize / 2, xorDistance, is_last_cell=True
)
else:
# For all other cells
printGaloisTapLine(
x + i * boxSize, y + boxSize / 2, xorDistance, is_last_cell=False
)
# For the last box, add the output feedback line going to the top feedback line
if i == len(taps) - 1:
# Calculate the coordinates for the output feedback line
box_right_x = x + i * boxSize + boxSize # Right edge of the last box
feedback_y = (
y + boxSize / 2 + xorDistance + xorSize
) # Height of the feedback line
# Draw line up and left to connect to feedback line
print(
"\\draw [line width={0}cm] ({1},{2}) -- ({1},{3}) -- ({4},{3});".format(
lineWidth,
box_right_x, # Start just right of the last box
y, # Start at output line height
feedback_y, # Go up to feedback line height
x - leftFeedbackDistance, # Go left to meet feedback line
)
)
printGaloisTapLine(x + i * boxSize, y + boxSize / 2, xorDistance)
count = count - 1
lastTapPos = i
# Always draw feedback line from output line
feedback_y = y + boxSize / 2 + xorDistance + xorSize
print(
"\\draw [line width={0}cm] ({1},{2}) -- ({1},{3}) -- ({4},{3});".format(
lineWidth,
x + (len(taps) - 1) * boxSize + boxSize, # Start just right of the last box
y, # Start at output line height
feedback_y, # Go up to feedback line height
x - leftFeedbackDistance, # Go left to meet feedback line
)
)
feedbackValue = values[-1]
printGaloisFeedbackPath(
x + (len(taps) - 1) * boxSize, # x1
x - leftFeedbackDistance, # x2
x - boxSize / 2, # x3
y + boxSize / 2 + xorDistance + xorSize, # y1
y, # y2
feedbackValue, # Feedback value
boxSize / 4, # Box size for position of the feedback value
)
printGaloisOutput(
x + (len(taps) - 1) * boxSize + boxSize / 2,
x + (len(taps) - 1) * boxSize + 2 * boxSize,
y,
values[-1],
boxSize / 4,
)
printPrologue()
def parse_binary_list(s):
"""Convert a string of 1s and 0s into a list of integers."""
try:
return [int(x) for x in s if x in "01"]
except ValueError:
raise argparse.ArgumentTypeError("Input must be a string of 0s and 1s")
def compile_and_convert(output_format="pdf", output_dir="."):
"""Compile LaTeX and convert to specified format."""
import subprocess
from pathlib import Path
# Ensure the output directory exists
Path(output_dir).mkdir(parents=True, exist_ok=True)
outputs = []
# Convert LaTeX to PDF
try:
subprocess.run(
["pdflatex", "-interaction=batchmode", "lfsr.tex"],
capture_output=True,
check=True,
)
outputs.append("lfsr.pdf")
# Generate other formats if requested
if output_format in ["eps", "all"]:
subprocess.run(
["pdftops", "-eps", "lfsr.pdf", "lfsr.eps"],
capture_output=True,
check=True,
)
outputs.append("lfsr.eps")
if output_format in ["png", "all"]:
subprocess.run(
["magick", "-density", "300", "lfsr.pdf", "lfsr.png"],
capture_output=True,
check=True,
)
outputs.append("lfsr.png")
# Move generated files to the output directory
for output in outputs:
Path(output).rename(Path(output_dir) / output)
# Clean up temporary files
for f in Path(".").glob("lfsr.*"):
if f.suffix in [".aux", ".log", ".tex"]:
f.unlink()
if Path("texput.log").exists():
Path("texput.log").unlink()
print(f"Galois LFSR diagram generated successfully in '{output_dir}':")
for output in outputs:
print(f"- {output}")
except subprocess.CalledProcessError as e:
print("Error during compilation/conversion:", file=sys.stderr)
if "pdflatex" in str(e.cmd):
print(
"Failed to generate PDF. Check if pdflatex is installed.",
file=sys.stderr,
)
elif "pdftops" in str(e.cmd):
print(
"Failed to generate EPS. Check if pdftops is installed.",
file=sys.stderr,
)
elif "magick" in str(e.cmd):
print(
"Failed to generate PNG. Check if ImageMagick is installed.",
file=sys.stderr,
)
sys.exit(1)
def main():
import argparse
import sys
import warnings
# Suppress syntax warnings about escape sequences
warnings.filterwarnings("ignore", category=SyntaxWarning)
# Set up argument parser
parser = argparse.ArgumentParser(
description="""Generate a Galois Linear Feedback Shift Register (LFSR) diagram.
A Galois LFSR is a specific type of LFSR where the feedback taps are XORed with
the output bit before being fed into the next register. This implementation is
more efficient in hardware compared to Fibonacci LFSRs.""",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"--taps",
type=parse_binary_list,
help="Tap sequence as a string of 0s and 1s (rightmost is x_0)\n"
"Default: 1001",
default="1001",
)
parser.add_argument(
"--init-values",
type=parse_binary_list,
help="Initial values as a string of 0s and 1s\n" "Default: 1111",
default="1111",
)
parser.add_argument(
"--hide-values",
action="store_true",
help="Hide values in the LFSR boxes (default: False)",
default=False,
)
parser.add_argument(
"--hide-names",
action="store_true",
help="Hide box names under the LFSR boxes (default: False)",
default=False,
)
parser.add_argument(
"--format",
choices=["pdf", "png", "eps", "all"],
help="Output format: pdf (default), png, eps, or all",
default="pdf",
)
parser.add_argument(
"--output-dir",
type=str,
help="Directory to save output files (default: current directory)",
default=".",
)
# Parse arguments, suppressing the default error output
try:
args = parser.parse_args()
except SystemExit:
sys.exit(1)
# Validate input lengths match
if len(args.taps) != len(args.init_values):
print(
"Error: Tap sequence and initial values must have the same length",
file=sys.stderr,
)
sys.exit(1)
# Validate input lengths are reasonable
if len(args.taps) > 32:
print("Error: LFSR length cannot exceed 32 bits", file=sys.stderr)
sys.exit(1)
# Print LFSR to file
with open("lfsr.tex", "w") as f:
# Redirect stdout to file
original_stdout = sys.stdout
sys.stdout = f
# Generate LaTeX for Galois LFSR
drawGaloisLFSR(args.taps, args.init_values, args.hide_values, args.hide_names)
# Restore stdout
sys.stdout = original_stdout
# Compile and convert to requested format
compile_and_convert(args.format)
if __name__ == "__main__":
main()