From 97b96801d1a7ddf4455fbab02efe670e4eb5e31c Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Wed, 11 Jan 2023 13:15:21 +0100 Subject: [PATCH] Ensure mux channels are registered in the bay --- src/emu/mux.c | 12 ++++++++++++ test/unit/mux.c | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/emu/mux.c b/src/emu/mux.c index b37b1b4..bd18502 100644 --- a/src/emu/mux.c +++ b/src/emu/mux.c @@ -142,6 +142,18 @@ mux_init(struct mux *mux, return -1; } + /* Ensure both channels are registered */ + if (bay_find(bay, select->name) == NULL) { + err("mux_init: select channel %s not registered in bay\n", + select->name); + return -1; + } + if (bay_find(bay, output->name) == NULL) { + err("mux_init: output channel %s not registered in bay\n", + output->name); + return -1; + } + /* The output channel must accept multiple writes in the same * propagation phase while the channel is dirty, as we may write to the * input and select channel at the same time. */ diff --git a/test/unit/mux.c b/test/unit/mux.c index 112fffd..504e09c 100644 --- a/test/unit/mux.c +++ b/test/unit/mux.c @@ -223,14 +223,24 @@ main(void) } /* Register all channels in the bay */ - bay_register(&bay, &output); - bay_register(&bay, &select); + if (bay_register(&bay, &select) != 0) + die("bay_register failed\n"); + for (int i = 0; i < N; i++) { - bay_register(&bay, &inputs[i]); + if(bay_register(&bay, &inputs[i]) != 0) + die("bay_register failed\n"); } struct mux mux; - mux_init(&mux, &bay, &select, &output, NULL); + /* Attempt to init the mux without registering the output */ + if (mux_init(&mux, &bay, &select, &output, NULL) == 0) + die("mux_init didn't fail\n"); + + if (bay_register(&bay, &output) != 0) + die("bay_register failed\n"); + + if (mux_init(&mux, &bay, &select, &output, NULL) != 0) + die("mux_init failed\n"); for (int i = 0; i < N; i++) mux_add_input(&mux, value_int64(i), &inputs[i]);