/*
 * Copyright (c) 2022, RPTU Kaiserslautern-Landau
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors:
 *    Derek Christ
 */

#include "AddressDecoderConfigs.h"

#include <gtest/gtest.h>

#include <DRAMSys/configuration/memspec/MemSpecLPDDR5.h>
#include <DRAMSys/simulation/AddressDecoder.h>

class AddressDecoderFixture : public ::testing::Test
{
protected:
    AddressDecoderFixture() :
        addressMappingJson(nlohmann::json::parse(addressMappingJsonString).at("addressmapping")),
        memSpecJson(nlohmann::json::parse(memSpecJsonString).at("memspec")),
        addressMappingConfig(addressMappingJson.get<DRAMSys::Config::AddressMapping>()),
        memSpecConfig(memSpecJson.get<DRAMSys::Config::MemSpec>()),
        memSpec(memSpecConfig),
        addressDecoder(addressMappingConfig, memSpec)
    {
    }

    nlohmann::json addressMappingJson;
    nlohmann::json memSpecJson;

    // Configs
    DRAMSys::Config::AddressMapping addressMappingConfig;
    DRAMSys::Config::MemSpec memSpecConfig;

    DRAMSys::MemSpecLPDDR5 memSpec;
    DRAMSys::AddressDecoder addressDecoder;
};

TEST_F(AddressDecoderFixture, Decoding)
{
    uint64_t address = 0x3A59'1474;
    auto decodedAddress = addressDecoder.decodeAddress(address);

    unsigned int channel = decodedAddress.channel;
    unsigned int rank = decodedAddress.rank;
    unsigned int bankgroup = decodedAddress.bankgroup;
    unsigned int bank = decodedAddress.bank;
    unsigned int row = decodedAddress.row;
    unsigned int column = decodedAddress.column;
    unsigned int byte = decodedAddress.byte;

    EXPECT_EQ(channel, 0);
    EXPECT_EQ(rank, 0);
    EXPECT_EQ(bankgroup, 3);
    EXPECT_EQ(bank, 12);
    EXPECT_EQ(row, 29874);
    EXPECT_EQ(column, 170);
    EXPECT_EQ(byte, 0);
}

TEST_F(AddressDecoderFixture, Encoding)
{
    unsigned int channel = 0;
    unsigned int rank = 0;
    unsigned int bankgroup = 3;
    unsigned int bank = 12;
    unsigned int row = 29874;
    unsigned int column = 170;
    unsigned int byte = 0;

    DRAMSys::DecodedAddress decodedAddress(channel, rank, bankgroup, bank, row, column, byte);

    uint64_t address = addressDecoder.encodeAddress(decodedAddress);
    EXPECT_EQ(address, 0x3A59'1474);
}

TEST_F(AddressDecoderFixture, DeEncoding)
{
    std::array testAddresses{std::uint64_t(0x3A59'1474),
                             std::uint64_t(0x0000'0000),
                             std::uint64_t(0x2FFA'1231),
                             std::uint64_t(0x0001'FFFF)};

    for (auto address : testAddresses)
    {
        DRAMSys::DecodedAddress decodedAddress = addressDecoder.decodeAddress(address);
        uint64_t encodedAddress = addressDecoder.encodeAddress(decodedAddress);

        EXPECT_EQ(encodedAddress, address);
    }
}