Skip to content

Commit

Permalink
✨ Add getopt_long()
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Dec 27, 2024
1 parent 23b2159 commit fec0954
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ fi
if [[ -x /opt/ccstudio/ccs/ccs_base/scripting/examples/loadti/loadti.sh ]]; then
PATH="/opt/ccstudio/ccs/ccs_base/scripting/examples/loadti${PATH:+:}$PATH"
fi
PATH="$PWD/scripts${PATH:+:}$PATH"

if command -v cl6x &>/dev/null; then
export C6X_C_DIR
C6X_C_DIR="$(dirname "$(dirname "$(realpath "$(command -v cl6x)")")")"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Default debugger is XDS100v3. See [targetConfigs](targetConfigs) to modify it.

```sh
# load /the/path/of/352x288.yuv to DDR2 and run:
# build/x264.out 352x288.yuv 1
scripts/burn.js /the/path/of/352x288.yuv -- 352x288.yuv 1
# build/x264.out --input=352x288.yuv --frames=10
scripts/burn.js /the/path/of/352x288.yuv -- build/x264.out --input=352x288.yuv --frames=10
ffplay build/out.264
```

Expand Down
3 changes: 1 addition & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,8 @@ if meson.is_cross_build()
join_paths(meson.current_source_dir(), get_option('cmd_file')),
language: ['c', 'linearasm'],
)
else
subdir('tests')
endif
subdir('tests')

deps = []
if cc.get_id() == 'c6000'
Expand Down
1 change: 1 addition & 0 deletions meson/ti-c6000.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ar = 'ar6x'
strip = 'strip6x'
nm = 'nm6x'
as = 'asm6x'
exe_wrapper = 'wrapper.sh'

[properties]
needs_exe_wrapper = true
Expand Down
21 changes: 12 additions & 9 deletions scripts/burn.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@ c64xp.expression.evaluate(
'GEL_LoadGel("/opt/ccstudio/ccs/ccs_base/emulation/boards/evmdm6467/gel/davincihd1080p_dsp.gel")'
);
c64xp.target.connect();
var bin = "build/x264.out";

if (arguments[0] && arguments[0] !== "--") {
var ddr2 = 0xa0000000;
arm926.memory.loadRaw(0, ddr2, arguments[0], 32, false); // DDR2 SDRAM
arm926.memory.verifyBinaryProgram(arguments[0], ddr2);
}

var args = [];
for (i in arguments)
if (arguments[i] === "--") {
args = [bin].concat(arguments.slice(Number(i) + 1));
args = arguments.slice(Number(i) + 1);
break;
}
c64xp.memory.loadProgram(bin, args);
c64xp.memory.verifyProgram(bin);
if (arguments[0] && arguments[0] !== "--") {
var ddr2 = 0xa0000000;
arm926.memory.loadRaw(0, ddr2, arguments[0], 32, false); // DDR2 SDRAM
arm926.memory.verifyBinaryProgram(arguments[0], ddr2);
if (args.length) {
c64xp.memory.loadProgram(args[0], args);
c64xp.memory.verifyProgram(args[0]);
c64xp.clock.runBenchmark();
}
if (args.length) c64xp.clock.runBenchmark();
5 changes: 5 additions & 0 deletions scripts/wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -e
cd "$(dirname "$(dirname "$(readlink -f "$0")")")"

exec scripts/burn.js -- "$@"
34 changes: 34 additions & 0 deletions tests/check_getopt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>

#include "extras/getopt.h"

static char shortopts[] = "0:1:2:";
static struct option longopts[] = {
{"option0", required_argument, NULL, '0'},
{"option1", required_argument, NULL, '1'},
{"option2", required_argument, NULL, '2'},
{NULL, 0, NULL, 0}};

int main(int argc, char *argv[]) {
int c;
char *results[3] = {};
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) {
case '0':
results[0] = optarg;
break;
case '1':
results[1] = optarg;
break;
case '2':
results[2] = optarg;
break;
default:
return -1;
}
}
for (c = 0; c < 3; ++c)
printf("%s\n", results[c]);
return EXIT_SUCCESS;
}
43 changes: 26 additions & 17 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
checkmk = find_program('checkmk')
if checkmk.found()
check_downsample_c = custom_target(
'check_downsample.c',
input: 'check_downsample.c.in',
output: 'check_downsample.c',
capture: true,
command: [checkmk, '@INPUT@'],
)
check_dep = dependency('check', fallback: ['check', 'check_dep'])
check_downsample = executable(
'check_downsample',
[check_downsample_c, '../downsample.c'],
include_directories: include_directories('..'),
dependencies: check_dep,
)
test('check_downsample', check_downsample)
check_getopt = executable(
'check_getopt',
['check_getopt.c', '../extras/getopt.c'],
include_directories: include_directories('..'),
)
test('check_getopt', check_getopt, args: ['--option0=0', '--option1', '1', '-22'])

if get_option('downsample').to_int() > 0
checkmk = find_program('checkmk')
if checkmk.found()
check_downsample_c = custom_target(
'check_downsample.c',
input: 'check_downsample.c.in',
output: 'check_downsample.c',
capture: true,
command: [checkmk, '@INPUT@'],
)
check_dep = dependency('check', fallback: ['check', 'check_dep'])
check_downsample = executable(
'check_downsample',
[check_downsample_c, '../downsample.c'],
include_directories: include_directories('..'),
dependencies: check_dep,
)
test('check_downsample', check_downsample)
endif
endif
27 changes: 22 additions & 5 deletions x264.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "input.h"
#include "output.h"

#include "extras/getopt.h"

/* In microseconds */
#define UPDATE_INTERVAL 1000000

Expand Down Expand Up @@ -85,17 +87,32 @@ int main(int argc, char **argv) {
return ret;
}

static char shortopts[] = "i:f:";
static struct option longopts[] = {
{"input", required_argument, NULL, 'i'},
{"frames", required_argument, NULL, 'f'},
{NULL, 0, NULL, 0}};

static int parse(int argc, char **argv, x264_param_t *param, cli_opt_t *opt) {
char *input_filename = "352x288.yuv";
char *output_filename = "out.264";
video_info_t info = {0};
int c;

if (argc > 1)
input_filename = argv[1];
if (argc > 2)
info.num_frames = strtol(argv[2], NULL, 0);
info.num_frames = 1;
opt->b_progress = 1;
info.num_frames = 1;
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) {
case 'i':
input_filename = optarg;
break;
case 'f':
info.num_frames = strtol(optarg, NULL, 0);
break;
default:
return 1;
}
}

x264_param_default(param);
cli_log_level = param->i_log_level;
Expand Down

0 comments on commit fec0954

Please sign in to comment.