-
Notifications
You must be signed in to change notification settings - Fork 1
/
phppld.php
156 lines (134 loc) · 3.34 KB
/
phppld.php
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
<?php
/**
* @brief PHP-PLD
* @date 2020-11-07
* @version 1.0
* @author Manawyrm
*/
$optionIndex = false;
$options = getopt ( "i:w:t:", [], $optionIndex );
if (!$options)
{
print_usage();
die();
}
if ($argc == $optionIndex)
{
error_log("[Error] No input file specified!");
error_log("");
print_usage();
die();
}
$filename = $argv[$optionIndex++];
if (!file_exists($filename))
{
error_log("[Error] Could not open input file!");
error_log("");
print_usage();
die();
}
if (!isset($options["i"]) || !is_numeric($options["i"]))
{
error_log("[Error] Invalid number of input bits!");
error_log("");
print_usage();
die();
}
$numberOfInputs = (int) $options["i"];
$numberOfOutputs = 8;
load_pld($filename);
if (isset($options["t"]) && $options["t"] !== false)
{
$output_array = evaluate_input((int)$options["t"], $numberOfInputs, $numberOfOutputs);
$output = "";
for ($i=0; $i < $numberOfOutputs; $i++) {
$output .= $output_array[$i] ? "1" : "0";
}
error_log("Result for input " . (int)$options["t"] . ":");
error_log($output);
}
if (isset($options["w"]) && $options["w"] !== false)
{
$bitstream = generate_bitstream($numberOfInputs, $numberOfOutputs);
file_put_contents($options["w"], $bitstream);
}
function print_usage()
{
error_log("PHP-PLD v0.1");
error_log("Written by Manawyrm");
error_log("");
error_log("Usage: php phppld.php <input file>");
error_log(" -i <number of input bits> (required)");
error_log(" -w <target filename>");
error_log(" -t <input to test>");
}
function load_pld($pldFilename)
{
$pldContent = file_get_contents($pldFilename);
$pldContent = str_replace("<?php", "", $pldContent);
$php = 'function pldeval (&$input, &$output) {' . "\n" . $pldContent . "\n}";
eval($php);
}
function generate_bitstream($numberOfInputs, $numberOfOutputs)
{
error_log("Generating " . ((2 ** $numberOfInputs) * $numberOfOutputs / 8) . " bytes of bitstream");
$bitstream = "";
if ($numberOfOutputs != 8)
{
throw new Exception("Number of outputs != 8 not supported (yet)");
}
for ($input = 0; $input < (2 ** $numberOfInputs); $input++)
{
$output_array = evaluate_input($input, $numberOfInputs, $numberOfOutputs);
$bitstream .= chr(integer_value_msb($output_array));
}
return $bitstream;
}
function evaluate_input($input, $numberOfInputs, $numberOfOutputs)
{
$input_array = integer_to_array($input, $numberOfInputs);
$output_array = [];
for ($outputBit=0; $outputBit < $numberOfOutputs; $outputBit++)
{
$output_array[$outputBit] = false;
}
$return = @pldeval($input_array, $output_array);
for ($outputBit=0; $outputBit < $numberOfOutputs; $outputBit++)
{
$output_array[$outputBit] = !!$output_array[$outputBit];
}
return $output_array;
}
function integer_value($array, $from = 0, $to = false)
{
if ($to === false)
$to = count($array);
$bits = "";
for ($i = $from; $i < $to; $i++)
{
$bits .= $array[$i] ? "1" : "0";
}
return bindec($bits);
}
function integer_value_msb($array, $from = 0, $to = false)
{
if ($to === false)
$to = count($array);
$bits = "";
for ($i = $from; $i < $to; $i++)
{
$bits .= $array[$i] ? "1" : "0";
}
$bits = strrev($bits);
return bindec($bits);
}
function integer_to_array($int, $length)
{
$output = [];
$bits = sprintf( "%".(int)$length."d", decbin( $int ));
for ($i=0; $i < $length; $i++)
{
$output[$i] = ($bits[$i] == "1") ? true : false;
}
return $output;
}