-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (164 loc) · 4.67 KB
/
main.cpp
File metadata and controls
190 lines (164 loc) · 4.67 KB
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
//******************************************************************************
//
// KS10 Console Microcontroller
//
//! \brief
//! FPGA Programming Utility
//!
//! \file
//! main.cpp
//!
//! \author
//! Rob Doyle - doyle (at) cox (dot) net
//!
//******************************************************************************
//
// Copyright (C) 2022 Rob Doyle
//
// This file is part of the KS10 FPGA Project
//
// The KS10 FPGA project is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// The KS10 FPGA project is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this software. If not, see <http://www.gnu.org/licenses/>.
//
//******************************************************************************
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <getopt.h>
#include "fpga_loader.hpp"
//!
//! \brief
//! This function loads firmware into the on-board FPGA.
//!
//! \param[in] argc
//! argc is the number of arguments provided.
//!
//! \param[in] argv
//! argv is an array of arguments.
//!
//! \returns
//! EXIT_SUCCESS or EXIT_FAILURE
//!
int main(int argc, char *argv[]) {
const char *usage =
"\n"
"The fpga_loader is an executable for the DE10-Nano that allows the target\n"
"device to load its own FPGA firmware.\n"
"\n"
"usage: " PROGNAME " [options] \"raw_binary_file.rbf\"\n"
"\n"
"Valid options are:\n"
" --debug Print debug messages.\n"
" --help Print help message and exit.\n"
" --quiet Suppress messages.\n"
"\n"
"Note: The FPGA firmware must be in Raw Binary File (RBF) format.\n"
"\n";
//
// Sort command line
//
static const struct option options[] = {
{"help", no_argument, 0, 0}, // 0
{"debug", no_argument, 0, 0}, // 1
{"q", no_argument, 0, 0}, // 2
{"quiet", no_argument, 0, 0}, // 3
{0, 0, 0, 0}, // 4
};
int index = 0;
bool debug = false;
bool quiet = false;
opterr = 0;
for (;;) {
int ret = getopt_long(argc, argv, "", options, &index);
if (ret == -1) {
break;
} else if (ret == '?') {
printf("%s: unrecognized option: %s\n", PROGNAME, argv[optind-1]);
printf(usage);
return true;
} else {
switch(index) {
case 0:
printf(usage);
return true;
case 1:
debug = true;
break;
case 2:
case 3:
quiet = true;
break;
}
}
}
//
// Check that the program arguments are correct
//
if (argv[optind] == NULL) {
printf("%s: missing filename\n", PROGNAME);
printf(usage);
return EXIT_FAILURE;
}
//
// Open the firmware file
//
FILE *fd = fopen(argv[optind], "r");
if (!fd) {
perror(PROGNAME);
return EXIT_FAILURE;
}
//
// Read the rbf file
//
uint32_t *buf = (uint32_t*)malloc(8*1024*1024);
size_t size = fread(buf, 1, 8*1024*1024, fd);
if (size == 0) {
perror(PROGNAME);
return EXIT_FAILURE;
}
if (!quiet) {
printf("%s: Successfully read file \"%s\" (%d bytes).\n", PROGNAME, argv[optind], size);
}
//
// Check input buffer alignment
//
uint32_t addr = (uint32_t)buf;
if ((addr & 0x03) != 0) {
fprintf(stderr, "%s: rbf data buffer is not aligned properly.\n", PROGNAME);
exit(EXIT_FAILURE);
}
//
// Check file length alignment
//
if ((size & 0x03) != 0) {
fprintf(stderr, "%s: rbf file length is not exact multiple of 32-bit words.\n", PROGNAME);
exit(EXIT_FAILURE);
}
//
// size is the number of 32-bit words
//
size /= sizeof(uint32_t);
//
// Program the FPGA
//
fpga_loader_t fpga_loader;
int ret = fpga_loader.loadFPGA(buf, size, debug);
free(buf);
//
// Cleanup
//
if (!quiet && (ret == EXIT_SUCCESS)) {
printf("%s: FPGA progammed successfully\n", PROGNAME);
}
return ret;
}