aboutsummaryrefslogtreecommitdiff
path: root/qtmips_machine/lcddisplay.cpp
blob: bd12b1c785759132b76865fe2f936de6b665ec9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// SPDX-License-Identifier: GPL-2.0+
/*******************************************************************************
 * QtMips - MIPS 32-bit Architecture Subset Simulator
 *
 * Implemented to support following courses:
 *
 *   B35APO - Computer Architectures
 *   https://cw.fel.cvut.cz/wiki/courses/b35apo
 *
 *   B4M35PAP - Advanced Computer Architectures
 *   https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start
 *
 * Copyright (c) 2017-2019 Karel Koci<cynerd@email.cz>
 * Copyright (c) 2019      Pavel Pisa <pisa@cmp.felk.cvut.cz>
 *
 * Faculty of Electrical Engineering (http://www.fel.cvut.cz)
 * Czech Technical University        (http://www.cvut.cz/)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 ******************************************************************************/

#include "lcddisplay.h"

using namespace machine;

LcdDisplay::LcdDisplay() {
    change_counter = 0;
    size_t need_bytes;
    fb_size = 0x4b000;
    fb_bpp = 16;
    fb_width = 480;
    fb_height = 320;
    if (fb_bpp > 12) {
        fb_linesize = ((fb_bpp + 7) >> 3) * fb_width;
    } else {
        fb_linesize = (fb_bpp * fb_width + 7) >> 3;
    }
    need_bytes = fb_linesize * fb_height;

    if (fb_size <= need_bytes)
        fb_size = need_bytes;

    fb_data = new uchar[fb_size];
    std::fill(fb_data, fb_data + fb_size, 0);
}

LcdDisplay::~LcdDisplay() {
    if (fb_data != nullptr)
        delete[] fb_data;
}

std::uint32_t LcdDisplay::pixel_address(uint x, uint y)
{
    std::uint32_t address;
    address = y * fb_linesize;
    if (fb_bpp > 12)
        address += x * ((fb_bpp + 7) >> 3);
    else
        address += x * fb_bpp / 8;

    return address;
}


bool LcdDisplay::wword(std::uint32_t address, std::uint32_t value) {
    address &= ~3;
    uint x, y, r, g, b;
    std::uint32_t c;
    std::uint32_t last_addr = address + 3;
    std::uint32_t pixel_addr;

    if (address + 3 >= fb_size)
        return 0;
#if 0
    printf("LcdDisplay::wword address 0x%08lx data 0x%08lx\n",
           (unsigned long)address, (unsigned long)value);
#endif
    if (value == rword(address, true))
        return false;

    fb_data[address + 0] = (value >> 24) & 0xff;
    fb_data[address + 1] = (value >> 16) & 0xff;
    fb_data[address + 2] = (value >> 8) & 0xff;
    fb_data[address + 3] = (value >> 0) & 0xff;

    change_counter++;

    y = address / fb_linesize;
    if (fb_bpp > 12)
        x = (address - y * fb_linesize) / ((fb_bpp + 7) >> 3);
    else
        x = (address - y * fb_linesize) * 8 / fb_bpp;

    while ((pixel_addr = pixel_address(x, y)) <= last_addr) {
        c = fb_data[pixel_addr] << 8;
        c |= fb_data[pixel_addr + 1];

        r = ((c >> 11) & 0x1f) << 3;
        g = ((c >> 5) & 0x3f) << 2;
        b = ((c >> 0) & 0x1f) << 3;

        emit pixel_update(x, y, r, g, b);

        if (++x >= fb_width) {
            x = 0;
            y++;
        }
    }

    emit write_notification(address, value);

    return true;
}

std::uint32_t LcdDisplay::rword(std::uint32_t address, bool debug_access) const {
    address &= ~3;
    (void)debug_access;
    std::uint32_t value;

    if (address + 3 >= fb_size)
        return 0;

    value = (std::uint32_t)fb_data[address + 0] << 24;
    value |= (std::uint32_t)fb_data[address + 1] << 16;
    value |= (std::uint32_t)fb_data[address + 2] << 8;
    value |= (std::uint32_t)fb_data[address + 3] << 0;

#if 0
    printf("LcdDisplay::rword address 0x%08lx data 0x%08lx\n",
           (unsigned long)address, (unsigned long)value);
#endif

    emit read_notification(address, &value);

    return value;
}

std::uint32_t LcdDisplay::get_change_counter() const {
    return change_counter;
}