aboutsummaryrefslogtreecommitdiffstats
path: root/libs/subcircuit/demo.cc
blob: 149dc6aa0e60b67723b2d60fba068abcb1601e58 (plain)
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
#include "subcircuit.h"
#include <stdio.h>

#define VERBOSE

int main()
{
	SubCircuit::Graph needle, haystack;

	// create needle graph

	needle.createNode("mul_1", "product");
	needle.createPort("mul_1", "A", 4);
	needle.createPort("mul_1", "B", 4);
	needle.createPort("mul_1", "Y", 4);
	needle.markExtern("mul_1", "A");
	needle.markExtern("mul_1", "B");

	needle.createNode("mul_2", "product");
	needle.createPort("mul_2", "A", 4);
	needle.createPort("mul_2", "B", 4);
	needle.createPort("mul_2", "Y", 4);
	needle.markExtern("mul_2", "A");
	needle.markExtern("mul_2", "B");

	needle.createNode("add_1", "sum");
	needle.createPort("add_1", "A", 4);
	needle.createPort("add_1", "B", 4);
	needle.createPort("add_1", "Y", 4);
	needle.markExtern("add_1", "Y");

	needle.createConnection("mul_1", "Y", "add_1", "A");
	needle.createConnection("mul_2", "Y", "add_1", "B");

#ifdef VERBOSE
	printf("\n");
	needle.print();
#endif

	// create haystack graph

#if 0
	for (int i = 0; i < 4; i++) {
		char id[100];
		snprintf(id, 100, "mul_%d", i);
		haystack.createNode(id, "mul");
		haystack.createPort(id, "A", 4);
		haystack.createPort(id, "B", 4);
		haystack.createPort(id, "Y", 4);
		haystack.markExtern(id, "A");
		haystack.markExtern(id, "B");
	}

	for (int i = 0; i < 3; i++) {
		char id[100];
		snprintf(id, 100, "add_%d", i);
		haystack.createNode(id, "add");
		haystack.createPort(id, "A", 4);
		haystack.createPort(id, "B", 4);
		haystack.createPort(id, "Y", 4);
	}

	haystack.createConnection("mul_0", "Y", "add_0", "A");
	haystack.createConnection("mul_1", "Y", "add_0", "B");

	haystack.createConnection("mul_2", "Y", "add_1", "A");
	haystack.createConnection("mul_3", "Y", "add_1", "B");

	haystack.createConnection("add_0", "Y", "add_2", "A");
	haystack.createConnection("add_1", "Y", "add_2", "B");
	haystack.markExtern("add_2", "Y");
#else
	std::vector<std::string> cellIds;
	srand48(12345);

	for (int i = 0; i < 45; i++) {
		char id[100];
		snprintf(id, 100, "cell_%02d", i);
		haystack.createNode(id, i < 30 ? "mul" : "add");
		haystack.createPort(id, "A", 4);
		haystack.createPort(id, "B", 4);
		haystack.createPort(id, "Y", 4);
		cellIds.push_back(id);
	}

	for (int i = 0; i < int(cellIds.size()); i++) {
		if (lrand48() % (i < 20 ? 3 : 2) != 0)
			continue;
		const std::string &id = cellIds[i];
		const std::string &id_left = cellIds[lrand48() % cellIds.size()];
		const std::string &id_right = cellIds[lrand48() % cellIds.size()];
		haystack.createConnection(id_left, "Y", id, "A");
		haystack.createConnection(id_right, "Y", id, "B");
	}
#endif

#ifdef VERBOSE
	printf("\n");
	haystack.print();
#endif

	// search needle in haystack

	SubCircuit::Solver solver;
	std::vector<SubCircuit::Solver::Result> results;

#ifdef VERBOSE
	solver.setVerbose();
#endif

	solver.addCompatibleTypes("product", "mul");
	solver.addCompatibleTypes("sum", "add");

	solver.addSwappablePorts("product", "A", "B");
	solver.addSwappablePorts("sum", "A", "B");

	solver.addGraph("needle", needle);
	solver.addGraph("haystack", haystack);
	solver.solve(results, "needle", "haystack");

	for (int i = 0; i < int(results.size()); i++) {
		printf("\nMatch #%d: (%s in %s)\n", i, results[i].needleGraphId.c_str(), results[i].haystackGraphId.c_str());
		for (const auto &it : results[i].mappings) {
			printf("  %s -> %s", it.first.c_str(), it.second.haystackNodeId.c_str());
			for (const auto &it2 : it.second.portMapping)
				printf(" %s:%s", it2.first.c_str(), it2.second.c_str());
			printf("\n");
		}
	}

	printf("\n");
	return 0;
}
on-zero, otherwise callback should set to the generated report ID * \param[in] ReportType Type of the report to create, either HID_REPORT_ITEM_In or HID_REPORT_ITEM_Feature * \param[out] ReportData Pointer to a buffer where the created report should be stored * \param[out] ReportSize Number of bytes written in the report (or zero if no report is to be sent) * * \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent */ bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, uint8_t* const ReportID, const uint8_t ReportType, void* ReportData, uint16_t* const ReportSize) { uint8_t JoyStatus_LCL = Joystick_GetStatus(); uint8_t ButtonStatus_LCL = Buttons_GetStatus(); /* Determine which interface must have its report generated */ if (HIDInterfaceInfo == &Keyboard_HID_Interface) { USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData; /* If first board button not being held down, no keyboard report */ if (!(ButtonStatus_LCL & BUTTONS_BUTTON1)) return 0; KeyboardReport->Modifier = HID_KEYBOARD_MODIFIER_LEFTSHIFT; if (JoyStatus_LCL & JOY_UP) KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_A; else if (JoyStatus_LCL & JOY_DOWN) KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_B; if (JoyStatus_LCL & JOY_LEFT) KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_C; else if (JoyStatus_LCL & JOY_RIGHT) KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_D; if (JoyStatus_LCL & JOY_PRESS) KeyboardReport->KeyCode[0] = HID_KEYBOARD_SC_E; *ReportSize = sizeof(USB_KeyboardReport_Data_t); return false; } else { USB_MouseReport_Data_t* MouseReport = (USB_MouseReport_Data_t*)ReportData; /* If first board button being held down, no mouse report */ if (ButtonStatus_LCL & BUTTONS_BUTTON1) return 0; if (JoyStatus_LCL & JOY_UP) MouseReport->Y = -1; else if (JoyStatus_LCL & JOY_DOWN) MouseReport->Y = 1; if (JoyStatus_LCL & JOY_LEFT) MouseReport->X = -1; else if (JoyStatus_LCL & JOY_RIGHT) MouseReport->X = 1; if (JoyStatus_LCL & JOY_PRESS) MouseReport->Button |= (1 << 0); *ReportSize = sizeof(USB_MouseReport_Data_t); return true; } } /** HID class driver callback function for the processing of HID reports from the host. * * \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced * \param[in] ReportID Report ID of the received report from the host * \param[in] ReportType The type of report that the host has sent, either HID_REPORT_ITEM_Out or HID_REPORT_ITEM_Feature * \param[in] ReportData Pointer to a buffer where the received report has been stored * \param[in] ReportSize Size in bytes of the received HID report */ void CALLBACK_HID_Device_ProcessHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo, const uint8_t ReportID, const uint8_t ReportType, const void* ReportData, const uint16_t ReportSize) { if (HIDInterfaceInfo == &Keyboard_HID_Interface) { uint8_t LEDMask = LEDS_NO_LEDS; uint8_t* LEDReport = (uint8_t*)ReportData; if (*LEDReport & HID_KEYBOARD_LED_NUMLOCK) LEDMask |= LEDS_LED1; if (*LEDReport & HID_KEYBOARD_LED_CAPSLOCK) LEDMask |= LEDS_LED3; if (*LEDReport & HID_KEYBOARD_LED_SCROLLLOCK) LEDMask |= LEDS_LED4; LEDs_SetAllLEDs(LEDMask); } }