Skip to content

061 Reusable Module ​

061-reusable-module.mc defines a reusable LED circuit instead of a single physical part:

text
module LED_INDICATOR(in signal, psnk ground)
{
    RES R_LIMIT(330R, 50V)
    LED D_STATUS(2.0V, 5mA)

    signal -> R_LIMIT -> D_STATUS.ANODE
    D_STATUS.CATHODE -> ground
}
  • module LED_INDICATOR(...) names a reusable circuit block.
  • in signal declares an input port and psnk ground declares a power-supply port. These are connection points on the module, not physical component pins.
  • The resistor, LED, and their internal connections belong to each module instance.

module main creates STATUS_GREEN and STATUS_RED using the same module type. A module instance uses the same TYPE INSTANCE declaration shape as a component instance. Member access such as STATUS_GREEN.signal selects one module port:

text
LED_INDICATOR STATUS_GREEN
GPIO_GREEN -> STATUS_GREEN.signal
STATUS_GREEN.ground -> GND

Each instance owns its own R_LIMIT and D_STATUS, while both instances connect to the surrounding design through their ports.

bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 06-multi-file-project/061-reusable-module.mc --lib mcode --pass1 --pass2 --top main
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 06-multi-file-project/061-reusable-module.mc --lib mcode --viz --top main -o 06-multi-file-project/061-reusable-module.html

Source ​

text
// Example: Reusable LED Module
// Goal: Define a circuit block with ports and instantiate it twice.
// Language focus: module ports, module instances, explicit port connections.

module LED_INDICATOR(in signal, psnk ground)
{
    RES R_LIMIT(330R, 50V)
    LED D_STATUS(2.0V, 5mA)

    signal -> R_LIMIT -> D_STATUS.A
    D_STATUS.K -> ground
}

module main
{
    io GND
    io GPIO_GREEN
    io GPIO_RED

    LED_INDICATOR STATUS_GREEN
    LED_INDICATOR STATUS_RED

    GPIO_GREEN -> STATUS_GREEN.signal
    STATUS_GREEN.ground -> GND
    GPIO_RED -> STATUS_RED.signal
    STATUS_RED.ground -> GND
}

MCode Bench is the public face of the MCode stack. Closed capabilities are served through the MCode gateway.