sm-blueprint-lib 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,56 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+
8
+
9
+ def decoder(bp: Blueprint, num_address: int, pos: Pos | Sequence = (0, 0, 0),
10
+ precreated_inputs_binary=None, precreated_outputs=None, precreated_output_enable=None,
11
+ address_divisor=1, with_enable=True):
12
+ pos = check_pos(pos)
13
+
14
+ if precreated_inputs_binary is not None:
15
+ inputs_binary = precreated_inputs_binary
16
+ else:
17
+ inputs_binary = ndarray(
18
+ (get_bits_required(num_address), 2), dtype=LogicGate)
19
+ if precreated_outputs is not None:
20
+ outputs = precreated_outputs
21
+ else:
22
+ outputs = [LogicGate(pos + (x+1, 0, 0), "0000FF")
23
+ for x in range(num_address)]
24
+ if with_enable:
25
+ if precreated_output_enable is not None:
26
+ output_enable = precreated_output_enable
27
+ else:
28
+ output_enable = LogicGate(pos + (0, 0, 0), "FF0000", 1)
29
+
30
+ if precreated_inputs_binary is None:
31
+ for b in range(get_bits_required(num_address)):
32
+ inputs_binary[b, :] = [
33
+ LogicGate(pos + (b+1, 1, 0), "FF0000", 4),
34
+ LogicGate(pos + (b+1, 2, 0), "FF0000", 1),
35
+ ]
36
+
37
+ for x in range(num_address):
38
+ bit_mask = num_to_bit_list(
39
+ x//address_divisor, get_bits_required(num_address//address_divisor))
40
+ connect(inputs_binary[~bit_mask, 0], outputs[x])
41
+ connect(inputs_binary[bit_mask, 1], outputs[x])
42
+ if with_enable:
43
+ connect(output_enable, outputs)
44
+
45
+ if precreated_inputs_binary is None:
46
+ bp.add(inputs_binary)
47
+ if precreated_outputs is None:
48
+ bp.add(outputs)
49
+ if with_enable:
50
+ if precreated_output_enable is None:
51
+ bp.add(output_enable)
52
+
53
+ if with_enable:
54
+ return inputs_binary, outputs, output_enable
55
+ else:
56
+ return inputs_binary, outputs
@@ -0,0 +1,45 @@
1
+ from typing import Sequence
2
+ from numpy import array, ndarray
3
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.parts.sensor import Sensor5
7
+ from sm_blueprint_lib.parts.timer import Timer
8
+ from sm_blueprint_lib.pos import Pos
9
+
10
+
11
+ def distance_sensor(bp: Blueprint,
12
+ sensor_range: range,
13
+ pos: Pos | Sequence = (0, 0, 0)):
14
+ pos = check_pos(pos)
15
+ bit_length = get_bits_required(len(sensor_range))
16
+ out = [LogicGate(pos + (x, 0, 0), "0000FF", 2) for x in range(bit_length)]
17
+ out = array(out, dtype=LogicGate)
18
+ sensors = [Sensor5(pos + (bit_length, -1, 1), "000000",
19
+ (False, True, "FFFFFF", False, x), xaxis=-3, zaxis=1) for x in sensor_range]
20
+
21
+ current_output_state = 0
22
+ for target in reversed(range(len(sensor_range))):
23
+ difference = current_output_state ^ (target+1)
24
+ if difference:
25
+ bit_mask = num_to_bit_list(difference, bit_length)
26
+ connect(sensors[target], out[bit_mask])
27
+ current_output_state ^= difference
28
+
29
+ bp.add(out, sensors)
30
+ return out, sensors
31
+
32
+
33
+ def distance_sensor_raycast(bp: Blueprint,
34
+ sensor_range: range,
35
+ pos: Pos | Sequence = (0, 0, 0)):
36
+ pos = check_pos(pos)
37
+ bit_length = len(sensor_range)
38
+ out = [LogicGate(pos + (x, 0, 0), "0000FF", 2) for x in range(bit_length)]
39
+ sensors = [Sensor5(pos + (bit_length, -1, 0), "000000",
40
+ (False, True, "FFFFFF", False, x), xaxis=1, zaxis=3) for x in sensor_range]
41
+
42
+ connect(sensors, out)
43
+
44
+ bp.add(out, sensors)
45
+ return out, sensors
@@ -0,0 +1,90 @@
1
+ from itertools import cycle
2
+ from typing import Sequence
3
+ from numpy import ndarray
4
+ from sm_blueprint_lib import get_bits_required, check_pos, connect
5
+ from sm_blueprint_lib.blueprint import Blueprint
6
+ from sm_blueprint_lib.parts.logicgate import LogicGate
7
+ from sm_blueprint_lib.pos import Pos
8
+ from sm_blueprint_lib.prebuilds.decoder import decoder
9
+
10
+
11
+ def ram(bp: Blueprint, bit_length: int, num_address: int, pos: Pos | Sequence = (0, 0, 0),
12
+ address_divisor=1,
13
+ pre_arr=None, pre_inputs=None, pre_outputs=None):
14
+ pos = check_pos(pos)
15
+
16
+ arr = pre_arr if pre_arr is not None else ndarray(
17
+ (bit_length, num_address, 4), dtype=LogicGate)
18
+ writers = [LogicGate(pos + (-1, 2, y), "000000", xaxis=1, zaxis=3)
19
+ for y in range(num_address)]
20
+ readers = [LogicGate(pos + (-1, 0, y), "000000", xaxis=1, zaxis=3)
21
+ for y in range(num_address)]
22
+ inputs = pre_inputs if pre_inputs is not None else [LogicGate(pos + (x, 1, -1), "FF0000", 1, xaxis=1, zaxis=3)
23
+ for x in range(bit_length)]
24
+ outputs = pre_outputs if pre_outputs is not None else [LogicGate(pos + (x, 1, -2), "0000FF", 1, xaxis=1, zaxis=3)
25
+ for x in range(bit_length)]
26
+ writers_binary = ndarray(
27
+ (get_bits_required(num_address//address_divisor), 2), dtype=LogicGate)
28
+ write_enable = LogicGate(pos + (-1, 2, -1), "FF0000", 1, xaxis=1, zaxis=3)
29
+ readers_binary = ndarray(
30
+ (get_bits_required(num_address//address_divisor), 2), dtype=LogicGate)
31
+ read_enable = LogicGate(
32
+ pos + (-1, 0, -1), "FF0000", 1, xaxis=1, zaxis=3)
33
+
34
+ if pre_arr is None:
35
+ for x in range(bit_length):
36
+ for y in range(num_address):
37
+ # if pre_arr is None:
38
+ arr[x, y, :] = [
39
+ # arr.flat[y*num_address + x] = [
40
+ l3 := LogicGate(pos + (x, 1, y+1), "000000", xaxis=1, zaxis=-3),
41
+ l2 := LogicGate(pos + (x, 2, y), "FF0000", 2),
42
+ l1 := LogicGate(pos + (x, 3, y), "000000"),
43
+ l0 := LogicGate(pos + (x, 3, y), "0000FF", 2, xaxis=1, zaxis=3),
44
+ ]
45
+ l2.connect(l1).connect(l0).connect(l0).connect(l2)
46
+ l0.connect(l3)
47
+ # else:
48
+ # print(x, y, bit_length, num_address, arr.shape)
49
+ # l3, l2, l1, l0 = arr[x, y, :]
50
+ # # l3, l2, l1, l0 = arr.reshape(())[x, y, :]
51
+ # l2.connect(l1).connect(l0).connect(l2)
52
+ # l0.connect(l3)
53
+ else:
54
+ for l3, l2, l1, l0 in arr.reshape((arr.shape[0]*arr.shape[1], 4)):
55
+ l2.connect(l1).connect(l0).connect(l2)
56
+ l0.connect(l3)
57
+ connect(writers, arr[:, :, 2].T)
58
+ connect(readers, arr[:, :, 0].T)
59
+ # connect(inputs, arr[:, :, 1])
60
+ for i, a in zip(cycle(inputs), arr[:, :, 1].T.flat):
61
+ connect(i, a)
62
+ # connect(arr[:, :, 0], outputs)
63
+ for a, o, in zip(arr[:, :, 0].T.flat, cycle(outputs)):
64
+ connect(a, o)
65
+ for y in range(get_bits_required(num_address//address_divisor)):
66
+ writers_binary[y, :] = [
67
+ LogicGate(pos + (-2, 2, y), "FF0000", 4, xaxis=1, zaxis=3),
68
+ LogicGate(pos + (-3, 2, y), "FF0000", 1, xaxis=1, zaxis=3),
69
+ ]
70
+ readers_binary[y, :] = [
71
+ LogicGate(pos + (-2, 0, y),
72
+ "FF0000", 4, xaxis=1, zaxis=3),
73
+ LogicGate(pos + (-3, 0, y),
74
+ "FF0000", 1, xaxis=1, zaxis=3),
75
+ ]
76
+ decoder(bp, num_address, (0, 0, 0), writers_binary, writers,
77
+ write_enable, address_divisor=address_divisor)
78
+ decoder(bp, num_address, (0, 0, 0), readers_binary, readers,
79
+ read_enable, address_divisor=address_divisor)
80
+
81
+ if pre_arr is None:
82
+ bp.add(arr)
83
+ if pre_inputs is None:
84
+ bp.add(inputs)
85
+ if pre_outputs is None:
86
+ bp.add(outputs)
87
+ bp.add(writers, readers, writers_binary,
88
+ write_enable, readers_binary, read_enable)
89
+
90
+ return arr, inputs, outputs, writers_binary, writers, write_enable, readers_binary, readers, read_enable
@@ -0,0 +1,72 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import check_pos
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+ from sm_blueprint_lib.prebuilds.counter import counter, counter_decrement
8
+
9
+
10
+ def register(bp: Blueprint,
11
+ bit_length: int,
12
+ OE=True,
13
+ pos: Pos | Sequence = (0, 0, 0)):
14
+ pos = check_pos(pos)
15
+ write = LogicGate(pos + (-1, 2 if OE else 1, 0), "FF0000", 1)
16
+ if OE:
17
+ output_enable = LogicGate(pos + (-1, 0, 0), "FF0000", 1)
18
+ arr = ndarray((bit_length, 4), LogicGate)
19
+ for x in range(bit_length):
20
+ arr[x] = [
21
+ l0 := LogicGate(pos + (x, 0, 0), "000000"),
22
+ l1 := LogicGate(pos + (x, 1, 0), "0000FF", 2),
23
+ l2 := LogicGate(pos + (x, 2, 0), "000000"),
24
+ l3 := LogicGate(pos + (x, 3, 0), "FF0000", 2),
25
+ ]
26
+ l3.connect(l2).connect(l1).connect(l1).connect(l3)
27
+ l1.connect(l0)
28
+ write.connect(l2)
29
+ output_enable.connect(l0)
30
+ bp.add(output_enable)
31
+ else:
32
+ arr = ndarray((bit_length, 3), LogicGate)
33
+ for x in range(bit_length):
34
+ arr[x] = [
35
+ l0 := LogicGate(pos + (x, 0, 0), "0000FF", 2),
36
+ l1 := LogicGate(pos + (x, 1, 0), "000000"),
37
+ l2 := LogicGate(pos + (x, 2, 0), "FF0000", 2),
38
+ ]
39
+ l2.connect(l1).connect(l0).connect(l0).connect(l2)
40
+ write.connect(l1)
41
+ bp.add(arr, write)
42
+ if OE:
43
+ return arr, write, output_enable
44
+ else:
45
+ return arr, write
46
+
47
+
48
+ def counter_register(bp: Blueprint,
49
+ bit_length: int,
50
+ OE=True,
51
+ with_increment=True,
52
+ with_decrement=True,
53
+ pos: Pos | Sequence = (0, 0, 0)):
54
+ pos = check_pos(pos)
55
+ r = register(bp, bit_length, OE, pos)
56
+
57
+ if with_decrement:
58
+ cdec = counter_decrement(bp,
59
+ bit_length=bit_length,
60
+ pos=pos+(0, OE, 1),
61
+ precreated_swxors=r[0][:, int(OE)])
62
+ if with_increment:
63
+ cinc = counter(bp,
64
+ bit_length=bit_length,
65
+ pos=pos+(0, OE, 1+with_decrement),
66
+ precreated_swxors=r[0][:, int(OE)])
67
+ if with_increment and with_decrement:
68
+ return r, cinc[0][:, 1], cinc[1], cdec[0][:, 1], cdec[1]
69
+ elif with_increment:
70
+ return r, cinc[0][:, 1], cinc[1]
71
+ else:
72
+ return r, cdec[0][:, 1], cdec[1]
@@ -0,0 +1,76 @@
1
+ from itertools import batched, cycle
2
+ from math import ceil
3
+ from typing import Sequence
4
+ from numpy import ndarray
5
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
6
+ from sm_blueprint_lib.blueprint import Blueprint
7
+ from sm_blueprint_lib.parts.logicgate import LogicGate
8
+ from sm_blueprint_lib.prebuilds.decoder import decoder
9
+ from sm_blueprint_lib.parts.timer import Timer
10
+ from sm_blueprint_lib.pos import Pos
11
+
12
+
13
+ def rom(
14
+ bp: Blueprint,
15
+ page_size: tuple[int, int],
16
+ data: Sequence[int],
17
+ pos: Pos | Sequence = (0, 0, 0)):
18
+ pos = check_pos(pos)
19
+ data = list(data)
20
+ arr = ndarray((*page_size, 2), dtype=LogicGate)
21
+ page_read = ndarray(page_size[1], dtype=LogicGate)
22
+ page_read2 = ndarray(page_size[1], dtype=LogicGate)
23
+ page_read_binary = ndarray(
24
+ (get_bits_required(page_size[1]), 2), dtype=LogicGate)
25
+ data_out = ndarray(page_size[0], dtype=LogicGate)
26
+ page_writers_binary = ndarray(
27
+ (get_bits_required(len(data)/page_size[1]), 2), dtype=LogicGate)
28
+ page_writers = []
29
+ enable = LogicGate(pos + (-1, page_size[1], 0), "FF00FF", 1)
30
+
31
+ for x in range(page_size[0]):
32
+ for y in range(page_size[1]):
33
+ arr[x, y, :] = [
34
+ LogicGate(pos + (x, y, 0), "000000", 0),
35
+ LogicGate(pos + (x, y, 1), "0000FF", 1),
36
+ ]
37
+ page_read[:] = [LogicGate(pos + (-1, y, 0), "000000", 0)
38
+ for y in range(page_size[1])]
39
+ page_read2[:] = [LogicGate(pos + (-1, y, 1), "000000", 0)
40
+ for y in range(page_size[1])]
41
+ offset0 = (-get_bits_required(page_size[1]) -
42
+ get_bits_required(len(data)/page_size[1])-1)
43
+ for x in range(get_bits_required(page_size[1])):
44
+ page_read_binary[x] = (
45
+ LogicGate(pos + (x+offset0, page_size[1]-1, 0), "FF0000", 4),
46
+ LogicGate(pos + (x+offset0, page_size[1], 0), "FF0000", 1)
47
+ )
48
+ # page_read_binary[:] = [(LogicGate(pos + (x+offset0, page_size[1]-1, 0), "FF0000", 4),
49
+ # LogicGate(pos + (x+offset0, page_size[1], 0), "FF0000", 1))
50
+ # for x in range(get_bits_required(page_size[1]))]
51
+ data_out[:] = [LogicGate(pos + (x, page_size[1], 0), "0000FF", 1)
52
+ for x in range(page_size[0])]
53
+
54
+ page_writers_binary[:] = [(LogicGate(pos + (x+offset0+get_bits_required(page_size[1]), page_size[1]-1, 0), "FF0000", 4),
55
+ LogicGate(pos + (x+offset0+get_bits_required(page_size[1]), page_size[1], 0), "FF0000", 1))
56
+ for x in range(get_bits_required(len(data)/page_size[1]))]
57
+
58
+ for i, data_batch in enumerate(batched(data, page_size[1])):
59
+ g0 = LogicGate(
60
+ pos + (-2-i//(page_size[1]), i % (page_size[1]) - 1, 0), "000000", 0)
61
+ page_writers.append(g0)
62
+ for j, d in enumerate(reversed(data_batch)):
63
+ connect(g0, arr[:, j, 1][num_to_bit_list(d, page_size[0])])
64
+
65
+ connect(arr[:, :, 1], arr[:, :, 0])
66
+ connect(arr[:, :, 0], data_out)
67
+ connect(page_read, page_read2)
68
+ connect(page_read2, arr[:, :, 0].T)
69
+ decoder(bp, page_size[1], precreated_inputs_binary=page_read_binary,
70
+ precreated_outputs=list(reversed(page_read)), precreated_output_enable=enable)
71
+ decoder(bp, ceil(len(data)/page_size[1]), precreated_inputs_binary=page_writers_binary,
72
+ precreated_outputs=page_writers, precreated_output_enable=enable, with_enable=False)
73
+
74
+ bp.add(arr, page_read, page_read2, page_read_binary, data_out,
75
+ page_writers_binary, page_writers, enable)
76
+ return arr, page_read, page_read_binary, data_out, page_writers_binary, page_writers, enable