//
// Grimoire
// Copyright 2025 Wenting Zhang
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "platform.h"
#include "board.h"
#include "app.h"

// 0x81 for DVI, 0x85 for DP
// Always use 0x85 for now, doesn't really matter
#define EDID_VID_IN_PARAM   (0x81)

#define SCREEN_SIZE_X   270
#define SCREEN_SIZE_Y   203
#define SCREEN_ASPECT   SCREEN_ASPECT_4_3

// 800x600 @ 60, 40MHz DMT
//#define SCREEN_CLK      40000
//#define SCREEN_HACT     800
//#define SCREEN_VACT     600
//#define SCREEN_HBLK     256
//#define SCREEN_HFP      40
//#define SCREEN_HSYNC    128
//#define SCREEN_VBLK     28
//#define SCREEN_VFP      1
//#define SCREEN_VSYNC    4

// DMT
#define SCREEN_CLK      162000
#define SCREEN_HACT     1600
#define SCREEN_VACT     1200
#define SCREEN_HBLK     560
#define SCREEN_HFP      64
#define SCREEN_HSYNC    192
#define SCREEN_VBLK     50
#define SCREEN_VFP      1
#define SCREEN_VSYNC    3

// CVT RBv2
//#define SCREEN_CLK      124488
//#define SCREEN_HACT     1600
//#define SCREEN_VACT     1200
//#define SCREEN_HBLK     80
//#define SCREEN_HFP      8
//#define SCREEN_HSYNC    32
//#define SCREEN_VBLK     35
//#define SCREEN_VFP      21
//#define SCREEN_VSYNC    8

static uint8_t edid[128] = {
    0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, // fixed header (0-7)
    0x6a, 0x12, // manufacturer ID (8-9)
    0x01, 0x00, // product code (10-11)
    0x42, 0x4b, 0x1d, 0x00, // serial number (12-15)
    0x01, // week of manufacture (16)
    0x20, // year of manufacture (17)
    0x01, // EDID version (18)
    0x03, // EDID revision (19)
    EDID_VID_IN_PARAM, // video input parameter (20)
    (SCREEN_SIZE_X / 10), // horizontal screen size in cm (21)
    (SCREEN_SIZE_Y / 10), // vertical screen size in cm (22)
    0x78, // display gamma (23)
    0x06, // supported feature (24)
    0xee, 0x95, 0xa3, 0x54, 0x4c, 0x99, 0x26, 0x0f, 0x50, 0x54, // chromatic (25-34)
    0x00, 0x00, 0x00, // established timing (35-37)
    0x01, // X resolution (38)
    0x00, // aspect ratio and vertical frequency (39)
    0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, // standard timing
    0x01, 0x00, 0x01, 0x00, 0x01, 0x00, // standard timing continued
    // descriptor 1 (54-71)
    ((SCREEN_CLK / 10) & 0xff), ((SCREEN_CLK / 10) >> 8), // pixel clock in 10kHz 54, 55
    (SCREEN_HACT & 0xff), // HACT LSB 56
    (SCREEN_HBLK & 0xff), // HBLK LSB 57
    (((SCREEN_HACT >> 8) << 4) | (SCREEN_HBLK >> 8)), // HACT MSB | HBLK MSB 58
    (SCREEN_VACT & 0xff), // VACT LSB 59
    (SCREEN_VBLK & 0xff), // VBLK LSB 60
    (((SCREEN_VACT >> 8) << 4) | (SCREEN_VBLK >> 8)), // VACT MSB | VBLK MSB 61
    (SCREEN_HFP & 0xff), // HFP LSB 62
    (SCREEN_HSYNC & 0xff), // HSYNC LSB 63
    (((SCREEN_VFP & 0xf) << 4) | (SCREEN_VSYNC & 0xf)), // VFP LSB | VSYNC LSB 64
    (((SCREEN_HFP >> 8) << 6) | ((SCREEN_HSYNC >> 8) << 4) |
        ((SCREEN_VFP >> 4) << 2) | (SCREEN_VSYNC >> 4)), // HFP MSB | HSYNC MSB | VFP MSB | VSYNC MSB 65
    (SCREEN_SIZE_X & 0xff), // Horizontal size in mm LSB 66
    (SCREEN_SIZE_Y & 0xff), // Vertical size in mm LSB 67
    (((SCREEN_SIZE_X >> 8) << 4) | (SCREEN_SIZE_Y >> 8)), // HSIZE MSB | VSIZE LSB 68
    0x00, // Horizontal border pixels 69
    0x00, // Vertical border lines 70
    0x1e, // Features bitmap 71
    // descriptor 2 (72-89) display name
    0x00, 0x00, 0x00, 0xfc, 0x00, 0x50, 0x61, 0x70, 0x65, 0x72, 0x20,
    0x4d, 0x6f, 0x6e, 0x69, 0x74, 0x6f, 0x72,
    // descriptor 3 (90-107) display name
    0x00, 0x00, 0x00, 0xfc, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
    0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
    // descriptor 4 (108-125) dummy
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, // number of extensions (126)
    0x00 // checksum (127)
};

void edid_init(void) {
    // Fill in runtime info
    uint32_t devid = board_get_uid();
    // Populate serial number with this ID
    edid[12] = (devid >> 24) & 0xff;
    edid[13] = (devid >> 16) & 0xff;
    edid[14] = (devid >> 8) & 0xff;
    edid[15] = (devid) & 0xff;

    // Fix checksum in EDID
    uint8_t checksum = 0;
    for (int i = 0; i < 127; i++) {
        checksum += edid[i];
    }
    checksum = ~checksum + 1;
    edid[127] = checksum;
}

uint8_t *edid_get_raw() {
    return edid;
}
