-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
113 lines (85 loc) · 2.86 KB
/
main.c
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
// SPDX-License-Identifier: MIT
// Copyright 2024 Algovoid
#include "wokwi-api.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct {
pin_t pin_1A;
pin_t pin_1B;
pin_t pin_1C;
pin_t pin_1D;
pin_t pin_1E;
pin_t pin_1F;
pin_t pin_2A;
pin_t pin_2B;
pin_t pin_2C;
pin_t pin_2D;
pin_t pin_1Y;
pin_t pin_2Y;
} chip_state_t;
uint8_t subGate1;
uint8_t subGate2;
uint8_t subGate3;
uint8_t subGate4;
static void set_logic(chip_state_t *chip) {
subGate1 = pin_read(chip->pin_1A) & pin_read(chip->pin_1B) & pin_read(chip->pin_1C) ;
subGate2 = pin_read(chip->pin_1D) & pin_read(chip->pin_1E) & pin_read(chip->pin_1F);
subGate3 = pin_read(chip->pin_2A) & pin_read(chip->pin_2B) ;
subGate4 = pin_read(chip->pin_2C) & pin_read(chip->pin_2D);
pin_write(chip->pin_1Y, !( subGate1 | subGate2 ) );
pin_write(chip->pin_2Y, !( subGate3 | subGate4 ) );
}
static void chip_pin_change(void *user_data, pin_t pin, uint32_t value) {
chip_state_t *chip = (chip_state_t*)user_data;
set_logic(chip);
// uncomment to see gates and output states
/*
printf("Gate 1 : %d\n" , subGate1);
printf("Gate 2 : %d\n" , subGate2);
printf("Gate 3 : %d\n" , subGate3);
printf("Gate 4 : %d\n" , subGate4);
printf("Output State 1Y : %u\n" , pin_read(chip->pin_1Y));
printf("Output State 2Y : %u\n" , pin_read(chip->pin_2Y)); */
}
void chip_init() {
printf("Initialize sn54ls51 chip!\n");
chip_state_t *chip = malloc(sizeof(chip_state_t));
chip->pin_1A = pin_init("1A", INPUT);
chip->pin_1B = pin_init("1B", INPUT);
chip->pin_1C = pin_init("1C", INPUT);
chip->pin_1D = pin_init("1D", INPUT);
chip->pin_1E = pin_init("1E", INPUT);
chip->pin_1F = pin_init("1F", INPUT);
chip->pin_2A = pin_init("2A", INPUT);
chip->pin_2B = pin_init("2B", INPUT);
chip->pin_2C = pin_init("2C", INPUT);
chip->pin_2D = pin_init("2D", INPUT);
chip->pin_1Y = pin_init("1Y", OUTPUT);
chip->pin_2Y = pin_init("2Y", OUTPUT);
pin_write(chip->pin_1A, LOW);
pin_write(chip->pin_1B, LOW);
pin_write(chip->pin_1C, LOW);
pin_write(chip->pin_1D, LOW);
pin_write(chip->pin_1E, LOW);
pin_write(chip->pin_1F, LOW);
pin_write(chip->pin_2A, LOW);
pin_write(chip->pin_2B, LOW);
pin_write(chip->pin_2C, LOW);
pin_write(chip->pin_2D, LOW);
const pin_watch_config_t config = {
.edge = BOTH,
.pin_change = chip_pin_change,
.user_data = chip,
};
pin_watch(chip->pin_1A, &config);
pin_watch(chip->pin_1B, &config);
pin_watch(chip->pin_1C, &config);
pin_watch(chip->pin_1D, &config);
pin_watch(chip->pin_1E, &config);
pin_watch(chip->pin_1F, &config);
pin_watch(chip->pin_2A, &config);
pin_watch(chip->pin_2B, &config);
pin_watch(chip->pin_2C, &config);
pin_watch(chip->pin_2D, &config);
set_logic(chip);
}