-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathminer.c
261 lines (226 loc) · 6.08 KB
/
miner.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
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
#include <gb/gb.h>
#include <stdio.h>
#include <string.h>
#include <gb/metasprites.h>
#include "keccak256.h"
#include <gb/console.h>
#include "sha2.h"
#include "sprite.h"
uint32_t logo_offset = 40;
void print_header() {
gotoxy(0, 7);
cls();
puts("Game Boy");
puts("Bitcoin Miner");
puts("by stacksmashing\n\n");
}
void hex_print(unsigned char *data, int len) {
for(int i=0; i < len; i++) {
printf("%x ", data[i] & 0xFF);
}
printf("\n");
}
int receive_block_header_fake(char *header) {
memset(header, 0x11, 76);
return 0;
}
void sb(unsigned char c) {
_io_out = c;
receive_byte();
while(_io_status == IO_RECEIVING) {
// sending...
}
// printf("don\n");
if(_io_status != IO_IDLE) {
printf("failStatus\n#%d\n", _io_status);
return;
}
return;
}
int receive_bytes(unsigned char *header, size_t len) {
for(int i = 0; i < len; i++) {
receive_byte();
while(_io_status == IO_RECEIVING) {
// Receiving...
}
// printf("don\n");
if(_io_status != IO_IDLE) {
printf("Status #%d\n", _io_status);
return 1;
}
header[i] = _io_in;
}
// printf("Done.");
// hex_print(header, 10);
return 0;
}
const uint8_t CMD_BLOCK_HEADER = 0x41;
const uint8_t CMD_TARGET = 0x42;
const uint8_t CMD_NEW_BLOCK = 0x43;
const uint8_t CMD_STATUS = 0x44;
const uint8_t RESP_WAIT_CMD = 0x61;
const uint8_t RESP_WAIT_DATA = 0x62;
const uint8_t RESP_SUCCESS = 0x63;
int receive_block_header(unsigned char *header) {
for(int i = 0; i < 76; i++) {
_io_out = RESP_WAIT_DATA;
receive_byte();
while(_io_status == IO_RECEIVING) {
// Receiving...
}
// printf("don\n");
if(_io_status != IO_IDLE) {
printf("Status #%d\n", _io_status);
return 1;
}
header[i] = _io_in;
}
_io_out = 0x41;
// printf("Done.");
// hex_print(header, 10);
return 0;
}
void wait_byte(const uint8_t b) {
while(1) {
// _io_out = 0x88;
receive_byte();
while(_io_status == IO_RECEIVING) {
// Receiving...
}
// _io_out = 0x41;
if(_io_status != IO_IDLE) {
printf("FAILED TO RECEIVE\n#%d\n", _io_status);
return;
}
if(_io_in == b) {
_io_out = 0x0;
return;
} else {
printf("Invalid command.\n");
}
}
}
void hexdump(unsigned char *f, size_t len) {
for(int i=0; i < len; i++) {
unsigned char c = f[i];
unsigned char c1 = c & 0x0F;
unsigned char c2 = (c >> 4) & 0x0F;
if(c2 < 10) {
printf("%c", '0' + c2);
} else {
printf("%c", 'A' + (c2 - 10));
}
if(c1 < 10) {
printf("%c", '0' + c1);
} else {
printf("%c", 'A' + (c1 - 10));
}
}
puts("");
}
void receive_block_data(unsigned char *block_header, unsigned char *target) {
_io_out = RESP_WAIT_CMD;
wait_byte(CMD_BLOCK_HEADER);
receive_block_header(block_header);
_io_out = RESP_WAIT_CMD;
wait_byte(CMD_TARGET);
receive_bytes(target, 32);
}
int mine_nonce(unsigned char *block_header, unsigned char *target, uint32_t *nonce_out) {
uint32_t nonce = 0;
while(1) {
receive_byte();
// Append nonce
memcpy(&block_header[76], &nonce, 4);
print_header();
// gotoxy(0, 7);
// cls();
// puts("Game Boy");
// puts("Bitcoin Miner");
// puts("by stacksmashing\n\n");
printf("Nonce: %5d\n", nonce);
// Increase nonce
nonce++;
printf("Hashing...\n");
uint8_t hash_rev[32];
uint8_t hash2[32];
uint8_t hash[32];
calc_sha_256(hash2, block_header, 80);
calc_sha_256(hash_rev, hash2, 32);
for(int i=0; i < 32; i++) {
hash[31-i] = hash_rev[i];
}
// printf("");
move_metasprite(sprite_metasprites[0], 0, 0, logo_offset, 40);
logo_offset += 4;
logo_offset %= 198;
receive_byte();
// Check hash...
for(int i=0; i < 32; i++) {
if(hash[i] < target[i]) {
printf("Success!\n");
// _io_out = RESP_SUCCESS;
// hexdump(block_header, 80);
*nonce_out = nonce - 1;
return 0;
} else if(hash[i] > target[i]) {
// printf("Fail!\n");
// return 1;
break;
}
}
// Check if we need to receive a new block.
if(_io_in == CMD_NEW_BLOCK) {
printf("New block data.\n");
return 1;
}
}
}
void miner(void) {
unsigned char block_header[80];
unsigned char target[32];
while(1) {
print_header();
printf("Waiting for data...\n");
receive_block_data(block_header, target);
_io_out = 0x0;
uint32_t nonce = 0;
int mine_result = mine_nonce(block_header, target, &nonce);
// Indicate success to host
if(mine_result == 0) {
_io_out = RESP_SUCCESS;
printf("Sending status...\n");
wait_byte(CMD_STATUS);
// send nonce over.
char data[4];
memcpy(data, &nonce, 4);
for(int i=0; i < 4; i++) {
sb(data[i]);
}
}
}
}
void main(void)
{
UBYTE i, n = 0;
unsigned char *s;
// load tile data into VRAM
set_sprite_data(0, sizeof(sprite_data) >> 4, sprite_data);
// set sprite tile
set_sprite_tile(0, 0);
SHOW_SPRITES;
#if sprite_TILE_H == 16
SPRITES_8x16;
#else
SPRITES_8x8;
#endif
move_metasprite(sprite_metasprites[0], 0, 0, 40, 40);
puts("Game Boy");
puts("Bitcoin Miner");
puts("by stacksmashing\n\n");
CRITICAL {
add_SIO(nowait_int_handler);
set_interrupts(SIO_IFLAG | VBL_IFLAG);
}
miner();
}