// Copyright 2012 Accellera Systems Initiative
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// 'xor' module with variable number of input terminals 

`include "disciplines.vams"

module V_xor(in, out);

    parameter integer size = 2 from [2:inf);

    input [0:size-1] in;
    output out;
    voltage [0:size-1] in;
    voltage out;

    parameter real vout_high = 5;
    parameter real vout_low = 0 from (-inf:vout_high);
    parameter real vth = 1.4;
    parameter real tdelay = 5n from [0:inf);
    parameter real trise = 1n from [0:inf);
    parameter real tfall = 1n from [0:inf);

    integer in_state[0:size-1];
    integer out_state;
    real vout;
    genvar i, j;

    analog begin
        @(initial_step)
            for (i = 0; i < size; i = i + 1)
                in_state[i] = 0;

        for (i = 0; i < size; i = i + 1)
            @(cross(V(in[i]) - vth)) begin
                in_state[i] = V(in[i]) > vth;
                out_state = 0;
                for (j = 0; j < size; j = j + 1)
                    if (in_state[j])
                        out_state = out_state + 1;
                if (out_state == 1)
                    vout = vout_high;
                else
                    vout = vout_low;
            end

        V(out) <+ transition(vout, tdelay, trise, tfall);

    end

endmodule
