[{"data":1,"prerenderedAt":213},["ShallowReactive",2],{"example-page-jumpnrun":3},{"id":4,"info":5,"parts":6,"wired":30,"refs":31,"nets":32,"moreNets":206,"code":207},"jumpnrun","A side-scrolling platformer in bare-metal C on an STM32F411 (Cortex-M4) with the 1.8\" ST7735S colour TFT mounted in landscape. Arrow keys run, space jumps (or click SW1-SW3). Collect coins, jump over gaps, reach the flag. The display scrolls in hardware (SCRLAR\u002FVSCSAD): each frame sends only the new columns and the player over SPI, and the core sleeps between frames. The source is in U1: change it in the code view (level, speed, colours) and run again.",[7,10,13,16,18,20,22,24,26,28],[8,9],"capacitor",7,[11,12],"button",3,[14,15],"ch9350l",1,[17,15],"ldo",[19,15],"resistor",[21,15],"st7735_tft",[23,15],"stm32f411",[25,15],"usb_a_socket",[27,15],"usb_keyboard",[29,15],"vdc",false,{"C1":8,"C2":8,"C3":8,"C5":8,"C6":8,"C7":8,"C8":8,"DS1":21,"J1":25,"KB1":27,"R3":19,"SW1":11,"SW2":11,"SW3":11,"U1":23,"U2":17,"U3":14,"V1":29},[33,77,91,108,113,118,124,130,136,142,149,156,162,169,176,183,190,198],{"name":34,"pins":35},"0",[36,39,41,43,45,47,49,51,54,56,58,60,62,64,66,68,70,72,74],[37,38],"C1","2",[40,38],"C2",[42,38],"C3",[44,38],"C5",[46,38],"C6",[48,38],"C7",[50,38],"C8",[52,53],"DS1","GND",[55,53],"J1",[57,53],"KB1",[59,38],"R3",[61,38],"SW1",[63,38],"SW2",[65,38],"SW3",[67,53],"U1",[69,53],"U2",[71,53],"U3",[71,73],"RST",[75,76],"V1","−",{"name":78,"pins":79},"3V3",[80,82,83,85,87,89],[40,81],"1",[42,81],[52,84],"VCC",[52,86],"LED",[67,88],"VDD",[69,90],"OUT",{"name":92,"pins":93},"V5",[94,95,96,97,99,100,102,104,106],[37,81],[46,81],[50,81],[55,98],"VBUS",[57,98],[69,101],"IN",[69,103],"EN",[71,105],"VIN5",[75,107],"+",{"name":109,"pins":110},"VCAP1",[111,112],[44,81],[67,109],{"name":114,"pins":115},"BOOT0",[116,117],[59,81],[67,114],{"name":119,"pins":120},"BTN_JUMP",[121,122],[65,81],[67,123],"PC2",{"name":125,"pins":126},"BTN_LEFT",[127,128],[61,81],[67,129],"PC0",{"name":131,"pins":132},"BTN_RIGHT",[133,134],[63,81],[67,135],"PC1",{"name":137,"pins":138},"KBD_3V3",[139,140],[48,81],[71,141],"VDD33",{"name":143,"pins":144},"KBD_RX",[145,147],[67,146],"PA3",[71,148],"TXD",{"name":150,"pins":151},"MOSI",[152,154],[52,153],"SDA\u002FMOSI",[67,155],"PA7",{"name":157,"pins":158},"SCK",[159,160],[52,157],[67,161],"PA5",{"name":163,"pins":164},"TFT_CS",[165,167],[52,166],"CS",[67,168],"PA4",{"name":170,"pins":171},"TFT_DC",[172,174],[52,173],"DC",[67,175],"PA6",{"name":177,"pins":178},"TFT_RES",[179,181],[52,180],"RES",[67,182],"PA1",{"name":184,"pins":185},"UART_TX",[186,188],[67,187],"PA2",[71,189],"RXD",{"name":191,"pins":192},"USB_DM",[193,195,196],[55,194],"D−",[57,194],[71,197],"DM (port 1 D−)",{"name":199,"pins":200},"USB_DP",[201,203,204],[55,202],"D+",[57,202],[71,205],"DP (port 1 D+)",0,{"file":208,"lang":209,"text":210,"lines":211,"more":212},"sketch.ino","cpp","\u002F\u002F Jump & run on an STM32F411 with a 1.8\" ST7735S TFT (landscape, 160 x 128, RGB565).\n\u002F\u002F\n\u002F\u002F Wiring: SPI1 SCK PA5, MOSI PA7 · TFT CS PA4, DC PA6, RES PA1 · buttons to GND with pull-ups:\n\u002F\u002F left PC0, right PC1, jump PC2 · USB keyboard on a CH9350L host module: its TXD to PA3\n\u002F\u002F (USART2 RX), 115200 8N1. In the simulator your keyboard is that USB keyboard: arrows or A \u002F D\n\u002F\u002F run, space, W or arrow up jump.\n\u002F\u002F\n\u002F\u002F Speed: the display scrolls in hardware (ST7735S SCRLAR\u002FVSCSAD). Per frame only the columns\n\u002F\u002F that come into view and the places where a sprite was or is now are sent over SPI, a few\n\u002F\u002F hundred bytes instead of 40 kB for a full frame. Between frames the core sleeps (WFI).\n#include \"clab.h\"\n\n#define TFT_CS  PA(4)\n#define TFT_DC  PA(6)\n#define TFT_RES PA(1)\n#define BTN_L   PC(0)\n#define BTN_R   PC(1)\n#define BTN_J   PC(2)\n\n#define W 144           \u002F\u002F playfield width; the 16 px strip on the left is the fixed status bar\n#define BAR 16\n#define H 128\n#define TS 8            \u002F\u002F tile size\n#define MAP_W 240       \u002F\u002F level width in tiles (1920 px)\n#define MAP_H 16\n#define MAXE 18         \u002F\u002F enemies in the level\n\n\u002F\u002F ---- SPI1 and the display ---------------------------------------------------------------------\nstatic void spi_init(void) {\n  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;\n  RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;\n  \u002F\u002F PA5 SCK, PA7 MOSI: alternate function 5\n  GPIOA->MODER = (GPIOA->MODER & ~((3u \u003C\u003C 10) | (3u \u003C\u003C 14))) | (2u \u003C\u003C 10) | (2u \u003C\u003C 14);\n  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~((15u \u003C\u003C 20) | (15u \u003C\u003C 28))) | (5u \u003C\u003C 20) | (5u \u003C\u003C 28);\n  GPIOA->OSPEEDR |= (3u \u003C\u003C 10) | (3u \u003C\u003C 14);\n  SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI;   \u002F\u002F mode 0, f_PCLK \u002F 2\n  SPI1->CR1 |= SPI_CR1_SPE;\n}\nstatic inline void spi_byte(uint8_t b) {\n  while (!(SPI1->SR & SPI_SR_TXE)) {}\n  *(volatile uint8_t *)&SPI1->DR = b;\n}\nstatic inline void spi_done(void) { while (SPI1->SR & SPI_SR_BSY) {} }\nstatic void cmd(uint8_t c) { spi_done(); pin_write(TFT_DC, false); spi_byte(c); spi_done(); pin_write(TFT_DC, true); }\nstatic inline void data(uint8_t d) { spi_byte(d); }\nstatic void cmd16x2(uint8_t c, uint16_t a, uint16_t b) { cmd(c); data(a >> 8); data(a); data(b >> 8); data(b); }\n\nstatic void tft_init(void) {\n  pin_output(TFT_CS); pin_output(TFT_DC); pin_output(TFT_RES);\n  pin_write(TFT_CS, true);\n  pin_write(TFT_RES, false); delay_ms(1); pin_write(TFT_RES, true); delay_ms(130);\n  pin_write(TFT_CS, false);\n  cmd(0x01); delay_ms(130);                 \u002F\u002F SWRESET\n  cmd(0x11); delay_ms(130);                 \u002F\u002F SLPOUT\n  cmd(0x3A); data(0x05);                    \u002F\u002F COLMOD 16 bit\n  cmd(0x36); data(0xA8);                    \u002F\u002F MADCTL MY | MV | BGR: landscape, BGR colour filter panel\n  \u002F\u002F SCRLAR: lines 0..143 scroll (the playfield), lines 144..159 stay: in landscape the status bar\n  cmd16x2(0x33, 0, W); data(0); data(BAR);\n  cmd(0x29);                                \u002F\u002F DISPON\n}\n\u002F** hardware scroll so that world column `cam` is at the left edge *\u002F\nstatic void scroll_to(int cam) {\n  uint16_t ssa = (uint16_t)((W - (cam % W)) % W);\n  cmd(0x37); data(ssa >> 8); data(ssa);\n}\n\n\u002F\u002F ---- level -----------------------------------------------------------------------------------\nenum { SKY, GROUND, GRASS, BRICK, COIN, FLAG, POLE, QBOX, USED, WATER, BUSH, FLOWER, CASTLE };\nstatic uint8_t map[MAP_W][MAP_H];\nstatic uint8_t gtop[MAP_W];   \u002F\u002F ground row of each column (MAP_H over water)\nstatic uint32_t seed;\nstatic int rnd(int n) { seed = seed * 1103515245u + 12345u; return (int)((seed >> 16) % (uint32_t)n); }\n\u002F** small hash for decoration (clouds, pebbles, grass) that never changes *\u002F\nstatic uint32_t hash(uint32_t x) { x ^= x >> 16; x *= 0x7feb352du; x ^= x >> 15; x *= 0x846ca68bu; return x ^ (x >> 16); }\n\n\u002F\u002F enemies: slimes crawl (stomp them), bats flutter (stomp them), hedgehogs have spikes (jump over)\nenum { E_SLIME, E_BAT, E_SPIKY };\ntypedef struct { int x, y, vx, alive, ox, oy, type, bx, by, ph, oph; } Enemy;   \u002F\u002F x, y in pixels\nstatic Enemy en[MAXE];\nstatic int nen;\nstatic int castle_x, castle_y;   \u002F\u002F castle at the end (pixels)\n\nstatic void make_level(void) {\n  seed = 12345; nen = 0;\n  int ground = 13;\n  for (int x = 0; x \u003C MAP_W; x++) {\n    if (x > 12 && x \u003C MAP_W - 24 && rnd(100) \u003C 6) {   \u002F\u002F a gap with water at the bottom\n      int len = 2 + rnd(2);\n      for (int k = 0; k \u003C len && x \u003C MAP_W; k++, x++) { for (int y = 0; y \u003C MAP_H; y++) map[x][y] = y >= 14 ? WATER : SKY; gtop[x] = MAP_H; }\n      x--; continue;\n    }\n    if (x > 12 && x \u003C MAP_W - 24 && rnd(100) \u003C 10) { ground += rnd(3) - 1; if (ground \u003C 9) ground = 9; if (ground > 13) ground = 13; }\n    gtop[x] = (uint8_t)ground;\n    for (int y = 0; y \u003C MAP_H; y++) map[x][y] = y \u003C ground ? SKY : y == ground ? GRASS : GROUND;\n    if (x > 3 && x \u003C MAP_W - 20 && rnd(100) \u003C 16) map[x][ground - 1] = rnd(3) ? BUSH : FLOWER;\n  }\n  \u002F\u002F brick platforms 4-5 tiles above the ground in front (one jump), ?-boxes in them, coins on top;\n  \u002F\u002F now and then a second one higher up\n  for (int x = 18; x \u003C MAP_W - 26; x += 8 + rnd(7)) {\n    int g = gtop[x] \u003C MAP_H ? gtop[x] : 13, y = g - 4 - rnd(2), len = 3 + rnd(4);\n    for (int k = 0; k \u003C len; k++) {\n      map[x + k][y] = rnd(5) == 0 ? QBOX : BRICK;\n      if (map[x + k][y] == BRICK && rnd(3)) map[x + k][y - 1] = COIN;\n    }\n    if (rnd(3) == 0 && y >= 6) {\n      int x2 = x + len - 1;\n      for (int k = 0; k \u003C 3; k++) { map[x2 + k][y - 4] = k == 1 ? QBOX : BRICK; map[x2 + k][y - 5] = COIN; }\n    }\n  }\n  \u002F\u002F the goal: a pole with a pennant, then the castle\n  int g = gtop[MAP_W - 14];\n  for (int y = 3; y \u003C g; y++) map[MAP_W - 14][y] = POLE;\n  map[MAP_W - 14][3] = FLAG;\n  castle_x = (MAP_W - 9) * TS; castle_y = (g - 5) * TS;\n  for (int x = MAP_W - 9; x \u003C MAP_W - 4; x++) for (int y = g - 5; y \u003C g; y++) map[x][y] = CASTLE;\n  \u002F\u002F enemies, away from the start\n  for (int x = 26; x \u003C MAP_W - 26 && nen \u003C MAXE; x += 10 + rnd(8)) {\n    int kind = rnd(10);\n    Enemy *e = &en[nen];\n    if (kind \u003C 3) {                                    \u002F\u002F bat: flies a wave above the ground",558,true,1796304401145]