Changeset bdd5a63 in buchla-emu for emu/ser.c


Ignore:
Timestamp:
08/01/2017 10:32:52 PM (7 years ago)
Author:
Thomas Lopatic <thomas@…>
Branches:
master
Children:
6e313dd
Parents:
ea878ba
Message:

Separate thread for rendering.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • emu/ser.c

    rea878ba rbdd5a63  
    6060static SDL_Window *win;
    6161static SDL_Renderer *ren;
     62static SDL_atomic_t frame;
    6263
    6364static TTF_Font *fon;
     
    7071static int32_t bel = 0;
    7172
    72 static void update(void)
    73 {
     73static void scroll(void)
     74{
     75        memmove(mem, mem + 1, (CON_H - 1) * (CON_W + 1));
     76        memset(mem + (CON_H - 1), ' ', CON_W);
     77}
     78
     79static void forw(void)
     80{
     81        if (cur_x < CON_W - 1) {
     82                ++cur_x;
     83                return;
     84        }
     85
     86        if (cur_y == CON_H - 1) {
     87                cur_x = 0;
     88                scroll();
     89                return;
     90        }
     91
     92        cur_x = 0;
     93        ++cur_y;
     94}
     95
     96static void back(void)
     97{
     98        if (cur_x > 0) {
     99                --cur_x;
     100                return;
     101        }
     102
     103        if (cur_y == 0) {
     104                return;
     105        }
     106
     107        cur_x = CON_W - 1;
     108        --cur_y;
     109}
     110
     111static void down(void)
     112{
     113        if (cur_y < CON_H - 1) {
     114                ++cur_y;
     115                return;
     116        }
     117
     118        scroll();
     119}
     120
     121static void echo(uint8_t c)
     122{
     123        if (c < 32) {
     124                switch (c) {
     125                case '\r':
     126                        cur_x = 0;
     127                        break;
     128
     129                case '\n':
     130                        down();
     131                        break;
     132
     133                case '\b':
     134                        back();
     135                        break;
     136
     137                case '\a':
     138                        bel = BEL_CYC;
     139                        break;
     140
     141                default:
     142                        echo('^');
     143                        echo((uint8_t)(c + '@'));
     144                        return;
     145                }
     146        }
     147        else {
     148                mem[cur_y][cur_x] = c;
     149                forw();
     150        }
     151
     152        SDL_AtomicAdd(&frame, 1);
     153}
     154
     155static void out(int32_t un, uint8_t c)
     156{
     157        if (SDL_LockMutex(cpu_mutex) < 0) {
     158                fail("SDL_LockMutex() failed: %s", SDL_GetError());
     159        }
     160
     161        state[un].rdr = c;
     162        state[un].rdr_ok = true;
     163        state[un].irq_r = true;
     164
     165        if (SDL_UnlockMutex(cpu_mutex) < 0) {
     166                fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
     167        }
     168}
     169
     170void ser_sdl(void)
     171{
     172        ver3("ser_sdl()");
     173
     174        static int32_t last = 0;
     175        int32_t now = SDL_AtomicGet(&frame);
     176
     177        if (last == now) {
     178                ver3("no update");
     179                return;
     180        }
     181
     182        last = now;
     183
    74184        if (SDL_FillRect(sur, NULL, bel == 0 ? CON_BGR : CON_BEL) < 0) {
    75185                fail("SDL_FillRect() failed: %s", SDL_GetError());
     
    86196
    87197        for (int32_t y = 0; y < CON_H; ++y) {
    88                 SDL_Surface *lin = TTF_RenderText_Blended(fon, (char *)mem[y], CON_FGR);
     198                char line[CON_W + 1];
     199
     200                if (SDL_LockMutex(cpu_mutex) < 0) {
     201                        fail("SDL_LockMutex() failed: %s", SDL_GetError());
     202                }
     203
     204                memcpy(line, mem[y], CON_W + 1);
     205
     206                if (SDL_UnlockMutex(cpu_mutex) < 0) {
     207                        fail("SDL_UnlockMutex() failed: %s", SDL_GetError());
     208                }
     209
     210                SDL_Surface *lin = TTF_RenderText_Blended(fon, line, CON_FGR);
    89211
    90212                if (lin == NULL) {
     
    118240}
    119241
    120 static void scroll(void)
    121 {
    122         memmove(mem, mem + 1, (CON_H - 1) * (CON_W + 1));
    123         memset(mem + (CON_H - 1), ' ', CON_W);
    124 }
    125 
    126 static void forw(void)
    127 {
    128         if (cur_x < CON_W - 1) {
    129                 ++cur_x;
    130                 return;
    131         }
    132 
    133         if (cur_y == CON_H - 1) {
    134                 cur_x = 0;
    135                 scroll();
    136                 return;
    137         }
    138 
    139         cur_x = 0;
    140         ++cur_y;
    141 }
    142 
    143 static void back(void)
    144 {
    145         if (cur_x > 0) {
    146                 --cur_x;
    147                 return;
    148         }
    149 
    150         if (cur_y == 0) {
    151                 return;
    152         }
    153 
    154         cur_x = CON_W - 1;
    155         --cur_y;
    156 }
    157 
    158 static void down(void)
    159 {
    160         if (cur_y < CON_H - 1) {
    161                 ++cur_y;
    162                 return;
    163         }
    164 
    165         scroll();
    166 }
    167 
    168 static void echo(uint8_t c)
    169 {
    170         if (c < 32) {
    171                 switch (c) {
    172                 case '\r':
    173                         cur_x = 0;
    174                         break;
    175 
    176                 case '\n':
    177                         down();
    178                         break;
    179 
    180                 case '\b':
    181                         back();
    182                         break;
    183 
    184                 case '\a':
    185                         bel = BEL_CYC;
    186                         break;
    187 
    188                 default:
    189                         echo('^');
    190                         echo((uint8_t)(c + '@'));
    191                         return;
    192                 }
    193         }
    194         else {
    195                 mem[cur_y][cur_x] = c;
    196                 forw();
    197         }
    198 
    199         update();
    200 }
    201 
    202 static void out(int32_t un, uint8_t c)
    203 {
    204         state[un].rdr = c;
    205         state[un].rdr_ok = true;
    206         state[un].irq_r = true;
    207 }
    208 
    209242void ser_key(SDL_KeyboardEvent *ev)
    210243{
     
    252285        }
    253286
     287        SDL_AtomicSet(&frame, 1);
     288
    254289        SDL_RWops *ops = SDL_RWFromFile(CON_FONT, "rb");
    255290
     
    286321                mem[y][CON_W] = 0;
    287322        }
    288 
    289         update();
    290323}
    291324
     
    309342
    310343                if (bel == BEL_CYC - 1 || bel == 0) {
    311                         update();
     344                        SDL_AtomicAdd(&frame, 1);
    312345                }
    313346        }
Note: See TracChangeset for help on using the changeset viewer.